Update 'Sample Usage' README.md section

This commit is contained in:
tznind
2022-10-20 16:18:29 +01:00
parent b3ef79ccf5
commit 121d0a0cc8
2 changed files with 43 additions and 76 deletions

119
README.md
View File

@@ -61,98 +61,65 @@ See the [`Terminal.Gui/` README](https://github.com/gui-cs/Terminal.Gui/tree/mas
## Sample Usage in C#
The following example shows basic Terminal.Gui application syntax.
![Simple Usage app](docfx/images/simpleusage.png)
```csharp
// A simple Terminal.Gui example in C# - using C# 9.0 Top-level statements
using Terminal.Gui;
using NStack;
Application.Init ();
// Initialize the console
Application.Init();
// Creates the top-level window to show
var win = new Window ("Example App") {
X = 0,
Y = 1, // Leave one row for the toplevel menu
// Creates the top-level window with border and title
var win = new Window("Example App (Ctrl+Q to quit)");
// By using Dim.Fill(), this Window will automatically resize without manual intervention
Width = Dim.Fill (),
Height = Dim.Fill ()
};
// Create input components and labels
Application.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 a new file", null),
new MenuItem ("_Close", "",null),
new MenuItem ("_Quit", "", () => { if (Quit ()) Application.Top.Running = false; })
}),
new MenuBarItem ("_Edit", new MenuItem [] {
new MenuItem ("_Copy", "", null),
new MenuItem ("C_ut", "", null),
new MenuItem ("_Paste", "", null)
})
});
Application.Top.Add (menu);
static bool Quit ()
var usernameLabel = new Label("Username:");
var usernameText = new TextField("")
{
var n = MessageBox.Query (50, 7, "Quit Example", "Are you sure you want to quit this example?", "Yes", "No");
return n == 0;
}
// Position text field adjacent to label
X = Pos.Right(usernameLabel) + 1,
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)
// Fill remaining horizontal space with a margin of 1
Width = Dim.Fill(1),
};
// Add the views to the main window,
win.Add (
// Using Computed Layout:
login, password, loginText, passText,
var passwordLabel = new Label(0,2,"Password:");
var passwordText = new TextField("")
{
Secret = true,
// align with the text box above
X = Pos.Left(usernameText),
Y = 2,
Width = Dim.Fill(1),
};
// Using Absolute Layout:
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")
// Create login button
var btnLogin = new Button("Login")
{
Y = 4,
// center the login button horizontally
X = Pos.Center(),
IsDefault = true,
};
// When login button is clicked display a message popup
btnLogin.Clicked += () => MessageBox.Query("Logging In", "Login Successful", "Ok");
// Add all the views to the window
win.Add(
usernameLabel, usernameText, passwordLabel, passwordText,btnLogin
);
// Run blocks until the user quits the application
Application.Run ();
// Show the application
Application.Run(win);
// Always bracket Application.Init with .Shutdown.
Application.Shutdown ();
```
The example above shows adding views using both styles of layout supported by **Terminal.Gui**: **Absolute layout** and **[Computed layout](https://gui-cs.github.io/Terminal.Gui/articles/overview.html#layout)**.
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:
```csharp
using Terminal.Gui;
class Demo {
static void Main ()
{
Application.Run<App> ();
Application.Shutdown ();
}
}
// After the application exits, release and reset console for clean shutdown
Application.Shutdown();
```
## Installing

BIN
docfx/images/Example.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB