2

我开发了一个自定义 MsgBox,几乎在所有方面都可以正常工作。唯一的问题是当 MsgBox 关闭父窗体时运行 Form_Activate 代码。普通的 MsgBox 不会(再次)运行该代码。

我知道我可以向 Form_Activate 添加一个布尔变量来检查它是否已经触发,但是当你有十几个表单时,这不是最好的解决方案。那么,有没有办法在关闭我的自定义 MsgBox 后不运行 Form_Activate?MsgBox 表单是否需要某种特殊类型或其他东西?我尝试了所有 BorderStyles 但这没有任何区别。

4

2 回答 2

2

您是否正在使用其他表单来制作自定义 MsgBox?

您不应该直接使用其他形式来显示自定义消息框。您应该创建一个 Activex 控件,并且当 MsgBox 关闭时,Activate 事件不会再次触发。

如果需要,您可以在控件中使用表单。(可能只需将代码放在 ActiveX 控件项目中并在表单中使用它)

我就是这样用的。

这是一个使用 Activex 控件的自定义 MsgBox 示例,也带有一个测试表单。

http://www.codeguru.com/code/legacy/vb_othctrl/2166_CustomMsgBox.zip

于 2013-02-26T22:55:22.937 回答
0

我为自定义 MsgBox 创建了一个类。

Public Class CustomMsgBox
'Creates the Main form
Dim Main As New Form

'Creates the buttons
Dim Btn1, Btn2, Btn3 As New Button

'Creates the label
Dim Lbl As New Label

'Creates the Output variable
Dim Output As Integer = 0

Private Sub Load()
    'Btn1 properties
    Btn1.Location = New Point(168, 69)
    Btn1.AutoSize = True
    Btn1.AutoSizeMode = AutoSizeMode.GrowOnly

    'Btn2 properties
    Btn2.Location = New Point(87, 69)
    Btn1.AutoSize = True
    Btn1.AutoSizeMode = AutoSizeMode.GrowOnly

    'Btn3 properties
    Btn3.Location = New Point(6, 69)
    Btn1.AutoSize = True
    Btn1.AutoSizeMode = AutoSizeMode.GrowOnly

    'Lbl properties
    Lbl.Location = New Point(12, 19)
    Lbl.AutoSize = True
    Lbl.AutoEllipsis = True

    'Main form properties
    Main.Size = New Size(211, 129)
    Main.AutoSize = True
    Main.AutoSizeMode = AutoSizeMode.GrowOnly
    Main.ShowIcon = False
    Main.Controls.Add(Btn1)
    Main.Controls.Add(Btn2)
    Main.Controls.Add(Btn3)
    Main.Controls.Add(Lbl)

    'Adds Handlers to the buttons
    AddHandler Btn1.Click, AddressOf btn1_Click
    AddHandler Btn2.Click, AddressOf btn2_Click
    AddHandler Btn3.Click, AddressOf btn3_Click

End Sub

Function CstMsgBox(ByRef Msg As String, ByRef Title As String, ByRef B1 As String, Optional ByRef B2 As String = Nothing, Optional ByRef B3 As String = Nothing) As Integer
    'Runs the Load() Sub
    Load()

    'Sets the values
    Lbl.Text = Msg
    Btn1.Text = B1
    Btn2.Text = B2
    Btn3.Text = B3
    Main.Text = Title

    'Checks if there is a value set to Btn2 and Btn3
    If Btn2.Text = Nothing Then
        Btn2.Hide()
    End If
    If Btn3.Text = Nothing Then
        Btn3.Hide()
    End If

    'Shows the MsgBox
    Main.Show()

    'Waits until a button is pressed
    Do Until Output <> 0
        Application.DoEvents()
    Loop

    'Closes the MsgBox
    Main.Close()
    Return Output

End Function

Private Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Sets the Output value to 1
    Output = 1

End Sub

Private Sub btn2_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Sets the Output value to 2
    Output = 2

End Sub

Private Sub btn3_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Sets the Output value to 3
    Output = 3

End Sub
End Class

您可以通过键入以下内容来使用它:

Dim CMB As New CustomMsgBox
    CCMB.CstMsgBox('MSG, 'TITLE, 'BUTTON1, 'Optional: BUTTON2, 'Optional: BUTTON3)

或者

Dim CMB As New CustomMsgBox
    Select Case CMB.CstMsgBox('MSG, 'TITLE, 'BUTTON1, 'Optional: BUTTON2, 'Optional: BUTTON3)
        Case 1
            'Code to execute when button1 is pressed
        Case 2
            'Code to execute when button2 is pressed
        Case 3
            'Code to execute when button3 is pressed
    End Select
于 2016-03-20T10:38:29.303 回答