2013-02-26 92 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