没有默认按钮的 MessageBox
MessageBox without a default button
我的应用程序中有一个表单,用户可以在其中输入序列号列表,每个序列号都经过快速检查,然后添加到列表中(这是用于盘点模块)。因此,用户通常使用条形码扫描仪从一堆库存物品中扫描序列号。
我正在处理 TextBox
的 KeyPress
事件,该事件在用户扫描项目并寻找 e.KeyChar == 13
(回车键)时获得焦点。每当按下回车键时,我就知道我有一个完整的序列号,然后我可以在将其添加到列表之前对其进行验证。
这是我的问题所在;在某些情况下,此时我必须提示用户他是否真的想要将库存项目添加到列表中。我为此使用 MessageBox
,像这样:
if (MessageBox.Show("This is a special stock item.\r\nDo you want to add it to the list?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
// Add item to list
else
// Do not add item to list
但是因为这只是偶尔发生的事情,所以用户通常甚至看不到 MessageBox
而只是扫描下一个序列号,当然这个序列号会丢失但以回车键结尾,这会触发 MessageBox
上的默认按钮,并且在没有打算这样做的情况下,有时甚至没有意识到它发生了,用户将一个项目添加到列表中并错过了后续项目。
有什么方法可以防止 MessageBox
在按下回车键时触发任何按钮? 我不介意用户在之后继续扫描条形码条形码并全部丢失,只要 MessageBox
一直在屏幕上,直到他意识到需要注意并故意 select 两个选项之一。
Is there a way I can prevent the MessageBox from firing any buttons if
enter is pressed?
没有
I don't mind if the user goes on scanning barcode after barcode and
losing them all, as long as the MessageBox remains on screen until he
realises his attention is required and deliberately select one of the
two options.
使用YesNoCancel
选项,并将默认设置为Button3
,即Cancel
按钮。现在继续循环,直到结果为 Cancel
。当循环退出时,用户将选择 Yes
或 No
:
DialogResult result;
do
{
result = MessageBox.Show("This is a special stock item.\r\nDo you want to add it to the list?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
} while (result == DialogResult.Cancel);
if (result == DialogResult.Yes)
{
// yes
}
else
{
// no
}
------------编辑---------
I'm not overly fond of the idea of having a button on the dialog that
means nothing other than for some obscure application logic (from the
user's perspective).
同意...具有 "noop" 按钮不是最佳选择。上面的解决方案是一个快速而肮脏的 "fix".
当您着手实现自己的自定义 MessageBox 表单时,这里有一种简单的方法可以让它在当前聚焦 Button 时忽略 Enter 键:
public partial class frmVerify : Form
{
public frmVerify()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter && this.ActiveControl is Button)
{
return true; // suppress the keystroke
}
return base.ProcessCmdKey(ref msg, keyData);
}
// ... more code ...
}
我的应用程序中有一个表单,用户可以在其中输入序列号列表,每个序列号都经过快速检查,然后添加到列表中(这是用于盘点模块)。因此,用户通常使用条形码扫描仪从一堆库存物品中扫描序列号。
我正在处理 TextBox
的 KeyPress
事件,该事件在用户扫描项目并寻找 e.KeyChar == 13
(回车键)时获得焦点。每当按下回车键时,我就知道我有一个完整的序列号,然后我可以在将其添加到列表之前对其进行验证。
这是我的问题所在;在某些情况下,此时我必须提示用户他是否真的想要将库存项目添加到列表中。我为此使用 MessageBox
,像这样:
if (MessageBox.Show("This is a special stock item.\r\nDo you want to add it to the list?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
// Add item to list
else
// Do not add item to list
但是因为这只是偶尔发生的事情,所以用户通常甚至看不到 MessageBox
而只是扫描下一个序列号,当然这个序列号会丢失但以回车键结尾,这会触发 MessageBox
上的默认按钮,并且在没有打算这样做的情况下,有时甚至没有意识到它发生了,用户将一个项目添加到列表中并错过了后续项目。
有什么方法可以防止 MessageBox
在按下回车键时触发任何按钮? 我不介意用户在之后继续扫描条形码条形码并全部丢失,只要 MessageBox
一直在屏幕上,直到他意识到需要注意并故意 select 两个选项之一。
Is there a way I can prevent the MessageBox from firing any buttons if enter is pressed?
没有
I don't mind if the user goes on scanning barcode after barcode and losing them all, as long as the MessageBox remains on screen until he realises his attention is required and deliberately select one of the two options.
使用YesNoCancel
选项,并将默认设置为Button3
,即Cancel
按钮。现在继续循环,直到结果为 Cancel
。当循环退出时,用户将选择 Yes
或 No
:
DialogResult result;
do
{
result = MessageBox.Show("This is a special stock item.\r\nDo you want to add it to the list?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
} while (result == DialogResult.Cancel);
if (result == DialogResult.Yes)
{
// yes
}
else
{
// no
}
------------编辑---------
I'm not overly fond of the idea of having a button on the dialog that means nothing other than for some obscure application logic (from the user's perspective).
同意...具有 "noop" 按钮不是最佳选择。上面的解决方案是一个快速而肮脏的 "fix".
当您着手实现自己的自定义 MessageBox 表单时,这里有一种简单的方法可以让它在当前聚焦 Button 时忽略 Enter 键:
public partial class frmVerify : Form
{
public frmVerify()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter && this.ActiveControl is Button)
{
return true; // suppress the keystroke
}
return base.ProcessCmdKey(ref msg, keyData);
}
// ... more code ...
}