fix: sample in readme so it compiles (#1162)

This commit is contained in:
Dag König
2021-04-01 18:13:51 +02:00
committed by GitHub
parent 7e99d44b52
commit f1fa8256de

112
README.md
View File

@@ -96,33 +96,32 @@ You can force the use of `System.Console` on Unix as well; see `Core.cs`.
See the [`Terminal.Gui/` README](https://github.com/migueldeicaza/gui.cs/tree/master/Terminal.Gui) for an overview of how the library is structured. The [Conceptual Documentation](https://migueldeicaza.github.io/gui.cs/articles/index.html) provides insight into core concepts.
### Sample Usage
The code below is done with the new [Top-level statements](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#top-level-statements) in C# 9.0.
```csharp
using Terminal.Gui;
using NStack;
class Demo {
static void Main ()
{
Application.Init ();
var top = Application.Top;
Application.Init();
var top = Application.Top;
// Creates the top-level window to show
var win = new Window ("MyApp") {
X = 0,
Y = 1, // Leave one row for the toplevel menu
// Creates the top-level window to show
var win = new Window("MyApp")
{
X = 0,
Y = 1, // Leave one row for the toplevel menu
// By using Dim.Fill(), it will automatically resize without manual intervention
Width = Dim.Fill (),
Height = Dim.Fill ()
};
top.Add (win);
// By using Dim.Fill(), it will automatically resize without manual intervention
Width = Dim.Fill(),
Height = Dim.Fill()
};
// Creates a menubar, the item "New" has a help menu.
var menu = new MenuBar (new MenuBarItem [] {
top.Add(win);
// Creates a menubar, the item "New" has a help menu.
var menu = new MenuBar(new MenuBarItem[] {
new MenuBarItem ("_File", new MenuItem [] {
new MenuItem ("_New", "Creates new file", NewFile),
new MenuItem ("_Close", "", () => Close ()),
new MenuItem ("_New", "Creates new file", null),
new MenuItem ("_Close", "",null),
new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
}),
new MenuBarItem ("_Edit", new MenuItem [] {
@@ -131,41 +130,48 @@ class Demo {
new MenuItem ("_Paste", "", null)
})
});
top.Add (menu);
top.Add(menu);
var login = new Label ("Login: ") { X = 3, Y = 2 };
var password = new Label ("Password: ") {
X = Pos.Left (login),
Y = Pos.Top (login) + 1
};
var loginText = new TextField ("") {
X = Pos.Right (password),
Y = Pos.Top (login),
Width = 40
};
var passText = new TextField ("") {
Secret = true,
X = Pos.Left (loginText),
Y = Pos.Top (password),
Width = Dim.Width (loginText)
};
// Add some controls,
win.Add (
// The ones with my favorite layout system, Computed
login, password, loginText, passText,
// The ones laid out like an australopithecus, with Absolute positions:
new CheckBox (3, 6, "Remember me"),
new RadioGroup (3, 8, new [] { "_Personal", "_Company" }),
new Button (3, 14, "Ok"),
new Button (10, 14, "Cancel"),
new Label (3, 18, "Press F9 or ESC plus 9 to activate the menubar")
);
Application.Run ();
}
static bool Quit()
{
var n = MessageBox.Query(50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
return n == 0;
}
var login = new Label("Login: ") { X = 3, Y = 2 };
var password = new Label("Password: ")
{
X = Pos.Left(login),
Y = Pos.Top(login) + 1
};
var loginText = new TextField("")
{
X = Pos.Right(password),
Y = Pos.Top(login),
Width = 40
};
var passText = new TextField("")
{
Secret = true,
X = Pos.Left(loginText),
Y = Pos.Top(password),
Width = Dim.Width(loginText)
};
// Add some controls,
win.Add(
// The ones with my favorite layout system, Computed
login, password, loginText, passText,
// The ones laid out like an australopithecus, with Absolute positions:
new CheckBox(3, 6, "Remember me"),
new RadioGroup(3, 8, new ustring[] { "_Personal", "_Company" }, 0),
new Button(3, 14, "Ok"),
new Button(10, 14, "Cancel"),
new Label(3, 18, "Press F9 or ESC plus 9 to activate the menubar")
);
Application.Run();
```
Alternatively, you can encapsulate the app behavior in a new `Window`-derived class, say `App.cs` containing the code above, and simplify your `Main` method to: