C# – First steps with C# and .NET – part 004 .
This tutorial is about how to start with an Empty project, use packages, add a class, and finish the project.
First, you need to start with an Empty Project from the main menu: File – New – Project.
Select the Windows Desktop – Empty Project adds the name of your project.
I named my project TerminalGui_001 because I used a package from NuGet named Terminal.Gui.
Use from the menu the Tool – Nuget Package Manager – Package Manage – Console to open the command shell for NuGet packages.
Into this command shell – is into the bottom area of Visual Studio, type this:
1 | PM> Install-Package Terminal.Gui |
This will install the Terminal.Gui package into your project.
Now you need to add your C# class.
Use right-click on your project tree structure and select Add – New Item… or keys Ctrl+Shift+A or just search the menu Project.
This will open a dialog and you need to select Class and name your class.
I named my class Ex001.
Now you can use the features of a package named Terminal.Gui.
About this package, you can read more here.
My example is simple and uses some controls to understand how is working.
Let’s see the source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Terminal.Gui; namespace TerminalGui_001 { class Ex001 { static void Main() { Application.Init(); var top = Application.Top; // Creates the top-level window to show var win = new Window(new Rect(0, 1, top.Frame.Width, top.Frame.Height - 1), "Ex001"); 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", "New file", NewFile), new MenuItem ("_Open", "Open file", OpenFile), new MenuItem ("_Close", "", () => Close ()), new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; }) }), new MenuBarItem ("_Edit", new MenuItem [] { new MenuItem ("_Copy", "", null), new MenuItem ("C_ut", "", null), new MenuItem ("_Paste", "", null) }) }); top.Add(menu); // Add some controls win.Add( new Label(3, 2, "Normal edit box: "), new TextField(14, 2, 40, ""), new Label(3, 4, "Password edit box: "), new TextField(14, 4, 40, "") { Secret = true }, new CheckBox(3, 6, "CheckBox"), new RadioGroup(3, 8, new[] { "check 1", "check 2", "check 3" }), new Button(3, 14, "Button Ok"), new Button(10, 14, "Button Cancel"), new Label(3, 18, "This is a label ")); Application.Run(); } private static bool Quit() { var mess_quit = MessageBox.Query(50, 7, "Quit Ex001", "Are you sure !?", "Yes", "No"); return mess_quit == 0; } private static void Close() { MessageBox.ErrorQuery(50, 5, "Error", "... nothing to close", "Yes!"); } public static Label my_dialog_label; private static void NewFile() { var my_dialog = new Dialog( "New File", 50, 20, new Button("Ok", is_default: true) { Clicked = () => { Application.RequestStop(); } }, new Button("Cancel") { Clicked = () => { Application.RequestStop(); } }); my_dialog_label = new Label(1, 1, "This is a label !"); my_dialog.Add(my_dialog_label); Application.Run(my_dialog); } public static void OpenFile() { var my_open_dialog = new OpenDialog("Open", "Open a file"); Application.Run(my_open_dialog); } } } |
Use Build – Build Solution or Ctrl+Shift+B then press the Start button to run it.
Let’s see the result of this source code: