返回第一个对话框
我有一个 WinForm,用作仅带有 OK
和 Cancel
按钮的对话框。所以:
Dim sr As New SlideRangeDialog
Dim dr As Windows.Forms.DialogResult
dr = sr.ShowDialog
我有一个 If/Then 来查看用户是否按下了“确定”。如果他们按下“确定”并且出现验证错误,我需要他们返回对话框并修复它。
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)。
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 to 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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将验证逻辑移至对话框本身或对话框的 Closing 事件处理程序中。后者可能更容易。我的 VB.NET 技能几乎不存在,所以如果这不合时宜,请原谅我:
然后稍后:
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:
Then later:
我认为您需要处理“确定”(或“是,否”等)并在表单本身中取消,并且如果验证失败则不要关闭它。
在用作对话框的表单上,您需要处理单击按钮中的事件。然后在事件处理程序中运行测试来决定是否应关闭表单。如果是这样,那么您可以将 DialogResult 设置为您需要的任何内容并运行 Me.Close()
请参阅此处 举个例子
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
由于它是 VSTO 并且您在使用“正常”方式在屏幕之间进行通信时遇到问题,为什么不尝试使用 pInvoke 来代替:
如果您知道如何获取主题窗口的 hwnds,请使用以下代码:
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:
正如 Preet 所建议的,恕我直言,处理按钮事件中的“Me.Close”语句是最直接的。
您还可以通过将其从按钮事件处理程序中删除并稍后关闭逻辑树中的表单来完成此操作。在我的示例中,我有两种形式。 Form1 有一个用于打开 Form2 的按钮,该按钮有两个按钮,分别是“确定”和“取消”按钮。因此,在第二种形式上,您可以使用以下内容:
您应该记住,在使用 ShowDialog 时,除非您特别要求,否则永远不会释放该形式。一个简单的是/否示例,询问用户是否确实要关闭。
只要在第二个表单中选择“确定”并在下面的消息框中选择“否”,就会递归调用 Button2_Click 子程序以保持显示。我希望这有帮助。
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:
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.
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.