Update Example.cs to match root README.md

This commit is contained in:
tznind
2022-10-20 18:36:53 +01:00
parent 121d0a0cc8
commit 516fec87e7
2 changed files with 45 additions and 62 deletions

View File

@@ -1,76 +1,57 @@
// A simple Terminal.Gui example in C# - using C# 9.0 Top-level statements // This is a simple example application. For the full range of functionality
// This is the same code found in the Termiminal Gui README.md file. // see the UICatalog project
// A simple Terminal.Gui example in C# - using C# 9.0 Top-level statements
using Terminal.Gui; using Terminal.Gui;
using NStack;
Application.Init (); // Initialize the console
Application.Init();
// Creates the top-level window to show // Creates the top-level window with border and title
var win = new Window ("Example App") { var win = new Window("Example App (Ctrl+Q to quit)");
X = 0,
Y = 1, // Leave one row for the toplevel menu
// By using Dim.Fill(), this Window will automatically resize without manual intervention // Create input components and labels
Width = Dim.Fill (),
Height = Dim.Fill ()
};
Application.Top.Add (win); var usernameLabel = new Label("Username:");
var usernameText = new TextField("")
// 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 n = MessageBox.Query (50, 7, "Quit Example", "Are you sure you want to quit this example?", "Yes", "No"); // Position text field adjacent to label
return n == 0; X = Pos.Right(usernameLabel) + 1,
}
var login = new Label ("Login: ") { X = 3, Y = 2 }; // Fill remaining horizontal space with a margin of 1
var password = new Label ("Password: ") { Width = Dim.Fill(1),
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 the views to the main window, var passwordLabel = new Label(0,2,"Password:");
win.Add ( var passwordText = new TextField("")
// Using Computed Layout: {
login, password, loginText, passText, Secret = true,
// align with the text box above
X = Pos.Left(usernameText),
Y = 2,
Width = Dim.Fill(1),
};
// Using Absolute Layout: // Create login button
new CheckBox (3, 6, "Remember me"), var btnLogin = new Button("Login")
new RadioGroup (3, 8, new ustring [] { "_Personal", "_Company" }, 0), {
new Button (3, 14, "Ok"), Y = 4,
new Button (10, 14, "Cancel"), // center the login button horizontally
new Label (3, 18, "Press F9 or ESC plus 9 to activate the menubar") 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 // Show the application
Application.Run (); Application.Run(win);
// Always bracket Application.Init with .Shutdown. // After the application exits, release and reset console for clean shutdown
Application.Shutdown (); Application.Shutdown();

View File

@@ -4,6 +4,8 @@ This example shows how to use the Terminal.Gui library to create a simple GUI ap
This is the same code found in the Terminal.Gui README.md file. This is the same code found in the Terminal.Gui README.md file.
To explore the full range of functionality in Terminal.Gui, see the [UICatalog](../UICatalog) project
See [README.md](https://github.com/gui-cs/Terminal.Gui) for a list of all Terminal.Gui samples. See [README.md](https://github.com/gui-cs/Terminal.Gui) for a list of all Terminal.Gui samples.
Note, the old `demo.cs` example has been deleted because it was not a very good example. It can still be found in the [git history](https://github.com/gui-cs/Terminal.Gui/tree/v1.8.2). Note, the old `demo.cs` example has been deleted because it was not a very good example. It can still be found in the [git history](https://github.com/gui-cs/Terminal.Gui/tree/v1.8.2).