在C#的MsgBox中,默认为No

17
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result = MessageBox.Show("Are there any other products in the carton?", "Question", buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

            if (result == DialogResult.Yes)
            {
                trans.Rollback();
                MessageBox.Show("Please go to the controll room for new packaging", "Message");
                frmHome main = new frmHome(empid);
                main.Show();
                this.Hide();
            }

            if (result == DialogResult.No)
            {
                trans.Commit();
                frmPalletCartonAllocation pca = new frmPalletCartonAllocation(pack, companyIdNo, skuIdNo, UnitsInCarton, UnitsInPack, carton_Code, orderNo, grvIdNo, empid);
                pca.Show();
                this.Hide();
            }

当消息框出现时,“是”按钮会被突出显示。我希望改为“否”按钮被突出显示。所以默认选项是“否”。

我该怎么做?


我猜它有一个重载函数,可以使用MessageBox默认按钮。 - V4Vendetta
4
在你的代码中有MessageBoxDefaultButton就足以提示了。没有付出任何努力,扣1分。 - user247702
@V4Vendetta 他已经在使用那个重载了,只需要更改枚举值即可。 - psubsee2003
@psubsee2003 对不起,我太粗心了,没有滚动到最后。 - V4Vendetta
1
请勿使用 if...if 块,而是使用 if...else if... 块,因为只有一个条件会为真。 - NeverHopeless
4个回答

22

改变这个

MessageBoxDefaultButton.Button1);

到这里

MessageBoxDefaultButton.Button2);

17

将消息框更改为:

DialogResult result = MessageBox.Show(
    "Are there any other products in the carton?",
    "Question",
    buttons,
    MessageBoxIcon.Question,
    MessageBoxDefaultButton.Button2
);

你应该尽力将消息框的父窗体放在第一个重载中。如果不这样做,消息框可能会落后于窗体,导致应用程序在用户屏幕上冻结,他们将不得不通过任务管理器来结束应用程序...MessageBox.Show(Form.... - Erik Schroder

5

将方法的MessageBoxDefaultButton参数更改为MessageBoxDefaultButton.Button2。


0

defaultResult MessageBoxResult

一个MessageBoxResult值,指定消息框的默认结果。

[System.Security.SecurityCritical]
public static System.Windows.MessageBoxResult Show(string messageBoxText, string caption, System.Windows.MessageBoxButton button, System.Windows.MessageBoxImage icon, System.Windows.MessageBoxResult defaultResult);

示例代码:

MessageBox.Show("消息", "标题", MessageBoxButton.YesNo, MessageBoxImage.Stop, MessageBoxResult.No);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接