【问题标题】:Custom MsgBox without form activate being fired没有表单激活的自定义 MsgBox 被触发
【发布时间】:2013-02-26 15:25:37
【问题描述】:

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

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

【问题讨论】:

  • 表单激活意味着焦点已经返回到父表单。此对话框是否以模态方式显示?
  • 是的,表格以模态方式显示。否则父表单不会等待它返回一个值。
  • 这引出了一个问题,即您在 _Activate 中所做的事情不应该被调用两次。由于每当您的应用程序重新获得焦点时都会调用该事件,因此它不适合初始化。
  • 你说的不是真的,迪安娜。 _Activate 只有在它自己的应用程序中重新获得焦点时才会运行。当它从外部应用程序重新获得焦点时,它不会。我的方案绝对合适。
  • 你的意思是你把 Form_Load 代码放在了 Form_Activate 中(在旧的 VB 代码中很常见)? VB 可能将 MsgBox 视为一种特殊情况(我知道这对你没有帮助)

标签: vb6 msgbox


【解决方案1】:

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

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

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

我就是这样用的。

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

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

【讨论】:

  • 我确实在使用另一种形式来创建 MsgBox。你是例子,但它有一个缺点。它需要在我使用的每个表单上都有一个 ActiveX 控件。也许我仍然可以通过将控件放在一个表单上然后公开重用该代码来使用它。
  • 使用 Activex 控件不是问题。但无论如何,您可以创建一个动态库 (customMsg.dll) 并将引用仅添加一次到您的项目中,并在您的所有表单上使用
  • 添加 ActiveX 控件真的比仅仅使用布尔值抑制 Form_Activate 中的逻辑更容易吗?
【解决方案2】:

我为自定义 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

【讨论】:

  • 对 DoEvents 使用循环不是很有效。当显示 msgbox 时,您的 CPU 可能正在达到峰值。
  • 我不知道其他解决方案。我是从网上得到的。
猜你喜欢
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 2020-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多