Form Navigation

evad4682

Well-known member
Joined
Nov 7, 2005
Messages
55
Programming Experience
Beginner
I have a windows project with two forms in it. On form1 one I have a linkbutton to be able to open form2. When form2 is opened I want to hide form1 and then show form2. On form2 there are some text boxes asking for information and a submit button. When the submit button is clicked I do some validation of the text in the text boxes. This is where the problem comes in - If I find an issue with the text in a text box I flag the text box in question with an error provider and pop up a message box explaining what is wrong. As soon as I click ok on the message box, form2 goes away and form1 comes back. I need form2 to stay active so that it can be filled out properly. How do I accomplish this?

Here is my code for opening form2
VB.NET:
Dim nf As New frmNotFound

 Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Me.Hide()
        nf.FromInformation(user, asset, compname, messid, ip2)
        nf.ShowDialog(Me)
        Me.Show()
    End Sub

My text validation for each text box is handled by a function. If the function returns pass, I update my database with some information. If it returns fail, then the sub routine ends. It’s when the sub routine ends that form2 goes away and form1 comes back. Please help, thanks.
 
My guess is that the problem with Form2 closing is on Form2, not Form1.

When the user clicks the button on Form2, instead of always closing it, you should only close it when the data has passed the validation function.

IE if the function returns True, then close the form, which means if it returns False then you should leave it open.
 
Nevermind, I tried it a bit of a different way.

On form1
VB.NET:
Dim nf As New frmNotFound   

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As 
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Me.Hide()
        Me.AddOwnedForm(nf)
        nf.FromInformation(user, asset, compname, messid, ip2)
        nf.Show()
    End Sub

On form2 to go back to form1
VB.NET:
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As 
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Dim f As Form1 = Me.Owner
        Me.Hide()
        f.Show()
    End Sub

Thanks for the help Juggalo. I believe my issue was calling form2 with showdialog(me) with me.show following that line (see first post). I think.
 
Nevermind, I tried it a bit of a different way.

On form1
VB.NET:
Dim nf As New frmNotFound   

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As 
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Me.Hide()
        Me.AddOwnedForm(nf)
        nf.FromInformation(user, asset, compname, messid, ip2)
        nf.Show()
    End Sub

On form2 to go back to form1
VB.NET:
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As 
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Dim f As Form1 = Me.Owner
        Me.Hide()
        f.Show()
    End Sub

Thanks for the help Juggalo. I believe my issue was calling form2 with showdialog(me) with me.show following that line (see first post). I think.

That would cause Form2 to open twice, not close it even if the data does not pass validation.

On Form1, I would have this code:
VB.NET:
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As 
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
    Me.Hide()
    Me.AddOwnedForm(nf)
    nf.FromInformation(user, asset, compname, messid, ip2)
    nf.ShowDialog()
End Sub

And on Form2, I would have this code:
VB.NET:
Private m_PassesValidation As Boolean = False

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As 
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
    If m_PassesValidation = True Then
      Dim f As Form1 = Me.Owner
      Me.Hide()
      f.Show()
    End If
End Sub
And of course when you validate the data be sure to set the m_PassesValidation to true right after you save the data in the database (so the form can close since it is valid data)
 
Back
Top