【发布时间】:2016-12-20 06:06:16
【问题描述】:
如何为以下代码实现自定义对话框结果,我需要在以下代码中进行哪些更改才能获得对话框结果?
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);
现在我如何进一步实现代码以获得对话结果?
【问题讨论】:
-
internal static DialogResult ShowBox(...并使用return fromShowMessage.ShowDialog(); -
谢谢,成功了
-
我最初尝试将返回值设置为 DialogResult,然后再将这个问题发布到 Void。但未能设置返回“return frmMessage.ShowDialog();”我是多么的愚蠢。无论如何,现在这有效。特别感谢 Ivan Stoev
-
你能建议我用我的问题中的上述代码让消息按钮默认的方法吗?
-
不确定将消息按钮设为默认值是什么意思。您可以考虑发布另一个问题,详细说明您想要实现的目标。
标签: c# winforms messagebox