返回第一个对话框

发布于 2024-10-01 06:57:50 字数 1334 浏览 11 评论 0原文

我有一个 WinForm,用作仅带有 OKCancel 按钮的对话框。所以:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

且行且努力 2024-10-08 06:57:50

尝试将验证逻辑移至对话框本身或对话框的 Closing 事件处理程序中。后者可能更容易。我的 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

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
冷默言语 2024-10-08 06:57:50

我认为您需要处理“确定”(或“是,否”等)并在表单本身中取消,并且如果验证失败则不要关闭它。

在用作对话框的表单上,您需要处理单击按钮中的事件。然后在事件处理程序中运行测试来决定是否应关闭表单。如果是这样,那么您可以将 DialogResult 设置为您需要的任何内容并运行 Me.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

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
软糖 2024-10-08 06:57:50

由于它是 VSTO 并且您在使用“正常”方式在屏幕之间进行通信时遇到问题,为什么不尝试使用 pInvoke 来代替:
如果您知道如何获取主题窗口的 hwnds,请使用以下代码:

    [DllImport("user32.dll")]
    public static extern bool SetFocus(IntPtr hwnd); 

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); 
捂风挽笑 2024-10-08 06:57:50

正如 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

只要在第二个表单中选择“确定”并在下面的消息框中选择“否”,就会递归调用 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文