Multiple forms, with mutiple text boxes

ravoll

Well-known member
Joined
Aug 26, 2010
Messages
50
Programming Experience
Beginner
HI all,
Appreciate the help I've recieved so far in this forum.
Am currently piecing my programm together and working on structure.
My plans will have me running multiple forms,and my question is this.

When I have ,say ,on form1, a "TextBox1" and on another form a TextBox with the same name,will I have conflicts when I access data across forms .
Should I be taking care to make sure textboxes and buttons names aren't being repeated,or will VB know what I want?
 
If you're saying 2 forms Form1 and Form2 and if each from contains a Textbox1, then the way you access or change data to that textbox is by identifying it as Form1.Textbox1 and Form2.Textbox2. This answers your question right?
 
Thanks ,
Thats what i was thinking,but my tutorials say that no longer works in VB2008.Have it figured out.

MY next problem:
Have three forms.Form1 is like an options page.Depending upon what I choose ,I either move to form 2 or 3.I would like to have form 1 closed when I move to another form.When I'm done on form2 or 3 and click finished or cancel or close,whatever,I want to close said form and return to form 1.Already have code that only allows me to open forms one at a time.
Also when I have form 2 or 3 open and just close it with the red "x" (Upper right),and try to open it again from form1 I get errors and crash.
Possibilities?
 
Tried

Me.Close
Form2.Show

Does not open form2, only closes form1 and shuts down debug mode.

I want it to work so.
I have 3 forms,1,2,3,
Form 1 is a type of options form with 2 buttons .one for form2 and one for form3.
when I click on any button, the appropriate form opens no problem.Since form 1 is still open behind forms 1 or 2,I have written code that cancels the buttons so no one can click the buttons on form1 until form 2 or 3 are closed.When form 2or3 is closed the the buttons on form1 are active again.But this is not really what I want.


When I choose on form1 for example,to open form2.I want form2 to open and form1 to close.When I'm done with form2 I want to close it and form1 will re-open.
 
Got It

When forms2 or 3 open,they activate "Form1.Hide()" and when I close them form1 comes back.

Help me to understand:
When I open forms 2/3 and say "Form1.close()" it shuts down the whole programm.
Could this be because of the code to open the other forms being on form1,and when I close it,I also shut off the code that's keeping the others open?


Now I have the problem that when I close form2 ,form1 is visable, but I can't reopen form2 any more.I get the error message:

"Cannot access a disposed object. Object name:'Form2'"
 
Last edited:
I think this is what you want.

You can not close form as thats the thread from where you open form2 and 3. If you close form1, you simply exit the whole application!
So instead you should (as you have noticed) hide form1 and form2 or form3 will appear. When done, you show form1 from form2 and form3. This you can do by hooking up FormClosing event of form2 and form3 as shown below.

Form1:
VB.NET:
Public Class Form1

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

        If RadioButton1.Checked Then
            Form2.Show()
            Me.Visible = False
        ElseIf RadioButton2.Checked Then
            Form3.Show()
            Me.Visible = False
        End If

    End Sub
End Class

form2:
VB.NET:
Public Class Form2

    Private Sub Form2_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Form1.Visible = True
    End Sub
End Class

form3:
VB.NET:
Public Class Form3

    Private Sub Form3_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Form1.Visible = True
    End Sub
End Class
 
When I open forms 2/3 and say "Form1.close()" it shuts down the whole programm.
You can select shutdown mode in project properties; 'when startup form closes' or 'when last form closes'.
 
Thanks but I still have the same problem, that when I close form2 ,form1 is visable, but I can't reopen form2 any more.I get the error message:

"Cannot access a disposed object. Object name:'Form2'"

Here is the code in form1 that opens Form2.

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

And on form2 I have this to hide form1:

Private Sub Free_Run_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form1.Hide()
Timer1.Enabled = False
Timer2.Enabled = False
Timer1.Interval = 250 'set the time interval'
Timer2.Interval = 250
End Sub

And here is how I close form2 and make form1 visable:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Close()
Form1.Show()
End Sub

As far as I can tell,it works just like your code.I just cannot reopen form2 after I close it.
 
Got it.
Changed from this:

Public Class Form1
Dim secondform As New Free_Run
Dim ThirdForm As New Dyno_Run

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

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

secondform.Show()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

ThirdForm.Show()
End Sub


End Class

To this:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim secondform As New Free_Run
secondform.Show()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ThirdForm As New Dyno_Run
ThirdForm.Show()
End Sub


End Class

Work great .No more "Cannot access a disposed object" error.
 
If you only intend to use one instance of the Free_Run form just call its default instance;
VB.NET:
Free_Run.Show()
 
Back
Top