Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open the second form?

I have Form1 and Form2 in my project. Form2 is just a form with settings for Form1. What is the command to open the Form2 from the Form1 and also what's the command to close it please?

like image 551
Nasgharet Avatar asked Apr 19 '11 14:04

Nasgharet


People also ask

How do I open a second form in Windows?

Now go to Solution Explorer and select your project and right-click on it and select a new Windows Forms form and provide the name for it as form2. Now click the submit button and write the code. When you click on the submit button a new form will be opened named form2.


2 Answers

You need to handle an event on Form1 that is raised as a result of user interaction. For example, if you have a "Settings" button that the user clicks in order to show the settings form (Form2), you should handle the Click event for that button:

private void settingsButton_Click(Object sender, EventArgs e) {     // Create a new instance of the Form2 class     Form2 settingsForm = new Form2();      // Show the settings form     settingsForm.Show(); } 

In addition to the Show method, you could also choose to use the ShowDialog method. The difference is that the latter shows the form as a modal dialog, meaning that the user cannot interact with the other forms in your application until they close the modal form. This is the same way that a message box works. The ShowDialog method also returns a value indicating how the form was closed.


When the user closes the settings form (by clicking the "X" in the title bar, for example), Windows will automatically take care of closing it.

If you want to close it yourself before the user asks to close it, you can call the form's Close method:

this.Close(); 
like image 103
Cody Gray Avatar answered Sep 19 '22 06:09

Cody Gray


//To open the form  Form2 form2 = new Form2();  form2.Show(); // And to close form2.Close(); 

Hope this helps

like image 37
cush Avatar answered Sep 18 '22 06:09

cush