1

VB2010. I must be missing something because I couldn't find a solution after searching for an hour. What I want to do is simple. In my app I want to display a modeless form so that it is floating while the user can still interact with the main form.

dim f as New frmColors
f.Show(Me)

But I only want one instance of the form at any time. So how can I prevent more than once instance being displayed, and if there is one instance then just give it focus?

sinDizzy
  • 1,300
  • 7
  • 28
  • 60

2 Answers2

2

Does something like this work for you, if the form is already visible you can not do a Show, you can just do a BringToFront, also you can check to see if the Form has been disposed so you can New up another one.

Public Class Form1
    Dim f As New frmColors

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If f.IsDisposed Then f = New frmColors 'To handle user closing form
        CheckForm(f)
    End Sub

    Private Sub CheckForm(frm As Form)
        If frm.Visible Then
            frm.BringToFront()
        Else
            frm.Show(Me)
        End If
    End Sub    
End Class
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • I was toying with a method kind of like your CheckForm. Actually I would display the form in hidden mode every time and then just make it visible/invisible. Your approach makes much more sense and uses resources much better. thanks! – sinDizzy Nov 14 '12 at 16:07
  • This doesn't seem to work. Visible can be false if the dialog isn't in focus. So in this case, it will create a 2nd window. – http203 Sep 20 '13 at 14:13
  • @http203 I disagree you are not creating another window if it is not visible you are showing the existing window. – Mark Hall Sep 20 '13 at 14:28
  • @MarkHall What I mean is, the Visible state is false when the window isn't in focus so the else condition calling frm.Show(Me) is executed, thus creating a new instance. In my case, the only way to show the dialog is to leave the focus (if already open) and to click the menu button.. so Visible is always false. – http203 Sep 20 '13 at 14:55
  • @http203 It should not create a new instance because you are not newing up the form it should just set focus to your existing form. It should just treat it as if the form was hidden. It is still the same form. – Mark Hall Sep 20 '13 at 15:09
  • @http203 I waited to get to a computer to verify my code examples functionality. It works as posted, if you click away from the secondary form my code brings it forward. I am not sure what you are doing that is different, but the fact that you are creating a second form makes me think that you are using the form's class name instead of an object of the Form class. I would have appreciated a chance to respond before you down voted the answer though, since it was the accepted answer and about a year old. In my opinion the problem is with something in your code not with my example. – Mark Hall Sep 20 '13 at 22:53
  • @MarkHall You're right, I apologize. It's a bit complicated to explain but I had a service class that handled creating the dialog so the bug was indeed on my side. Your code works. I tried undoing the downvote but it says I'm not allowed because it's now locked in, unless the answer gets edited. Sorry! – http203 Sep 23 '13 at 13:36
0

Make your form follow the singleton pattern. I can't vouch for this sample, but from the text it appears to do what you want.

jzonthemtn
  • 3,344
  • 1
  • 21
  • 30