如何显示带有复选框的MessageBox
本文关键字:复选框 MessageBox 何显示 显示 | 更新日期: 2023-09-27 18:20:21
我想创建一个具有是/否按钮和复选框的MessageBox
。
该应用程序是一个图片大小调整器,它将同时重新调整多个图片的大小;在这个过程中,它将检查新的位置文件名是否存在,并选择覆盖它。
如果需要,MessageBox
将为用户提供覆盖任何新文件的选项,而复选框将防止用户在想要覆盖每个文件时必须单击是x多次。
如何在MessageBox
对话框中添加复选框?
创建一个自定义对话框。这里有一些东西可以给你一个想法:
public static class CheckboxDialog
{
public static bool ShowDialog(string text, string caption)
{
Form prompt = new Form();
prompt.Width = 180;
prompt.Height = 100;
prompt.Text = caption;
FlowLayoutPanel panel = new FlowLayoutPanel();
CheckBox chk = new CheckBox();
chk.Text = text;
Button ok = new Button() { Text = "Yes" };
ok.Click += (sender, e) => { prompt.Close(); };
Button no = new Button() { Text = "No" };
no.Click += (sender, e) => { prompt.Close(); };
panel.Controls.Add(chk);
panel.SetFlowBreak(chk, true);
panel.Controls.Add(ok);
panel.Controls.Add(no);
prompt.Controls.Add(panel);
prompt.ShowDialog();
return chk.Checked;
}
}
你可以这样使用它:
bool overwrite = CheckboxDialog.ShowDialog("overwrite", "Overwrite location?");
您不能在MessageBox中添加复选框。正如Tim和rsbarro建议的那样,您应该创建一个自定义对话框。Tim的回答会很好,而且不需要创建新的类。如果你想在设计器中设计表单,你可以试试这个。
- 使用两个按钮和所需的复选框创建一个新表单
- 在表单设计器中,将Yes按钮的
DialogResult
属性设置为Yes,并将No- 在窗体上创建一个反映复选框状态的属性(可选-我不喜欢从另一个窗体引用一个窗体上的控件,但如果将复选框公开,也可以)
public bool DoForAll
{
get { return checkBox.Checked; }
}
- 在主窗体上,需要时显示子窗体。例如:
var options = new Options();
var result = options.ShowDialog();
if (result == DialogResult.Yes)
{
var doForAll = options.DoForAll;
}
对于vb.net(视图是此代码,只显示一个按钮)
clsMsgBoxV1.ShowDialog("PDF solo de albaranes. (No avisarme mas)", "PDF")
并使用这个共享类:
Public Class clsMsgBoxV1
Shared prompt As Form
Shared chk As CheckBox
Public Shared Function ShowDialog(Text As String, caption As String) As Boolean
'frmUsuario
prompt = New Form()
chk = New CheckBox()
prompt.StartPosition = FormStartPosition.CenterParent
prompt.Width = 220
prompt.Height = 150
prompt.Text = caption
prompt.MinimumSize = prompt.Size
prompt.MaximumSize = prompt.Size
prompt.MaximizeBox = False
prompt.MinimizeBox = False
Dim panel As FlowLayoutPanel = New FlowLayoutPanel()
chk.Text = Text
chk.Width = chk.Width * 2
chk.Height = 50
Dim ok As Button = New Button() With {
.Text = "Leido"
}
AddHandler ok.Click, AddressOf OKClick
'Dim no As Button = New Button() With {
'.Text = "No"
'}
'AddHandler no.Click, AddressOf NoClick
ok.Width = prompt.Width - 30
panel.Controls.Add(chk)
panel.SetFlowBreak(chk, True)
panel.Controls.Add(ok)
'panel.Controls.Add(no)
prompt.Controls.Add(panel)
prompt.ShowDialog()
prompt.Dispose()
Return chk.Checked
End Function
Private Shared Sub NoClick(sender As Object, e As EventArgs)
prompt.Close()
End Sub
Private Shared Sub OKClick(sender As Object, e As EventArgs)
prompt.Close()
End Sub
End Class
我意识到这是一个非常古老的线程,但我只想花两分钱。
Ookii是将CheckBox实现为MessageBox或自定义它的最简单方法:https://github.com/ookii-dialogs/ookii-dialogs-wpf(我使用它是因为经典的"文件夹浏览器"对话框,所以我想还不如用它来做复选框的事情)
这是我的代码:
using Ookii.Dialogs.Wpf;
//create instance of ookii dialog
TaskDialog dialog = new();
//create instance of buttons
TaskDialogButton butYes = new TaskDialogButton("Yes");
TaskDialogButton butNo = new TaskDialogButton("No");
TaskDialogButton butCancel = new TaskDialogButton("Cancel");
//checkbox
dialog.VerificationText = "Dont Show Again"; //<--- this is what you want.
//customize the window
dialog.WindowTitle = "Confirm Action";
dialog.Content = "You sure you want to close?";
dialog.MainIcon = TaskDialogIcon.Warning;
//add buttons to the window
dialog.Buttons.Add(butYes);
dialog.Buttons.Add(butNo);
dialog.Buttons.Add(butCancel);
//show window
TaskDialogButton result = dialog.ShowDialog(this);
//get checkbox result
if (dialog.IsVerificationChecked)
{
//do stuff
}
//get window result
if (result != butYes)
{
//if user didn't click "Yes", then cancel the closing.
e.Cancel = true;
return;
}