Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I open multiple forms in C#?

Tags:

c#

forms

datagrid

I have a DataGridView with data loaded from a database. Each row has a 'View' button and when I click on it another form is opened with specific information but the main form with the DataGridView is still opened. I do this with this code:

FormView fr = new FormView(id);
fr.ShowDialog();

The problem is that I can't open several FormViews at the same time because the focus is still in the first FormView opened. How can I do this?

like image 470
Barbara PM Avatar asked Aug 21 '12 10:08

Barbara PM


2 Answers

when you use ShowDialog on a form it shows and waits for a response of type DialogeResult, but when you just need to show a form and no response is needed you can simply call form.Show()
here is the code :

When you want a result:

Form f = new Form(); 
DialogResult res = f.ShowDialog();  

//code stops here until you return something of type DialogResult  
if (res == System.Windows.Forms.DialogResult.OK)  
    I_Will_Run_Just_When_DialogReult_Returned_Is_OK();  

when you only want to show the form:

f.Show();  
I_Will_Run_Anyway_Right_After_Showing_Form(); 
like image 69
Mahdi Tahsildari Avatar answered Nov 09 '22 22:11

Mahdi Tahsildari


ShowDialog opens a modal form which means that the ShowDialog method won't return until the user has closed the form.

Use fr.Show method instead.

like image 45
logicnp Avatar answered Nov 09 '22 22:11

logicnp