C # Custom message box Landscaping step

Source: Internet
Author: User

Windows with the message box is ugly, especially to go to WIN7 under more, the project used in the message box is very much, basically has the hint information, the error message, inquires the information these three kinds, from the esthetic angle, MessageBox.Show () This method comes out the message box to be somewhat unsatisfactory, So I wrote one myself and felt more comfortable with myself.

Description: The following naming ignores case.

Custom Info Box

System Information Box

Customize the Query box

System Inquiry Box

Step 1:

Create a new form Frmmessagebox, place a PictureBox (Picico) control and a label (Labinfo) control and two panel controls, where a button is placed in the Panel1, named Btnok, and the Text property is determined ( &o), the DialogResult property is set to OK, In this case, the end of the dialog will return to DialogResult.OK, here in order to be able to adapt to the length of the message to change the width of the button and the width of the dialog box, deliberately put two panel, the other panel placed two buttons, one is, a No.

In this way, I believe you already know the use of these controls, picture frame used to display the corresponding picture, information box is a big tick, ask box is a big question mark, in advance to add resources to the project, the proposed picture file name best info.png/ Error.png such, the time to take resources to find good, label used to display messages.

Step 2:

Define enumerated types, there are three of them defined here, and you can add them yourself.

public enum Messageboxstyle






Step 3:

overriding constructors

Public Frmmessagebox (Messageboxstyle messageboxstyle,string msg)


InitializeComponent ();

if (Messageboxstyle = = Messageboxstyle.info)


Picico.image = Global::myalarmsystem.properties.resources.info;

This. Text = "hint";

Panel1. Visible = true;

Panel2. Visible = false;


else if (Messageboxstyle = = messageboxstyle.question)


Picico.image = global::myalarmsystem.properties.resources.question;

This. Text = "inquiry";

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 ("XXFarEastFont-Arial", 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;


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.