C Make a Form Visible Again
| Purchase and download the full PDF and ePub versions of this Visual C# eBook for only $9.99 |
When developing a Windows application using C# it is a fairly condom bet to presume that it will consist of multiple forms (otherwise known every bit windows). It is unlikely, even so, that all of those forms will demand to be displayed as soon as the application starts upward. In fact, it is more likely that most of the forms will remain subconscious until the user performs some action that requires a grade to be displayed.
In this chapter nosotros will embrace the topic of hiding and showing forms when developing applications in C#.
Contents
Creating a C# Application Containing Multiple Forms
Before we can look at hiding and showing Forms we beginning need to create an application in Visual Studio which contains more than one class. Begin by starting Visual Studio and creating a new Windows Form Application project chosen CSharpShowForm.
Visual Studio will prime the new project with a single grade. Click on the Form to select it and, using the Properties panel modify the Proper name of the class to mainForm. Next nosotros 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:
The Add together New Item window allows new items of diverse types to be added to the project. For this case we merely need to add a new Windows Class to our application. With Windows Form selected in the window change the proper noun of the form to subForm.cs and click on Add. Visual Studio will now display an additional tab containing the second form:
Switch between the two forms by clicking on the corresponding tabs.
At present 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 accept created an application with 2 forms the next footstep is provide a mechanism for hiding and showing subForm. Before doing that, however, we first need to understand the difference betwixt modal and not-modal windows.
Understanding Modal and Non-modal Forms
A Windows class 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 grade is modal, every bit shortly 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 chore before proceeding to another role of the application. In the following sections we will embrace the cosmos of both modal and non-modal forms in C#.
Writing C# Code to Display a Not-Modal Class
Nosotros are going to utilize the push control on mainForm to brandish subForm when information technology is clicked by the user. To practice this, double click on the button control to display the Click event procedure.
Earlier nosotros can phone call whatsoever methods on the subForm we first need to instantiate it as an object. To do so nosotros simply use the new argument to create a new object from the subForm form:
private void button1_Click(object sender, EventArgs e) { subForm myNewForm = new subForm(); } In this event procedure we want to call the Show() method of the myNewForm object we take instantiated from the subForm class to get in display. To attain this, modify the Click event handler as follows:
private void button1_Click(object sender, EventArgs east) { subForm myNewForm = new subForm(); myNewForm.Bear witness(); } To test this code press F5 to compile and run the application. When it appears click on the button in the master course and the sub form volition announced. Y'all volition find that, since this is a not-modal form, yous can still collaborate with the main form while the sub-form is visible (i.eastward you tin click on the button in the main course).
Close the running application.
Some other way to hide and show a form is to gear up the Visible property of the grade object to either true or false. For example:
private void button1_Click(object sender, EventArgs e) { subForm myNewForm = new subForm(); myNewForm.Visible = true; } Writing C# Lawmaking to Brandish a Modal Form
We will now modify the outcome procedure for the push to create a modal form. To practise so we need to phone call the ShowDialog() method of the subForm. Modify the Click event procedure of the mainForm button as follows:
individual void button1_Click(object sender, EventArgs e) { subForm myNewForm = new subForm(); myNewForm.ShowDialog(); } Press F5 once once again to build and run the awarding. After pressing the button in the main class to display the sub course you will detect that the main course is inactive as long as the sub course is displayed. Until the sub grade is dismissed this will remain the case.
Close the running application.
Hiding Forms in C#
In that location are 2 ways to make a grade disappear from the screen. I way is to Hide the form and the other is to Shut the form. When a form is hidden, the form and all its properties and settings still be in retentivity. In other words, the form notwithstanding exists, it is just not visible. When a course is closed, the form is physically deleted from retentiveness and volition need to be completely recreated earlier it can be displayed once again.
To hibernate a form it is necessary to call the Hibernate() method of the form to exist hidden. Using our instance, we volition wire up the button on the subForm to shut the form. Click on the tab for the second class (the subForm) in your design and double click on the button control to display the Click event process.
One very important point to note here is that the push button control is going to hide its own Form. In this case, the event procedure tin reference this instead of referencing the object by name. With this in mind, alter the procedure as follows:
private void button1_Click(object sender, EventArgs e) { this.Hide(); } Printing F5 to build and run the application. Click on the button in the main form to brandish the sub class. Now, when you press the button in the sub form, the form will be hidden.
Closing Forms in C#
Equally mentioned in the previous section, in society to remove a form both from the display, and from memory, it is necessary to Close rather than Hide information technology. 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 demand to use this instead of subForm when calling the Close() method:
private void button1_Click(object sender, EventArgs eastward) { this.Close(); } When the subForm button is pressed the form will exist closed and removed from memory.
| Purchase and download the full PDF and ePub versions of this Visual C# eBook for just $9.99 |
harriscrinver1946.blogspot.com
Source: https://www.techotopia.com/index.php/Hiding_and_Showing_Forms_in_C_Sharp
0 Response to "C Make a Form Visible Again"
Post a Comment