Reputation: 10240
I have a few forms in my program and I obviously have navigation as well. NEXT and BACK buttons. I have the NEXT buttons coded like so:
Private Sub NextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextButton.Click
' Closes current screen and opens the next
Me.Visible = False
Form4.ShowDialog()
End Sub
And the BACK buttons like so:
Private Sub BackButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackButton.Click
' Closes current screen and opens the previous screen
Me.Visible = False
Form2.ShowDialog()
End Sub
As you can tell this is from Form3.
So. I go forward fine, but as soon as I hit back my program doesnt want to run.
what am I doing wrong?
Upvotes: 1
Views: 386
Reputation: 10780
If you must use OpenDialog, here's an example on how this can be accomplished:
Firstly a reference to Form1 needs to be set in the load event. The reference must first be created in Form2 (see below):
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Form2.f1 = Me
End Sub
Private Sub Next_Click(sender As System.Object, e As System.EventArgs) Handles ButNext.Click
Me.Visible = False
Form2.ShowDialog()
End Sub
In Form2, create a Form1 public variable that is set in the load event of Form1. In the Previous button handler, set the reference to Form1's visible property to True instead of calling ShowDialog.
Public Class Form2
Public Property f1 As Form1 ' you can also create a variable instead of a property
Private Sub Previous_Click(sender As System.Object, e As System.EventArgs) Handles ButPrev.Click
f1.Visible = True
Me.Visible = False
End Sub
Private Sub ButNext_Click(sender As System.Object, e As System.EventArgs) Handles ButNext.Click
Me.Visible = False
Form3.ShowDialog()
End Sub
Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load
' repeat process for Form3
Form3.f2 = Me
End Sub
End Class
Repeat this process for all dialog forms in your application.
Upvotes: 1
Reputation: 1263
When showing a form using 'ShowDialog' in VB, you have to evaluate the response and dismiss the form. Just setting Visible to false isn't enough.
See code here: http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
You may just want to show the form, not showDialog it, and there's samples here: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.show.aspx
Hope that helps.
Upvotes: 2