Click here to Skip to main content
16,018,802 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Hi,

I am working on window application in VB.Net. I do have a problem with form.show().
The scenario is-

suppose i have form1 with two buttons
button1.text = "one"
button2.text = "one"

I have another form2
i want to open form2 on each button click present on form1.

VB
'click event on button1
Dim obj As New form2(button1.text)
obj.Show()


VB
'click event on button2
Dim obj As New form2(button2.text)
obj.Show()


on form2
VB
Public Sub New(ByVal ReceiveValue As String)
  MyBase.New()
  InitializeComponent()
End Sub


Now every time i click on button1 or button2 a new form2 is open.

I want to open 1 form for button1 and 1 form for button2.
How do i prevent duplicate forms for each button1 and button2 if form is already open.

thanks
Posted

Everytime you create a new instance of the form a new one will open.

The best way to handle the problem is to allow another class (lets just say a controller class) to open both these forms for you. You can keep a track of the number of open forms inside this and control the number you want to be seen open on the screen.
 
Share this answer
 
thanks for the solution..

but i have done with my below solution

VB
For Each CurrentForm As Form In Application.OpenForms
            If CurrentForm.Name = "form2" Then
              Dim Form2Instance As form2 = DirectCast(CurrentForm, form2)
              If Form2Instance.TextBox1.Text = "some text" Then
                Form2Instance.BringToFront()
                Exit Sub
              End If
            End If
          Next

          Dim form As New form2("some text")
          form.Show()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900