16
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

4 回答 4

21

改变这个

MessageBoxDefaultButton.Button1);

对此

MessageBoxDefaultButton.Button2);
于 2013-06-10T11:48:36.663 回答
16

将消息框更改为:

DialogResult result = MessageBox.Show(
    "Are there any other products in the carton?",
    "Question",
    buttons,
    MessageBoxIcon.Question,
    MessageBoxDefaultButton.Button2
);
于 2013-06-10T11:49:29.677 回答
5

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

于 2013-06-10T11:50:36.717 回答
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("Message", "Caption", MessageBoxButton.YesNo, MessageBoxImage.Stop, MessageBoxResult.No);

于 2018-01-21T13:52:07.810 回答