C# Winform messagebox,如何在C中禁用YESNO选项#
我想在消息框中显示YesNo取消按钮,但同时,我想禁用YesNo按钮并只启用Cancel按钮 我想这样做的原因是我正在做一个演示应用程序,我想向用户展示特定的功能是可用的,但同时我不想给他们保存访问权限 下面是我的代码,现在来看看如何禁用YesNo按钮C# Winform messagebox,如何在C中禁用YESNO选项#,c#,winforms,messagebox,C#,Winforms,Messagebox,我想在消息框中显示YesNo取消按钮,但同时,我想禁用YesNo按钮并只启用Cancel按钮 我想这样做的原因是我正在做一个演示应用程序,我想向用户展示特定的功能是可用的,但同时我不想给他们保存访问权限 下面是我的代码,现在来看看如何禁用YesNo按钮 DialogResult result = MessageBox.Show("Save changes to " + this.Text.Substring(0, this.Text.Length - 1) + "?",
DialogResult result = MessageBox.Show("Save changes to " + this.Text.Substring(0, this.Text.Length - 1) + "?",
"Save confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
实际上,我想显示YesNo按钮,但我想为它禁用click Access。我想向用户显示3个按钮“是”、“否”和“取消”,但“单击访问权限”应仅用于“取消”按钮。可能吗
编辑:
谢谢大家的回答。
我找到了问题的答案
我的自定义消息框代码,我希望这可能对其他人有所帮助
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
String customResult = CustomMsgBox.ShowBox("Save changes to " + this.Text.Substring(0, this.Text.Length - 1) + "?", enumMessageIcon.Question);
枚举没有此类选项,它包含以下成员
因此,更好的选择是定制消息框,为此,您可以尝试,或者,或者简单地按照配方
- 创建一个窗体(让它成为
),构造函数接受一个字符串值,该字符串值是您想要显示的消息frmMessage
- 给出适当的标题文本,将其设置为
保存确认
- 放置标签以在构造函数的标签中显示消息
- 放置三个按钮,给出它们的名称和文本
- 禁用这两个选项(是/否),您的消息框已准备就绪
frmMessage frmMessageInstance = new frmMessage("Save changes to " + this.Text.Substring(0, this.Text.Length - 1) + "?");
frmMessageInstance.ShowDialog();
如所述,唯一的方法是创建自己的自定义messageBox
。我会这样做
/// <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 ();
}
}
}
//
///类内部使用的表单。
///
内部部分类CustomMessageForm:Form
{
///
///设计器支持需要此构造函数。
///
公共CustomMessageForm()
{
初始化组件();
}
公共CustomMessageForm(字符串标题、字符串描述)
{
初始化组件();
this.titleLabel.Text=标题;
this.descriptionLabel.Text=说明;
}
}
///
///您的自定义消息框助手。
///
公共静态类CustomMessageBox
{
公共静态无效显示(字符串标题、字符串描述)
{
//使用construct可以确保在表单关闭时释放资源
使用(var表单=新的CustomMessageForm(标题、说明)){
form.ShowDialog();
}
}
}
优雅地从for question的答案中复制过来只需创建自己的对话框,而不是使用内置对话框。为什么不创建自己的带有按钮的消息框呢?我是c#新手。我不知道如何创建自己的消息框。你必须创建自己的自定义对话框并对其进行管理。一旦你用谷歌搜索了如何操作,这就很简单了。遵循这个指南:实际上我想要YesNo按钮,但我想为它禁用click Access。我想向用户显示3个按钮“是”、“否”和“取消”,但“单击访问权限”应仅用于“取消”按钮。这可能吗?你能建议我如何将消息按钮设置为默认代码吗?可以在这里找到:@kaviarasan那个问题已经有了公认的答案,对吗?那个么你们还需要什么呢?我想把Ok按钮设为默认值。由于按钮是在运行时创建的,如何将特定按钮设置为默认值?