2013-02-26 48 views
2

我开发了一个自定义MsgBox,几乎可以在任何方式下正常工作。唯一的问题是当MsgBox关闭时父窗体运行Form_Activate代码。正常的MsgBox不会再运行该代码。自定义MsgBox没有窗体激活正在被解雇

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

+1

形式激活手段重点又回到了父窗体。这个对话框是模态显示的吗? – 2013-02-26 15:29:31

+0

是的,窗体以模态显示。否则,父表单不会等待它返回一个值。 – Martin 2013-02-26 15:37:27

+0

这引出了你在'_Activate'中做什么不应该被调用两次的问题。因为只要您的应用程序重新获得焦点,就会调用该事件,因此不适合初始化。 – Deanna 2013-02-26 15:43:00

回答

2

您是否使用其他表单来定制MsgBox?

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

在控制,如果你想它,你可以使用的形式。 (大概只需要放置一个ActiveX控件项目中的代码,并在表单中使用它)

我使用这种方式。

这是使用ActiveX控件,用测试形式太定制MSGBOX例子。

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

+0

的我确实用另一种形式来创建MSGBOX。你是典型的作品,但它有一个缺点。它需要在我使用的每个表单上都有一个ActiveX控件。也许我还可以通过把一个窗体上的控件使用它,然后公开地重新使用该代码。 – Martin 2013-02-27 09:48:11

+0

使用ActiveX控件是没有问题的。但无论如何,你可以创建一个动态库(customMsg.dll),只有一次添加引用到您的proyect,并在所有形式的 – Gonzalo 2013-02-27 10:18:50

+0

使用是不是真的更容易添加一个ActiveX控件不仅仅是抑制Form_Activate逻辑与布尔? – MarkJ 2016-03-21 15:06:33

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 
+0

使用循环与调用DoEvents是不是很有效。显示msgbox时,您的CPU可能达到峰值。 – Martin 2016-03-21 14:14:01

+0

我不知道其他的解决办法。我从互联网上获得它。 – HighTechProgramming15 2016-03-24 06:53:33