2

我正在开发一个自定义消息框类,如下所示 -

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

如何使用这些功能返回对话结果?

我怎么知道按下了哪个按钮?

我不知道如何处理无形类中的按钮单击事件。

你能给我这个主意吗?

先感谢您。

SK保罗

4

2 回答 2

1

从简单的开始。您将不得不使用AddHandler 和 RemoveHandler关键字手动连接事件

AddHandler btnNo.Click, AddressOf btnNo_Click

btnNo 是按钮对象。“.Click”是您要捕获的事件。AddressOf 获取指向函数的指针(基本上,它告诉编译器函数在哪里。将其视为不同类型的“句柄”。)

完成后,您必须通过这样做来移除处理程序。

RemoveHandler btnNo.Click, AddressOf btnNo_Click

要设置对话框结果,必须通过 ShowDialog 调用表单。您只需设置表单的 DialogResults 属性。我会在 form.closure 事件中这样做。

me.DialogResult = Windows.Forms.DialogResult.OK
于 2011-08-03T03:10:14.813 回答
0
Me.DialogResult = Windows.Forms.DialogResult.Abort
Me.Close()

它将返回结果 Abort

于 2014-04-25T20:57:12.837 回答