简体   繁体   English

没有默认按钮的MessageBox

[英]MessageBox without a default button

I have a form in my application where users can enter a list of serial numbers, each to be subjected to a quick check and then added to a list (this is for a stock take module). 我的应用程序中有一个表格,用户可以输入一个序列号列表,每个序列号都需要进行快速检查,然后添加到列表中(这是用于盘点模块的)。 So the users typically use barcode scanners to scan the serial numbers from a pile of stock items. 因此,用户通常使用条形码扫描仪从一堆存货中扫描序列号。

I'm handling the KeyPress event of the TextBox that has focus while the users are scanning items and look for e.KeyChar == 13 (the enter key). 我正在处理用户正在扫描项目并查找e.KeyChar == 13 (Enter键)时具有焦点的TextBoxKeyPress事件。 Whenever enter is pressed, I know that I have a complete serial number which I can then validate before adding it to the list. 每当按下Enter键时,我就知道我拥有完整的序列号,然后可以对其进行验证,然后将其添加到列表中。

Here's where my problem occurs; 这是我的问题发生的地方; Under certain conditions, I have to prompt the user at this point on whether he really wants the stock item added to the list or not. 在某些条件下,我必须在这一点上提示用户是否确实要将库存商品添加到列表中。 I'm using a MessageBox for that, like so: 我为此使用了一个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

But because this is something that only happens occasionally, the users often don't even see the MessageBox and simply scans the next serial number, which gets lost of course but which ends with an enter key, which fires the default button on the MessageBox and without having intended to do so, and sometimes not even aware that it's happened, the user adds an item to the list and misses a subsequent item. 但是因为这只是偶尔发生,所以用户通常甚至看不到MessageBox而只是扫描下一个序列号,该序列号当然会丢失,但以回车键结尾,从而触发MessageBox上的默认按钮,无需这样做,有时甚至不知道它发生了,用户将一个项目添加到列表中而错过了下一个项目。

Is there a way I can prevent the MessageBox from firing any buttons if enter is pressed? 有没有一种方法可以防止MessageBox在按下Enter键时触发任何按钮? 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. 我不介意用户是否继续扫描条形码并丢失所有条形码,只要MessageBox一直显示在屏幕上,直到他意识到需要注意并故意选择两个选项之一即可。

Is there a way I can prevent the MessageBox from firing any buttons if enter is pressed? 有没有一种方法可以防止MessageBox在按下Enter键时触发任何按钮?

No. 没有。

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. 我不介意用户是否继续扫描条形码并丢失所有条形码,只要MessageBox一直显示在屏幕上,直到他意识到需要注意并故意选择两个选项之一即可。

Use the YesNoCancel option, and set the default to Button3 , which will be the Cancel button. 使用YesNoCancel选项,并将默认值设置为Button3 ,这将是“ Cancel按钮。 Now keep looping while the result is Cancel . 现在,在结果为Cancel继续循环。 When the loop drops out, the user will have selected either Yes or No : 当循环退出时,用户将选择“是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

        }

---------- Edit ---------- ----------编辑----------

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). 我不太喜欢在对话框上有一个按钮的想法,除了对于一些晦涩的应用程序逻辑(从用户的角度来看)外,其他什么都没有。

Agreed...having a "noop" button isn't optimal. 同意...拥有“小睡”按钮不是最佳选择。 The solution above is a quick and dirty "fix". 上面的解决方案是一个快速而肮脏的“修复程序”。

When you get around to implementing your own custom MessageBox Form, here's an easy way to make it ignore the Enter key whenever a Button is currently focused: 当您开始实现自己的自定义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 ...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM