[英]Winform messagebox, How to disable YESNO options in C#
I want to show YesNoCancel button in the message box, but at the same time, I wanted to disable YesNo button and enable only Cancel button. 我想在消息框中显示YesNoCancel按钮,但同时,我想禁用YesNo按钮,而仅启用Cancel按钮。
The reason I wanted to do like this is I am doing a demo application where I want to show users that particular feature is available but at the same time I don't want to give them save access. 我想这样做的原因是我正在做一个演示应用程序,在此我想向用户显示特定功能可用,但与此同时,我不想给他们保存访问权限。
Following is my code, now to how to Disable YesNo button. 以下是我的代码,现在介绍如何禁用YesNo按钮。
DialogResult result = MessageBox.Show("Save changes to " + this.Text.Substring(0, this.Text.Length - 1) + "?",
"Save confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
Actually I want to show YesNo buttons but I wanted to disable click acess for it. 实际上,我想显示YesNo按钮,但是我想禁用它的单击访问。 I wanted to show users 3 buttons YES , No and Cancel but click acess should be given only to cancel button.
我想向用户显示3个按钮YES,No和Cancel,但是单击acess只能用于取消按钮。 Is that possible?
那可能吗?
edit: Thanks all for the answers. 编辑:谢谢大家的答案。
I have found soultion for my question 我为我的问题感到高兴
My code for custom message box, I hope this might help someone 我的自定义消息框代码,希望对您有所帮助
customMsgBox.cs customMsgBox.cs
enter code here { public partial class CustomMsgBox : Form
{
static CustomMsgBox MsgBox;
static string Button_id;
public CustomMsgBox()
{
InitializeComponent();
}
internal static string ShowBox(string txtMessage, enumMessageIcon messageIcon)
{
MsgBox = new CustomMsgBox();
MsgBox.labelCustomMsg.Text = txtMessage;
MsgBox.addIconImage(messageIcon);
MsgBox.ShowDialog();
return Button_id;
}
/// <summary>
/// We can use this method to add image on message box.
/// I had taken all images in ImageList control so that
/// I can easily add images. Image is displayed in
/// PictureBox control.
/// </summary>
/// <param name="MessageIcon">Type of image to be displayed.</param>
private void addIconImage(enumMessageIcon MessageIcon)
{
switch (MessageIcon)
{
case enumMessageIcon.Error:
pictureBox1.Image = imageList1.Images["Error"]; //Error is key
//name in imagelist control which uniquely identified images
//in ImageList control.
break;
case enumMessageIcon.Information:
pictureBox1.Image = imageList1.Images["Information"];
break;
case enumMessageIcon.Question:
pictureBox1.Image = imageList1.Images["Question"];
break;
case enumMessageIcon.Warning:
pictureBox1.Image = imageList1.Images["Warning"];
break;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
Button_id = "Cancel";
MsgBox.Dispose();
}
private void btnNo_Click(object sender, EventArgs e)
{
Button_id = "No";
MsgBox.Dispose();
}
private void btnYes_Click(object sender, EventArgs e)
{
Button_id = "Yes";
MsgBox.Dispose();
}
}
#region constant defiend in form of enumration which is used in showMessage class.
internal enum enumMessageIcon
{
Error,
Warning,
Information,
Question,
}
internal enum enumMessageButton
{
OK,
YesNo,
YesNoCancel,
OKCancel
}
#endregion
} }
main.cs main.cs
String customResult = CustomMsgBox.ShowBox("Save changes to " + this.Text.Substring(0, this.Text.Length - 1) + "?", enumMessageIcon.Question);
The MessageBoxButtons
Enumerations does not have such options, It contains the following members MessageBoxButtons
枚举没有此类选项,它包含以下成员
So the better option for you is a custom message box, for this you can try this , this or this , Or simply follow the recipe, 因此,对您来说,更好的选择是自定义消息框,为此,您可以尝试this , this或this ,或者简单地遵循食谱,
frmMessage
) with a constructor accepts a string value that is the message that you wanted to display, frmMessage
)接受一个字符串值,该字符串值是您要显示的消息, Save confirmation
, Save confirmation
, Usage Example: 用法示例:
Now you need to create an object of this message box and call them like the following: 现在,您需要创建此消息框的对象,并按如下所示对其进行调用:
frmMessage frmMessageInstance = new frmMessage("Save changes to " + this.Text.Substring(0, this.Text.Length - 1) + "?");
frmMessageInstance.ShowDialog();
As explained by Un-lucky , the only way you can do that is by creating your own custom messageBox
. 如Un-lucky所述,唯一的方法是创建自己的自定义
messageBox
。 I would do something like this 我会做这样的事情
/// <summary>
/// The form internally used by <see cref="CustomMessageBox"/> class.
/// </summary>
internal partial class CustomMessageForm : Form
{
/// <summary>
/// This constructor is required for designer support.
/// </summary>
public CustomMessageForm ()
{
InitializeComponent();
}
public CustomMessageForm (string title, string description)
{
InitializeComponent();
this.titleLabel.Text = title;
this.descriptionLabel.Text = description;
}
}
/// <summary>
/// Your custom message box helper.
/// </summary>
public static class CustomMessageBox
{
public static void Show (string title, string description)
{
// using construct ensures the resources are freed when form is closed
using (var form = new CustomMessageForm (title, description)) {
form.ShowDialog ();
}
}
}
Graciously copied from an answer by Dan Abramov for question How to create a custom MessageBox? 从Dan Abramov的问题答案中复制而来的问题如何创建自定义MessageBox?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.