Custom Dialog result for the following custom message box

906 views Asked by At

How can I implement custom dialog result for the following code, what all the changes I need to make in the following code to get the dialog result?

private void addButton(enumMessageButton MessageButton)
    {
        switch (MessageButton)
        {
            case enumMessageButton.OK:
                {
                    //If type of enumButton is OK then we add OK button only.
                    Button btnOk = new Button();  //Create object of Button.
                    btnOk.Text = "OK";  //Here we set text of Button.
                    btnOk.DialogResult = DialogResult.OK;  //Set DialogResult property of button.
                    btnOk.FlatStyle = FlatStyle.Popup;  //Set flat appearence of button.
                    btnOk.FlatAppearance.BorderSize = 0;
                    btnOk.SetBounds(pnlShowMessage.ClientSize.Width - 80, 5, 75, 25);  // Set bounds of button.
                    pnlShowMessage.Controls.Add(btnOk);  //Finally Add button control on panel.
                }
                break;
            case enumMessageButton.OKCancel:
                {
                    Button btnOk = new Button();
                    btnOk.Text = "OK";
                    btnOk.DialogResult = DialogResult.OK;
                    btnOk.FlatStyle = FlatStyle.Popup;
                    btnOk.FlatAppearance.BorderSize = 0;
                    btnOk.SetBounds((pnlShowMessage.ClientSize.Width - 70), 5, 65, 25);
                    pnlShowMessage.Controls.Add(btnOk);

                    Button btnCancel = new Button();
                    btnCancel.Text = "Cancel";
                    btnCancel.DialogResult = DialogResult.Cancel;
                    btnCancel.FlatStyle = FlatStyle.Popup;
                    btnCancel.FlatAppearance.BorderSize = 0;
                    btnCancel.SetBounds((pnlShowMessage.ClientSize.Width - (btnOk.ClientSize.Width + 5 + 80)), 5, 75, 25);
                    pnlShowMessage.Controls.Add(btnCancel);

                }
                break;
        }
    }

internal static void ShowBox(string messageText, string messageTitle, enumMessageIcon messageIcon, enumMessageButton messageButton)
    {
        frmShowMessage frmMessage = new frmShowMessage();
        frmMessage.setMessage(messageText);
        frmMessage.Text = messageTitle;
        frmMessage.addIconImage(messageIcon);
        frmMessage.addButton(messageButton);
        frmMessage.ShowDialog();
    }

Main.cs

frmShowMessage.ShowBox("This is message box which represent message with title, custome button and custom icon.", "This is message title", enumMessageIcon.Question, enumMessageButton.OKCancel);

Now how do further I implement code to get dialog result?

2

There are 2 answers

1
Mohit S On BEST ANSWER

Instead of

internal static void ShowBox(string messageText, string messageTitle, enumMessageIcon messageIcon, enumMessageButton messageButton)
{
    frmShowMessage frmMessage = new frmShowMessage();
    frmMessage.setMessage(messageText);
    frmMessage.Text = messageTitle;
    frmMessage.addIconImage(messageIcon);
    frmMessage.addButton(messageButton);
    frmMessage.ShowDialog();
}

try this

internal static DialogResult ShowBox(string messageText, string messageTitle, enumMessageIcon messageIcon, enumMessageButton messageButton)
{
    frmShowMessage frmMessage = new frmShowMessage();
    frmMessage.setMessage(messageText);
    frmMessage.Text = messageTitle;
    frmMessage.addIconImage(messageIcon);
    frmMessage.addButton(messageButton);
    return frmMessage.ShowDialog();
}
0
dgorti On

You have the formMessage object, adding controls and calling showDialog(). Note that when the ShowDialog returns, you still have the object frmMessage and you can access the methods and properties on that object.

So when the corresponding buttons are clicked, you could set a property in your fromMessage class on what button is pressed. Perhaps you have some additional fields that you can set.

After the showDialog returns, you can access those properties, perhaps construct your own DialogResult and return that result