In vb.net application , I open the form2 from form1 by btn event with following code.
But if the user minimise it and again open it from the button event, there are 2 forms in the taskbar...
I want the same form2 is opened everytime without multiplication of forms in taskbar.
How can i do that?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Dim frm2 As New Form2
        frm2.Show()

    End Sub

Recommended Answers

All 3 Replies

You are creating a new Form2 every time you click your button.

You need to check if th form does exist and if it does set the window.state to normal. Something like this:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim frmCollection = System.Windows.Forms.Application.OpenForms
        For Each f As Form In frmCollection
            If f.Name = "Form2" Then
                MsgBox("Form exists")
                f.WindowState = FormWindowState.Normal
                Exit Sub
            End If
        Next
        Dim form2 As New Form
        form2.Show()
        form2.Name = "Form2"
         form2.Text = "Form2"
    End Sub

Ok I got it,
it worked fine....

Thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.