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

时间:2014-08-27 20:42:58

标签: vb.net user-interface

我想知道如何在SaveFileDialog框等对话框关闭时强制我的主gui更新。我尝试了Main.LostFocusMain.GotFocusMain.EnterMain.MouseEnterMain.MouseLeaveMain.MouseMove,但无论我尝试什么功能,我都无法做到得到我想要的结果。

单击图片时会打开对话框。单击时会更改图片,而在调用icon_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

这一直有效,直到对话框关闭,此时图像变为NewMapHover时应该是NewMapDefault,因为鼠标不再在图片的边界内。在Main.LostFocusMain.GotFocusMain.Whatever之类的通话中,我有icon_new.image = My.Resources.NewMapDefault,但即使此通话触发,图片也会以NewMapHover结尾。我不确定为什么会发生这种情况或如何解决它。非常感谢任何帮助。

编辑:这是调用dialog_box

的点击事件
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

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

3 个答案:

答案 0 :(得分:0)

尝试双击SaveFileDialog,它将打开SaveFileDialog_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

答案 1 :(得分:0)

尝试

Private Sub icon_new_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icon_new.MouseUp
    Dim pnt As Point
    Dim rect As Rectangle

    rect.X = icon_new.Location.X
    rect.Y = icon_new.Location.Y
    rect.Width = icon_new.Width
    rect.Height = icon_new.Height

    pnt = PointToClient(Cursor.Position)

    If Not rect.Contains(pnt) Then
        Return
    End If

    icon_new.Image = My.Resources.NewMapHover
End Sub

瓦尔特

答案 2 :(得分:0)

这是我原来的代码。

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_open添加为boolean,并在每次调用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事件。