MessageBoxButtons
是一個枚舉類型,它用于表示在消息框中顯示的按鈕集合。這個枚舉類型通常與 MessageBox
類一起使用,以創建和顯示消息框。MessageBoxButtons
可以與其他 UI 組件協同工作,以便在用戶與應用程序交互時提供反饋或確認操作。
以下是一些示例,說明如何將 MessageBoxButtons
與其他 UI 組件協同工作:
DialogResult
屬性,可以在按鈕點擊時自動關閉消息框并返回一個結果。例如:MessageBoxButton buttons = MessageBoxButtons.OKCancel;
DialogResult result = MessageBox.Show("Are you sure?", "Confirmation", buttons);
if (result == DialogResult.OK)
{
// 用戶點擊了 OK 按鈕
MessageBox.Show("OK button clicked.");
}
else if (result == DialogResult.Cancel)
{
// 用戶點擊了 Cancel 按鈕
MessageBox.Show("Cancel button clicked.");
}
MessageBox
控件,并根據用戶的響應執行相應的操作。例如,在一個登錄對話框中,可以使用 MessageBox
控件來確認用戶名和密碼是否正確:string username = txtUsername.Text;
string password = txtPassword.Text;
bool isValidUser = CheckUserCredentials(username, password);
if (isValidUser)
{
MessageBox.Show("Login successful!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// 跳轉到主界面或其他邏輯
}
else
{
MessageBox.Show("Invalid username or password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Panel
控件創建一個自定義的消息框,并在其中添加 Button
控件來關閉消息框:Panel customMessageBox = new Panel();
customMessageBox.BorderStyle = BorderStyle.FixedSingle;
customMessageBox.Size = new Size(300, 100);
customMessageBox.TextAlign = ContentAlignment.MiddleCenter;
Label messageLabel = new Label();
messageLabel.Text = "Are you sure?";
messageLabel.AutoSize = true;
messageLabel.Location = new Point(150, 20);
Button okButton = new Button();
okButton.Text = "OK";
okButton.DialogResult = DialogResult.OK;
okButton.Click += new EventHandler(okButton_Click);
okButton.Location = new Point(100, 60);
customMessageBox.Controls.Add(messageLabel);
customMessageBox.Controls.Add(okButton);
DialogResult result = MessageBox.Show(customMessageBox, "Confirmation");
if (result == DialogResult.OK)
{
// 用戶點擊了 OK 按鈕
}
這些示例展示了如何將 MessageBoxButtons
與其他 UI 組件協同工作,以創建交互式的用戶界面并提供有用的反饋。