Generate dialog result from a custom message box class


Keywords:vb.net 


Question: 

I am developing a custom messagebox class like the following-

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

Those functions are designed with related graphics, color, title and heading.

btnOk will return DialogResult.Ok, btnNo will return DialogResult.No and btnYes will return DialogResult.Yes

How do i return the dialog result with those functions?

How do i know that which button is pressed?

i dont know how to handle a button click event in a formless class.

Would you give me the idea?

Thank you in advance.

SKPaul


2 Answers: 

Start with the easy one. You will have to manually wire-up the events by using the AddHandler and RemoveHandler keywords this

AddHandler btnNo.Click, AddressOf btnNo_Click

btnNo is the button object. The ".Click" is the event you want captured. The AddressOf gets a pointer to the function (basically, it tells the compiler where the function is. Think of it as a different type of "handles".)

You'll have to remvoe the handler when your done, by doing this.

RemoveHandler btnNo.Click, AddressOf btnNo_Click

To set the Dialog Results, the form must be called via ShowDialog. You Simple set the DialogResults property of the form. I'd do it in the form.closing event.

me.DialogResult = Windows.Forms.DialogResult.OK


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

and it will return result Abort