2011-08-03 108 views
2

對話的結果我開發一個自定義MessageBox類,如以下各項生成自定義消息框類

Public Class MyCustomMsgBox 


    Private MyForm As Form = New Form 
    Private lblHeadline As Label = New Label 
    Private lblMessageBody As Label = New Label 
    Private btnNo As Button = New Button 
    Private btnOk As Button = New Button 
    Private btnYes As Button = New Button 

    Public Sub New(ByVal Message As String) 
     With MyForm 
      .Width = 438 
      .Height = 214 
      .Controls.AddRange(New Control() {lblHeadline, lblMessageBody, btnNo, btnYes, btnOk}) 
     End With 
    End Sub 

    Public Shared Function ShowErrorMsg(ByVal ErrorMessage As String) As  Windows.Forms.DialogResult 
     Dim obj As MyCustomMsgBox = New MyCustomMsgBox(ErrorMessage) 
     obj.MyForm.ShowDialog() 
    End Sub 

    Public Shared function ShowSuccessMsg(ByVal SuccessMessage As String) As  Windows.Forms.DialogResult 
     'some code 
    End Sub 

    Public Shared Function AskQuestions(ByVal Questions As String) As Windows.Forms.DialogResult 
     'some code 
    End Sub 

    Public Shared Function ShowExceptions(ByVal ExMessage As String) As Windows.Forms.DialogResult 
     'some code 
    End Sub 


    'Private Sub btnNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo.Click 
    ' Windows.Forms.DialogResult.No() 
    'End Sub 

    End Class 

這些功能都設計有相關的圖形,顏色,標題和標題。

btnOk將返回DialogResult.Ok,btnNo將返回DialogResult.No和btnYes將返回DialogResult.Yes

我如何返回與這些功能的對話框結果?

我如何知道哪個按鈕被按下?

我不知道如何處理無形類中的按鈕單擊事件。

你能告訴我這個想法嗎?

預先感謝您。

SKPaul

回答

1

從容易開始。你將不得不使用AddHandler and RemoveHandler關鍵字這

AddHandler btnNo.Click, AddressOf btnNo_Click 

btnNo是按鈕對象手動關聯式的事件。 「.Click」是您想要捕獲的事件。 AddressOf得到一個指向函數的指針(基本上,它告訴編譯器函數在哪裏,把它看作是一個不同類型的「句柄」。)

當你完成時,你必須提交處理函數,通過這樣做。

RemoveHandler btnNo.Click, AddressOf btnNo_Click 

要設置對話框結果,必須通過ShowDialog調用窗體。您可以簡單地設置表單的DialogResults屬性。我會在form.closing事件中做到這一點。

me.DialogResult = Windows.Forms.DialogResult.OK 
+0

哇,它爲我工作。你簡直太棒了。非常感謝。 –

0
Me.DialogResult = Windows.Forms.DialogResult.Abort 
Me.Close() 

,它會返回結果中止

+0

您不需要'Me.Close()'行。將模態窗體的DialogResult屬性設置爲None而不是None會關閉它。 – LarsTech