34,333
edits
Changes
New page: When developing a Windows application using C# it is a fairly safe bet to assume that it will consist of multiple forms (otherwise known as windows). It is unlikely, however, that all of t...
When developing a Windows application using C# it is a fairly safe bet to assume that it will consist of multiple forms (otherwise known as windows). It is unlikely, however, that all of those forms will need to be displayed as soon as the application starts up. In fact, it is more likely that most of the forms will remain hidden until the user performs some action that requires a form to be displayed.
In this chapter we will cover the topic of hiding and showing forms when developing applications in C#.
== Creating a Visual Basic Application Containing Multiple Forms ==
Before we can look at hiding and showing Forms we first need to create an application in Visual Studio which contains more than one form. Begin by starting Visual Studio and creating a new Windows Application project called CSharpShowForm.
Visual Studio will prime the new project with a single form. Click on the Form to select it and, using the ''Properties'' panel change the ''Name'' of the form to ''mainForm''. Next we need to add a second form. To do this, click on the ''Add New Item'' in the Visual Studio toolbar to invoke the ''Add New Item'' window:
[[Image:visual_studio_add_new_item.jpg]]
The ''Add New Item'' window allows new items of various types to be added to the project. For this example we just need to add a new Windows Form to the our application. With ''Windows Form'' selected in the window click on ''Add''. Visual Studio will now display an additional tab containing the second form:
[[Image:visual_studio_two_forms.jpg]]
Switch between the two forms by clicking on the respective tabs. With Form2 visible click on the form and change the name of the form in the ''Properties'' panel to ''subForm''.
Now that you have created two forms, add a Button to each form by displaying the Toolbox and dragging a Button onto each form.
Now that we have created an application with two forms the next step is provide a mechanism for hiding and showing ''subForm''. Before doing that, however, we first need to understand the difference between modal and non-modal windows.
== Understanding Modal and Non-modal Forms ==
A Windows form can be displayed in one of two modes, modal and non-modal. When a form is non-modal it means that other forms in the other forms in the application remain accessible to the user (in that they can still click on controls or use the keyboard in other forms).
When a form is modal, as soon as it is displayed all other forms in the application are disabled until the modal dialog is dismissed by the user. Modal forms are typically used when the user is required to complete a task before proceeding to another part of the application. In the following sections we will cover the creation of of both modal and non-modal forms in Visual Basic.
== Writing Visual Basic Code to Display a Non-Modal Form ==
We are going to use the button control on ''mainForm'' to display ''subForm'' when it is clicked by the user. To do this double click on the button control to display the ''Click'' event procedure. In this event procedure we want to call the ''Show()'' method of the ''subForm'' to make it display. To achieve this, modify the Click event handler as follows:
<pre>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
subForm.Show()
End Sub
</pre>
To test this code press F5 to compile and run the application. When it appears click on the button in the main form and the sub form will appear. You will notice, since this is a non-modal form, that you can still interact with the main form while the sub-form is visible (i.e you can click on the button in the main form).
Close the running application.
== Writing Visual Basic Code to Display a Non-Modal Form ==
We will now modify the event procedure for the button to create a modal form. To do so we need to call the ''ShowDialog()'' method of the ''subForm''. Modify the ''Click'' event procedure of the ''mainForm'' button as follows:
<pre>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
subForm.ShowDialog()
End Sub
</pre>
Press '''F5''' once again to build and run the application. After pressing the button in the main form to display the sub form you will find that the main form is inactive as long as the sub form is displayed. Until the sub form is dismissed this will remain the case.
Close the running application.
== Hiding Forms in Visual Basic ==
There are two ways to make a form disappear from the screen. One way is to ''Hide'' the form and the other is to ''Close'' the form. When a form is hidden, the form and all its properties and settings still exist in memory. In other words, the form still exists, it is just not visible. When an form is closed, the form is physically deleted from memory and will need to be completely recreated before it can be displayed again.
To hide a form it is necessary to call the ''Hide()'' method of the form to be hidden. Using our example, we will wire up the button on the ''subForm'' to close the form. Click on the tab for the second form (the ''subForm'') in your design and double click on the button control to display the ''Click'' event procedure.
One very important point to note here is that the button control is going to hide its own Form. In this case, the event procedure must reference ''Me'' instead of referencing ''subForm'' by name. With this in mind, modify the procedure as follows:
<pre>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Me.Hide()
End Sub
</pre>
Press F5 to build and run the application. Click on the button in the main form to display the sub form. Now, when you press the button in the sub form, the form will be hidden.
== Closing Forms in Visual Basic ==
As mentioned in the previous section, in order to remove a form both from the display, and form memory, it is necessary to ''Close'' rather than ''Hide'' it. In Visual Studio double click, once again, the button in ''subForm'' to view the ''Click'' event procedure. Once again, because the button is in the form we are closing we need to use ''Me'' instead of ''subForm'' when calling the ''Close()'' method:
<pre>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Me.Close()
End Sub
</pre>
When the ''subForm'' button is pressed the form will be closed and removed from memory.
In this chapter we will cover the topic of hiding and showing forms when developing applications in C#.
== Creating a Visual Basic Application Containing Multiple Forms ==
Before we can look at hiding and showing Forms we first need to create an application in Visual Studio which contains more than one form. Begin by starting Visual Studio and creating a new Windows Application project called CSharpShowForm.
Visual Studio will prime the new project with a single form. Click on the Form to select it and, using the ''Properties'' panel change the ''Name'' of the form to ''mainForm''. Next we need to add a second form. To do this, click on the ''Add New Item'' in the Visual Studio toolbar to invoke the ''Add New Item'' window:
[[Image:visual_studio_add_new_item.jpg]]
The ''Add New Item'' window allows new items of various types to be added to the project. For this example we just need to add a new Windows Form to the our application. With ''Windows Form'' selected in the window click on ''Add''. Visual Studio will now display an additional tab containing the second form:
[[Image:visual_studio_two_forms.jpg]]
Switch between the two forms by clicking on the respective tabs. With Form2 visible click on the form and change the name of the form in the ''Properties'' panel to ''subForm''.
Now that you have created two forms, add a Button to each form by displaying the Toolbox and dragging a Button onto each form.
Now that we have created an application with two forms the next step is provide a mechanism for hiding and showing ''subForm''. Before doing that, however, we first need to understand the difference between modal and non-modal windows.
== Understanding Modal and Non-modal Forms ==
A Windows form can be displayed in one of two modes, modal and non-modal. When a form is non-modal it means that other forms in the other forms in the application remain accessible to the user (in that they can still click on controls or use the keyboard in other forms).
When a form is modal, as soon as it is displayed all other forms in the application are disabled until the modal dialog is dismissed by the user. Modal forms are typically used when the user is required to complete a task before proceeding to another part of the application. In the following sections we will cover the creation of of both modal and non-modal forms in Visual Basic.
== Writing Visual Basic Code to Display a Non-Modal Form ==
We are going to use the button control on ''mainForm'' to display ''subForm'' when it is clicked by the user. To do this double click on the button control to display the ''Click'' event procedure. In this event procedure we want to call the ''Show()'' method of the ''subForm'' to make it display. To achieve this, modify the Click event handler as follows:
<pre>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
subForm.Show()
End Sub
</pre>
To test this code press F5 to compile and run the application. When it appears click on the button in the main form and the sub form will appear. You will notice, since this is a non-modal form, that you can still interact with the main form while the sub-form is visible (i.e you can click on the button in the main form).
Close the running application.
== Writing Visual Basic Code to Display a Non-Modal Form ==
We will now modify the event procedure for the button to create a modal form. To do so we need to call the ''ShowDialog()'' method of the ''subForm''. Modify the ''Click'' event procedure of the ''mainForm'' button as follows:
<pre>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
subForm.ShowDialog()
End Sub
</pre>
Press '''F5''' once again to build and run the application. After pressing the button in the main form to display the sub form you will find that the main form is inactive as long as the sub form is displayed. Until the sub form is dismissed this will remain the case.
Close the running application.
== Hiding Forms in Visual Basic ==
There are two ways to make a form disappear from the screen. One way is to ''Hide'' the form and the other is to ''Close'' the form. When a form is hidden, the form and all its properties and settings still exist in memory. In other words, the form still exists, it is just not visible. When an form is closed, the form is physically deleted from memory and will need to be completely recreated before it can be displayed again.
To hide a form it is necessary to call the ''Hide()'' method of the form to be hidden. Using our example, we will wire up the button on the ''subForm'' to close the form. Click on the tab for the second form (the ''subForm'') in your design and double click on the button control to display the ''Click'' event procedure.
One very important point to note here is that the button control is going to hide its own Form. In this case, the event procedure must reference ''Me'' instead of referencing ''subForm'' by name. With this in mind, modify the procedure as follows:
<pre>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Me.Hide()
End Sub
</pre>
Press F5 to build and run the application. Click on the button in the main form to display the sub form. Now, when you press the button in the sub form, the form will be hidden.
== Closing Forms in Visual Basic ==
As mentioned in the previous section, in order to remove a form both from the display, and form memory, it is necessary to ''Close'' rather than ''Hide'' it. In Visual Studio double click, once again, the button in ''subForm'' to view the ''Click'' event procedure. Once again, because the button is in the form we are closing we need to use ''Me'' instead of ''subForm'' when calling the ''Close()'' method:
<pre>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Me.Close()
End Sub
</pre>
When the ''subForm'' button is pressed the form will be closed and removed from memory.


