msgbox C#中的默认值为No

时间:2013-06-10 11:46:55

标签: c# .net winforms

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();
            }

当消息框出现时,“是”按钮会突出显示。我希望“否”按钮突出显示。因此默认为“否”。

我该怎么做?

4 个答案:

答案 0 :(得分:19)

更改此

MessageBoxDefaultButton.Button1);

到这个

MessageBoxDefaultButton.Button2);

答案 1 :(得分:13)

将消息框更改为:

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

答案 2 :(得分:4)

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

答案 3 :(得分:0)

main.o:main.cpp:(.text+0x1e): undefined reference to '__imp__ZN2sf5ColorC1Ev'

  

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

defaultResult MessageBoxResult

SampleCode:

[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);