以下自定义消息框

时间:2016-12-20 06:06:16

标签: c# winforms messagebox

如何为以下代码实现自定义对话框结果,我需要在以下代码中进行哪些更改才能获得对话框结果?

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);

现在我如何进一步实现代码以获得对话框结果?

2 个答案:

答案 0 :(得分:2)

而不是

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();
}

试试这个

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();
}

答案 1 :(得分:0)

你有formMessage对象,添加控件并调用showDialog()。 请注意,当ShowDialog返回时,您仍然拥有对象frmMessage和 您可以访问该对象的方法和属性。

因此,当单击相应的按钮时,您可以在您的设置中设置属性 fromMessage类按下什么按钮。也许您可以设置一些其他字段。

showDialog返回后,您可以访问这些属性,也许构建自己的DialogResult并返回该结果