C # customize message frame beautification

Source: Internet
Author: User

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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.