nikos_22

I have 2 forms. from the button of form1 i display the form2
Form2 f= new Form2();
f.Show();
How can i check if the form2 is dislayed (or has been closed) , so i dont display twice the form2 when i click again the button.


Re: Windows Forms General form method Show()

Gavin Jin - MSFT

I think there are many metheds to do this.first,you can use "f.ShowDialog()" to make the form2 show only once.I think this is the easist way.

2.you can dim a countnumber, the countnumber will -1 with the button clicked,if countnumber<0,the button.visible= false

.......so on






Re: Windows Forms General form method Show()

Moayad Mardini

You can use ShowDialog() instead, because it returns what did the user do with the another form, for example :

Form2 frm = new Form2();

if (frm.ShowDialog() == DialogResult.OK) {

MessageBox.Show(@"The user clicked OK");

}

else {

MessageBox.Show(@"The user closed the form");

}

Just put a button in the second form and set its DialogResult property to OK.





Re: Windows Forms General form method Show()

nikos_22

In solution 2 i think the form1 must be informed if the form2 is shown or not.

Is any way to know that the form2 (which is displayed with Show() ) has closed.





Re: Windows Forms General form method Show()

nikos_22

Thank you but i must do it with Show() (for other reasons).

Is any way to do it





Re: Windows Forms General form method Show()

Moayad Mardini

Change a public variable when the form is Disposed.





Re: Windows Forms General form method Show()

L.Pettett

I have been searching for a solution to this problem too and wasn't happy with all of the work-arounds on the internet. Public Vairables are bad, disabling buttons is no good for some and keeping and integer counter is complicating a simple problem...

Okay,

So a good clean solution is to use Application.OpenForms()...

For Each shownForm as Form in Application.OpenForms
if (shownForm.Name = frmTheScreenIAmCheckingFor.Name) then
'Found you! the pesky is shown up already :-)
'Do something here
end if
Next

Cheers All,
Luc Pettett




Re: Windows Forms General form method Show()

Gavin Jin - MSFT

Public Class Form1

Public frm2Bool As Boolean

Public frm2 As Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If frm2Bool = False Then

Form2.Show()

frm2Bool = True

Else

MsgBox("form2 has showed")

End If

End Sub

End Class