Nov 17, 2014

C# tip 1: Open a new window with a button click

Finally, I decide to forget about the cross-platform thing and just want to finish my project as soon as possible. Because I mainly use Windows, I turn to C# for the GUI development.  I write my code in C++ with Visual Studio, so it may be easier for me to write C# under the same environment.

Now I'm thinking of building the GUI with C#, the main calculation will still be running in C++. The idea is C# just for the configuration and output of parameter list, which would be imported later in C++ code. All the front end and data processing are built with C#, only the developer touches on the C++  calculation part.

The first tip I eager to learn is how to generate a Window by click a button.
It it rather intuitive in C#.


  • Right click on the project name and select Add -> Window, give your new window a name, say, comEditorWindow.



  •  Drag a button object to the design viewer, name it.



  • In the Properties, click on "Event handler"(thundering mark).



  • At option "Click", input a name for you click, say, "buttonClick" then the Visual Studio would jump right into the method by which you write about what will happen when click the button.


One thing to note is that if you choose "Show( )" instead of "ShowDialog( )", then the user could click on the main window freely, so usually it's recommended to use "ShowDialog( )", or the user might have clicked the button twice and open two same windows. But sometimes both windows might need to be active at the same time, so choose the one that fits your need.