Redisplaying modeless window.

C

cr113

Suppose you have Button1 on Form1. If the user clicks Button1, I want
to show Form2 modelessly. If Form2 is already open I just want to
redisplay it, not open a new window. Thru trial and error I came up
with the following code. Is there a better way to do this?


Public Class Form1
Dim objForm As Form2

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

If (IsNothing(objForm)) Then
objForm = New Form2
objForm.Show()
Else
If (objForm.Created) Then
objForm.BringToFront()
Else
objForm = New Form2
objForm.Show()
End If
End If

End Sub

End Class
 
T

Terry

I guess it depends on what kind of 'state' the Form2 can be in. What you
have here won't work if the form was hidden or minimized. Have a minimize
button on Form2? Have any other button that causes a Me.Hide?
 
C

cr113

I guess it depends on what kind of 'state' the Form2 can be in. What you
have here won't work if the form was hidden or minimized. Have a minimize
button on Form2? Have any other button that causes a Me.Hide?

Good point. I replaced the BringToFront method with a Dispose and New
Form and it seems to work. Thanks!

Public Class Form1
Dim objForm As Form2

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

If (IsNothing(objForm)) Then
objForm = New Form2
objForm.Show()
Else
If (objForm.Created) Then
objForm.Dispose 'changed from BringToFront
objForm = New Form2
objForm.Show()
Else
objForm = New Form2
objForm.Show()
End If
End If

End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top