After Unhiding a Form How to Load It Again in C


Buy and download the full PDF and ePub versions of this Visual C# eBook for just $9.99


When developing a Windows awarding using C# it is a fairly safe bet to assume that it volition consist of multiple forms (otherwise known as windows). Information technology is unlikely, however, that all of those forms will need to be displayed as shortly as the application starts up. In fact, information technology is more probable that most of the forms will remain subconscious until the user performs some action that requires a form to exist displayed.

In this chapter we volition cover the topic of hiding and showing forms when developing applications in C#.

Contents


Creating a C# Application Containing Multiple Forms

Before nosotros can expect at hiding and showing Forms we first demand to create an application in Visual Studio which contains more than i class. Begin by starting Visual Studio and creating a new Windows Form Application project called CSharpShowForm.

Visual Studio will prime the new project with a unmarried form. Click on the Course to select it and, using the Properties panel modify the Name of the form to mainForm. Adjacent we need to add a second form. To exercise this, click on the Add New Item in the Visual Studio toolbar to invoke the Add New Item window:

Visual studio add new item.jpg

The Add New Item window allows new items of various types to exist added to the project. For this example we just need to add a new Windows Form to our application. With Windows Form selected in the window change the name of the form to subForm.cs and click on Add. Visual Studio will now display an additional tab containing the 2nd form:

C sharp two forms.jpg

Switch between the two forms past clicking on the respective tabs.

Now that y'all have created two forms, add a Button to each form by displaying the Toolbox and dragging a Push button onto each course.

Now that we have created an application with two forms the side by side step is provide a machinery for hiding and showing subForm. Before doing that, still, we start need to empathize the difference between modal and non-modal windows.

Understanding Modal and Non-modal Forms

A Windows course tin can be displayed in one of 2 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 utilize the keyboard in other forms).

When a form is modal, as before long equally it is displayed all other forms in the application are disabled until the modal dialog is dismissed past the user. Modal forms are typically used when the user is required to complete a task before proceeding to another office of the application. In the post-obit sections we will cover the creation of both modal and not-modal forms in C#.


Writing C# Code to Display a Not-Modal Form

We are going to use the button command on mainForm to display subForm when it is clicked by the user. To do this, double click on the push command to display the Click result procedure.

Earlier we can telephone call any methods on the subForm we kickoff demand to instantiate it as an object. To do and then we simply use the new statement to create a new object from the subForm class:

          private void button1_Click(object sender, EventArgs e)         {             subForm myNewForm = new subForm();         }        

In this consequence process we want to call the Show() method of the myNewForm object we have instantiated from the subForm class to make it display. To reach this, modify the Click result handler as follows:

          individual void button1_Click(object sender, EventArgs eastward)         {             subForm myNewForm = new subForm();              myNewForm.Show();         }        

To examination this code press F5 to compile and run the awarding. When information technology appears click on the button in the main class and the sub form will appear. You will observe that, since this is a non-modal class, yous can still interact with the main class while the sub-form is visible (i.due east yous can click on the button in the primary form).

Shut the running application.

Some other way to hide and evidence a form is to set the Visible property of the course object to either true or false. For case:

          individual void button1_Click(object sender, EventArgs due east)         {             subForm myNewForm = new subForm();              myNewForm.Visible = true;         }        

Writing C# Lawmaking to Brandish a Modal Form

Nosotros volition at present modify the result procedure for the button to create a modal form. To do and so nosotros 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 over again to build and run the application. Later pressing the push in the principal course to display the sub form you lot will detect that the main course is inactive as long equally the sub form is displayed. Until the sub form is dismissed this volition remain the example.

Close the running application.

Hiding Forms in C#

There are two ways to brand a course disappear from the screen. One style is to Hide the class and the other is to Close the form. When a form is hidden, the form and all its properties and settings still be in memory. In other words, the form still exists, information technology is just non visible. When a form is airtight, the class is physically deleted from retention and will need to be completely recreated before it can exist displayed again.

To hide a grade it is necessary to call the Hide() method of the form to be subconscious. Using our example, nosotros will wire upwards the button on the subForm to close the grade. Click on the tab for the second class (the subForm) in your blueprint and double click on the button command to display the Click event process.

One very of import point to note here is that the push control is going to hibernate its ain Form. In this case, the event procedure can reference this instead of referencing the object by name. With this in mind, change the procedure as follows:

          individual void button1_Click(object sender, EventArgs e)         {             this.Hide();         }        

Printing F5 to build and run the application. Click on the button in the master form to display the sub course. At present, when you press the button in the sub form, the grade will be hidden.

Closing Forms in C#

As mentioned in the previous section, in gild to remove a course both from the display, and from memory, it is necessary to Close rather than Hide it. In Visual Studio double click, once once again, the button in subForm to view the Click result procedure. In one case once more, because the push is in the form we are closing we need to employ this instead of subForm when calling the Shut() method:

          private void button1_Click(object sender, EventArgs due east)         {             this.Close();         }        

When the subForm push is pressed the form will exist closed and removed from memory.

Buy and download the total PDF and ePub versions of this Visual C# eBook for only $9.99


hendersonconifice.blogspot.com

Source: https://www.techotopia.com/index.php/Hiding_and_Showing_Forms_in_C_Sharp

0 Response to "After Unhiding a Form How to Load It Again in C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel