Return to the first dialog
I have a WinForm that is used as a Dialog with just OK
and Cancel
buttons. So:
Dim sr As New SlideRangeDialog
Dim dr As Windows.Forms.DialogResult
dr = sr.ShowDialog
I have an If/Then to see if the user pressed OK. If they pressed OK and there is a validation error, I need them to go back to the dialog and fix it.
If dr = Windows.Forms.DialogResult.OK Then
Dim mr As Windows.Forms.DialogResult
mr = MsgBox("Click Yes to fix, No to not fix or Cancel to go " + vbCrLf + _
" back to the dialog to fix.", MsgBoxStyle.YesNoCancel)
Select Case mr
Case Windows.Forms.DialogResult.Yes
''# something
Case Windows.Forms.DialogResult.No
''# something more
Case Windows.Forms.DialogResult.Cancel
''# RIGHT HERE is where I'm having the problem.
''# I just want "Cancel" to return to the first dialog.
sr.DialogResult = Windows.Forms.DialogResult.None
End Select
Else
''#other thing
End If
What would I put in Case Windows.Forms.DialogResult.Cancel
to take me right back to the first dialog as sr.DialogResult = Windows.Forms.DialogResult.None
doesn't seem t开发者_Python百科o be working?
I've tried raising the event sub again (it's a click from a menu item), but this doesn't work with the technology I'm using (VSTO Ribbon).
Try moving your validation logic either into the dialog itself, or into a Closing event handler for the dialog. The latter might be easier. My VB.NET skills are practically non-existent so forgive me if this is off the mark:
Dim sr As New SlideRangeDialog
Dim dr As Windows.Forms.DialogResult
AddHandler dr.Closing, AddressOf SlideRangeDialog_Closing
dr = sr.ShowDialog
Then later:
Public Sub SlideRangeDialog_Closing(Sender As Object, e As CancelEventArgs)
' cast Sender as a SlideRangeDialog and check its
' DialogResult property to see if they clicked OK.
' Your validation goes in here.
' If anything fails validation, set e.Cancel to True and the
' dialog won't close.
End Sub
I think you need to handle the ok (or Yes NO etc) and cancel in the form itself and not close it if the validation fails.
On the Form that is used as a Dialog you need to handle the Click Events from the buttons. Then run a test in the event handler to decide if the form should be closed. If it does then you can set the DialogResult to whatever you need and run Me.Close()
See here for an example
Private Sub OKCmd_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles OKCmd.Click
If Not ____do_your_test_here____ Then
MsgBox("Cannot press OK because of blah blah blah . Try again.", MsgBoxStyle.Exclamation)
Else
Me.DialogResult = DialogResult.OK
Me.Close()
End If
End Sub
Since its VSTO and you are having issues to use "normal" means to communicate between screens, why dont you try to use pInvoke instead: Given you know how to obtain the hwnds of subject windows use this code:
[DllImport("user32.dll")]
public static extern bool SetFocus(IntPtr hwnd);
As Preet suggested it is imho most straightforward to handle the 'Me.Close' statement from the button events.
You can also do this by removing it from the button event handler and closing the form in your logic tree later. In my example I have two forms. Form1 has a button to open Form2 and that one has two buttons with OK and Cancel buttons. So on the second form you could use the following:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Dispose()
End Sub
You should keep in mind that when using the ShowDialog the form is never disposed of unless you specifically call for it. A simple Yes/No example to ask if the user wants to close for sure.
If Form2.ShowDialog = Windows.Forms.DialogResult.OK Then
Select Case MessageBox.Show("Sure to close?", "Warning", MessageBoxButtons.YesNo)
Case Windows.Forms.DialogResult.Yes
Form2.Dispose()
Case Windows.Forms.DialogResult.No
Button2_Click(sender, e)
End Select
End If
The Button2_Click sub is called recursively to keep displaying as long as OK is chosen in the second form and NO in the following Messagebox. I hope this helps.
精彩评论