Pregunta

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?

¿Fue útil?

Solución

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

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top