如何在VB.Net中关闭对话框时强制执行操作?

如何在VB.Net中关闭对话框时强制执行操作?,vb.net,user-interface,Vb.net,User Interface,我想知道当SaveFileDialog对话框关闭时,如何强制我的主gui更新。我尝试过Main.LostFocus,Main.GotFocus,Main.Enter,Main.MouseEnter,Main.MouseLeave,和Main.MouseMove,但无论我尝试什么函数,我都无法得到我想要的结果 单击图片时,将打开该对话框。单击图片时图片会发生变化,调用图标\u new.MouseUp时图片会再次发生变化。问题是,在关闭对话框之前,它的操作是正确的。在这一点上,图片会变回鼠标悬停时的

我想知道当
SaveFileDialog
对话框关闭时,如何强制我的主gui更新。我尝试过
Main.LostFocus
Main.GotFocus
Main.Enter
Main.MouseEnter
Main.MouseLeave
,和
Main.MouseMove
,但无论我尝试什么函数,我都无法得到我想要的结果

单击图片时,将打开该对话框。单击图片时图片会发生变化,调用
图标\u new.MouseUp
时图片会再次发生变化。问题是,在关闭对话框之前,它的操作是正确的。在这一点上,图片会变回鼠标悬停时的图像

下面是这张图片经常做的事情:

Private Sub icon_new_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseDown
    icon_new.Image = My.Resources.NewMapClick
End Sub
Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
    icon_new.Image = My.Resources.NewMapHover
End Sub
Private Sub icon_new_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles icon_new.MouseEnter
    icon_new.Image = My.Resources.NewMapHover
End Sub
Private Sub icon_new_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles icon_new.MouseLeave
    icon_new.Image = My.Resources.NewMapDefault
End Sub
这将一直工作到对话框关闭,此时图像将变为
newmapover
,而此时图像应为
NewMapDefault
,因为鼠标不再位于图片的边界内。在诸如
Main.LostFocus
Main.GotFocus
Main.which
之类的调用中,我有
icon\u new.image=My.Resources.NewMapDefault
,但即使触发此调用,图像也会以
NewMapHover
结束。我不知道为什么会发生这种情况,也不知道如何解决。非常感谢您的帮助

编辑:这是调用对话框的单击事件

Private Sub icon_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles icon_new.Click

    If file_created = True Then
        save()
    Else 'file_created = false'
        SaveWindow.FileName = txt_name.Text
        If SaveWindow.ShowDialog() = DialogResult.OK Then
            file_path = SaveWindow.FileName
        End If
        file_created = True
    End If
    save()
    new_file()

End Sub

如果文件尚未保存,则会打开一个对话框,提示用户保存文件。我还玩过一个有Yes、No和Cancel提示的
MsgBox()
,但为了简单起见,我把它拿了出来,因为结果是一样的,而且有三分之一的时间会出现SaveFile对话框。

试着双击SaveFileDialog,它会打开SaveFileDialog\u FileOk事件代码,然后将与MouseLeave事件中相同的代码放在那里

按下“保存”按钮后,当SaveFileDialog即将关闭时,将触发此事件


编辑:

您可以在单击事件中尝试执行此操作:

Dim DResult As DialogResult
DResult = SaveFileDialog1.ShowDialog()

If DResult = System.Windows.Forms.DialogResult.OK Then
    'Code for when you press the save button, and when the image should change
ElseIf DResult = System.Windows.Forms.DialogResult.Cancel Then
    'Code for image change...
End If
试一试


瓦尔特

这是我最初的代码

Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
    icon_new.Image = My.Resources.NewMapHover
End Sub
我将变量
dialog\u open
添加为
布尔值
,并在调用SaveFile.ShowDialog()时将其设置为
true
。然后我将MouseUp事件更改为:

Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
    icon_new.Image = My.Resources.NewMapHover
    If dialog_open = True Then
        icon_new.Image = My.Resources.NewMapDefault
        dialog_open = False
    End If
End Sub

感谢@valter和@Kevin Hogg建议我编辑MouseUp事件。

唯一的问题是对话框关闭或单击取消。我已经尝试在调用对话框的click事件中输入结果为cancel或save时的代码,但结果仍然相同。理论上,这应该调用click事件,然后对话框应该被完全处理并脱离堆栈,然后是修复图像的代码,但由于某些原因,最终结果仍然是不正确的。不过,谢谢你的帮助。不幸的是,这是我第一次尝试。很抱歉,它不起作用。当鼠标向下、向上或单击时,对话框会打开?你有鼠标点击事件吗?提供代码。您是否尝试过将
icon\u new.Image=My.Resources.NewMapDefault
作为单击事件中的第一件事放在对话框之前?MouseEnter/Leave事件通常非常麻烦,当你做一些事情,比如用一个对话框把焦距调离时,你把故障拨到11。当你点击鼠标时,事件是:鼠标点击->鼠标向上。但在你的鼠标点击中,你打开了一个新窗口,因此你的窗体失去了焦点,鼠标离开事件被触发。老鼠还没有被触发。正在等待对话框关闭。所以你们有鼠标点击->鼠标离开->(正在等待对话框关闭)鼠标_up@ZeverMX调用模态对话框窗体意味着鼠标指针并不总是在原始控件上;
MouseLeave
事件仅在鼠标指针离开控制区域时触发,而不是在鼠标指针位于全新位置时触发。您是否在
MouseUp
事件中尝试过相同的代码?谢谢您的帮助。编辑MouseUp事件是一种方法,但我想我找到了一种更简单的方法。
Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
    icon_new.Image = My.Resources.NewMapHover
    If dialog_open = True Then
        icon_new.Image = My.Resources.NewMapDefault
        dialog_open = False
    End If
End Sub