Programming in C#

11. Opening a new Window

In a C# Windows Form App, an initial window is created for you in your project called Form1 This window can contain various components from labels, text boxes, buttons and so on.Also, you can change the form's properties in the Properties tab such as Name, Location, Size, Colour and so on.
Additional windows can be easily added by right clicking the solution in the 'Solution Explorer' and select Add, Windows Form ...

Alternatively, you can create a new window programmically by creating an instance of the Windows Form:

System.Windows.Forms.Form myForm = new System.Windows.Forms.Form();

Once the Window is created, you can then programmically set its properties, for example,

myForm.Name = "Form2";
myForm.Location = new Point(50, 50); // Set the location of form by x,y points.
myForm.Size = new Size (100, 200);
myForm.Text = "Form2";
myForm.BackColor = Color.Blue;
myForm.Show();

An existing Form can be called from another form by creating an instance of that form and then use the Show() function to display it. Use the Hide() method to remove the window.

11.1 Form Events

Forms also respond to events when a windows is interacted with suchas when if first opens, it will call the Load event, when it is closed it will call eith the FormClosing or FormClosed events. Other events include resize, click, DoubleClick, MouseClick, SizeChanged, and others. Each even can have a method associated with it with two parameters: object sender and EventArgs e . These are useful when you want to use the same function for multiple controls. The sender parameter will have the control's object name of the control that was clicked on. Then EventArgs ar used for any additional events that have data.

Sometimes when you double-click a form or control, a default event function is created for you in code ready for you to fill in. For example, if you double click on the first form, it will generate the following code for you:

private void Form1_Load(object sender, EventArgs e) Handles MyBase.Load
{
// Do something here after the form has loaded
}

You can set the events and specify the name of the method to call in the Form's Properties. Click on the lightning (Events) icon at the bottom right to list the available events for the form.
Form events include: Click, DoubleClick, MouseCaptureChanged, MouseClick, MouseDoubleClick, ResizeBegin, ResizeEnd, Scholl, Paint, DragDrop, Enter, Leave, KeyPress etc.

11.2 Adding Controls to a Form

Forms can contain specific types of controls for the user to interact with including Buttons, labels, text boxes, pictures, list boxes, date pickers, progress bars, radio buttons and so on.
To add a control, select it from the Toolbox list on the left and drag and drop the control on to the form. You can resize it in the gui by select the edges of the control and drag it outwords to
resize it. To make other changes, you can change the attributes in the Properties list at the bottom right including control name, text, colour, font, and so on.

Alternatively you can programmically add new control and and set its properties, for example, a text box with a label:

Label myLabel = new System.Windows.Forms.Label();
myLabel.Text = "Name:";
myLabel.Top = 100;
myLabel.Left = 50;

TextBox nameText = new System.Windows.Forms.TextBox();
nameText.Top = 100;
nameText.Left = 150;

When the program runs, you can read what the user enters by reading the nameText.Text property.

You can programmically, set up events for controls, esp. if controls are similar and call the same routine. Use the SystemEventHandler() function to add an event
to a form (or other control). Notice the use of += operator to add an Event handler to the control Event.

control.eventName += new SystemEventHandler(this.methodName);
e.g.
Button1.Click += new SystemEventHandler(this.Button1_Click);