Windows built-in message boxes are ugly, especially when switched to Windows 7. There are many message boxes in the project. There are basically three types of message boxes: prompt information, error information, and inquiry information, messageBox. the show () method is a bit unsatisfactory, so I wrote one myself, and I felt more comfortable.
Note: The following naming conventions are case-insensitive.
Custom information box
System Information Box
Custom query box
System inquiry box
Step 1:
Create a new form frmMessageBox and place a picturebox (picICO) control, a label (labinfo) control, and two panel controls. Put a button in panel1 and name it btnOk, the text attribute is OK (& O), and The dialogresult attribute is set to OK. In this way, the dialogresult is returned after the dialog box is closed. OK. Here, we want to change the button width and dialog box width to adapt to the message length. We want to place two panels, one for the other, and one for the other.
For example, logs/error.png, you can find the resource at that time. The label is used to display messages.
Step 2:
Defines enumeration types. Three types are defined here. You can also add them by yourself.
Public enum MessageBoxStyle
{
Info = 0,
Question = 1,
Error = 2
};
Step 3:
Override Constructor
Public frmMessageBox (MessageBoxStyle messageBoxStyle, string msg)
{
InitializeComponent ();
If (messageBoxStyle = MessageBoxStyle.info)
{
PicICO. Image = global: myAlarmSystem.Properties.Resources.info;
This. Text = "prompt ";
Panel1.Visible = true;
Panel2.Visible = false;
}
Else if (messageBoxStyle = MessageBoxStyle. question)
{
PicICO. Image = global: myAlarmSystem. Properties. Resources. question;
This. Text = "";
Panel1.Visible = false;
Panel2.Visible = true;
}
Else if (messageBoxStyle = MessageBoxStyle. error)
{
PicICO. Image = global: myAlarmSystem. Properties. Resources. error;
This. Text = "error ";
Panel1.Visible = true;
Panel2.Visible = false;
}
This. labInfo. Text = msg;
SizeF size = TextRenderer. MeasureText (msg, new Font ("", 15, FontStyle. Regular ));
Int TempWidth = (int) size. Width;
If (TempWidth <= 249) {return ;}
This. Width = (int) size. Width + 130;
This. panel1.Width = TempWidth-20;
This. panel2.Width = TempWidth-20;
BtnYes. Width = TempWidth/2-20;
BtnNo. Width = TempWidth/2-20;
}