Customize the message in the System.Windows.Forms.Form MessageBox
阿新 • • 發佈:2020-08-13
public class MsgBox : System.Windows.Forms.Form { Label bodyLabel = new Label(); Button yesBtn = new Button(); Button noBtn = new Button(); public MsgBox() { } public MsgBox(string titleMsg, string bodyMsg, string yesMsg,string noMsg) {this.ClientSize = new System.Drawing.Size(490, 150); this.Text = titleMsg; yesBtn.Location = new System.Drawing.Point(111, 112); yesBtn.Size = new System.Drawing.Size(75, 23); yesBtn.Text = yesMsg; yesBtn.BackColor = Control.DefaultBackColor; yesBtn.Click+= YesBtnClick; noBtn.Location = new System.Drawing.Point(311, 112); noBtn.Size = new System.Drawing.Size(75, 23); noBtn.Text = noMsg; noBtn.BackColor = Control.DefaultBackColor; noBtn.Click += NoBtnClick; bodyLabel.Location = newSystem.Drawing.Point(111, 50); bodyLabel.Text = bodyMsg; bodyLabel.Font = Control.DefaultFont; bodyLabel.AutoSize = true; this.BackColor = Color.White; this.ShowIcon = false; this.Controls.Add(noBtn); this.Controls.Add(yesBtn); this.Controls.Add(bodyLabel); } private void YesBtnClick(object sender, EventArgs e) { this.DialogResult = DialogResult.Yes; } private void NoBtnClick(object sender, EventArgs e) { this.DialogResult = DialogResult.No; } }
static void CustomizeMsgBoxDemo() { MsgBox msgBoxForm = new MsgBox( "Warning", "Is it right!", "Right!", "Wrong!" ); msgBoxForm.FormBorderStyle = FormBorderStyle.FixedSingle; msgBoxForm.MaximizeBox = false; msgBoxForm.MinimizeBox = false; msgBoxForm.StartPosition = FormStartPosition.CenterParent; DialogResult dlgResult=msgBoxForm.ShowDialog(); if(dlgResult==DialogResult.Yes) { MessageBox.Show("Right!"); } else { MessageBox.Show("Wrong!"); } }
Copy and update from
https://stackoverflow.com/questions/12704026/how-to-change-text-on-messagebox-buttons