没有表单的自定义MsgBox激活被触发

时间:2013-02-26 15:25:38

标签: vb6 msgbox

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

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

2 个答案:

答案 0 :(得分:2)

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

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

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

我用那种方式。

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

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

答案 1 :(得分: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)

OR

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