Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear friend,

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 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
Posted
Updated 2-Aug-11 16:53pm
v4

Have you tried:
VB
Private Sub btnNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo.Click
        Me.DialogResult = DialogResult.No
End Sub
 
Share this answer
 
Comments
Saumitra Kumar Paul 2-Aug-11 21:41pm    
Thank you for your reply.

But the problem is with the last part "Handles btnNo.Click". "btnNo" is getting blue underline and showing the tooltip "Handles clause require a WithEvents variable defining in the containing type or one of its base types."
JOAT-MON 3-Aug-11 1:06am    
Based on this article:
http://www.codeproject.com/KB/vb/StepByStepEventsInVBNET.aspx
it looks like you need to add WithEvents to your button declarations. Take a look at the 'Handling events (static)' section.
As you do not derive your class MyCustomMsgBox from anything but simply wrap it around a form, you have a freedom to implement return result the way you want. Not clear why you think this is a problem.

Probably, you want to return dialog result from ShowErrorMsg. First, make it a function and return obj.MyForm.ShowDialog(). This call already returns System.Windows.Forms.DialogResult. Now, you want to return right value. It's simple because System.Windows.Forms.Form.DialogResult is a read/write property, so you can assign proper value to MyForm. Assign it in the event handles of your buttons.

Do the same thing in ShowSuccessMsg.

Now, I think your temporary object obj is redundant. It does not carry any functionality. You can create a temporary instance of the form instead and return its DialogResult. Also, having two separate functions ShowErrorMsg and ShowSuccessMsg is very… questionable.

From the stand point of the message dialog functionality there is no distinct difference between error and success. You could perfectly satisfy your requirements with only one method ShowMessage. Even if you want to show error and success in different ways (say, different color), a method parameter will do. What's wrong about passing System.Windows.Forms.MessageBoxIcon? (Also, think about this: why showing success at all? You can consider success as a normal most expected behavior of all operation, show only the errors. :-))

Why not reproducing interface of System.Windows.Forms.MessageBox.Show methods? You pass information about buttons you need, icon (which also conduct information about the indent of the message: error, success or something else). You can even improve this interface: add images, allow for custom button set, etc.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900