-
30-09-2019 - |
题
我有一个winform,它用作对话框 OK
和 Cancel
纽扣。所以:
Dim sr As New SlideRangeDialog
Dim dr As Windows.Forms.DialogResult
dr = sr.ShowDialog
我有一个IF/然后查看用户是否按下确定。如果他们按确定并存在验证错误,我需要他们返回对话框并修复它。
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
我会放什么 Case Windows.Forms.DialogResult.Cancel
带我回到第一个对话框 sr.DialogResult = Windows.Forms.DialogResult.None
似乎不起作用?
我已经尝试再次提高事件子(这是从菜单项中点击),但这与我使用的技术(VSTO RIBBON)不起作用。
解决方案
尝试将验证逻辑移至对话框本身,或将其转移到对话框的关闭事件处理程序中。后者可能更容易。我的VB.NET技能实际上是不存在的,所以如果这不在意,请原谅我:
Dim sr As New SlideRangeDialog
Dim dr As Windows.Forms.DialogResult
AddHandler dr.Closing, AddressOf SlideRangeDialog_Closing
dr = sr.ShowDialog
然后稍后:
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
其他提示
我认为您需要处理确定(或是否等),并以表格本身取消,并且如果验证失败,则不要关闭它。
在用作对话框的表单上,您需要从按钮中处理点击事件。然后在事件处理程序中运行测试,以决定是否应关闭表单。如果这样做,您可以将Dialogresult设置为您需要的任何内容,然后运行我.close()
看 这里 例如
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
由于它的VSTO并且您遇到了使用“正常”手段在屏幕之间进行通信的问题,因此您为什么不尝试使用PINVOKE:鉴于您知道如何获得主题Windows的HWNDS使用此代码:
[DllImport("user32.dll")]
public static extern bool SetFocus(IntPtr hwnd);
正如Preet所建议的那样,从按钮事件中处理“ me.close”陈述是最直接的。
您也可以通过将其从按钮事件处理程序中删除并稍后关闭逻辑树中的表格来做到这一点。在我的示例中,我有两种形式。 Form1具有打开Form2的按钮,一个有两个按钮,带有确定和取消按钮。因此,在第二种形式上,您可以使用以下内容:
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
您应该记住,当使用Showdialog时,除非您专门致电,否则永远不会处理表单。一个简单的是/否示例可以询问用户是否要确定关闭。
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
只要选择确定以第二种形式选择OK而在以下消息框中选择,button2_click sub被递归地调用以保持显示。我希望这有帮助。