C # custom message box

Source: Internet
Author: User

I haven't written a blog for more than a year. I really admire my brother in the garden for having a new blog every week! I just got free these days. I reorganized my projects that I have worked on over the past three years. I read the project code last year and thought it was very spam. I read the code from last year, I also think that garbage can be optimized and reused in many places. I even found several significant bugs in a core project. Well, I have sold more than 500 sets, currently, no customer has reflected this bug. I have been engaged in security industry programming, from VB to VB.. net, from VC to QT, from C # desktop development to C # embedded development, and to multiple languages, encapsulate frequently-used processing in many projects for future use, especially a few private activities recently. If we use encapsulated frequently-used processing for development, the estimation speed will be much faster, the efficiency will also be improved a lot. It may take weeks to integrate common processing and custom controls into a project.

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 plus 130; this. panel1.Width = TempWidth-20; this. panel2.Width = TempWidth-20; btnYes. width = TempWidth/2-20; btnNo. width = TempWidth/2-20 ;}

 

The length is limited here. If the length is less than 249, the width of the form and the button width remain unchanged.

Code download: http://files.cnblogs.com/feiyangqingyun/myAlarmSystem20130526.zip

 

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.