Custom Width Message Box per Label Length?

Posted on 2006-07-07
Medium Priority
Last Modified: 2011-10-03
I have two problems:

1. My text does not show on my message box, I found the Thread(sleep) is a problem.....

2. I would like to base the winform (custom message box) on the varrying label length...

Leave it to microsoft to NOT allow for a more flexable MessageBox.Show.
As if thier thousands of programmers have never needed to auto-close one.

Anyway, here's what I have so far:

[Messages form]
      private void Messages_Load(object sender, EventArgs e)
            this.Width = lblMessage.Text.Length;   // always too narrow, cuts-off half of label.

[Form1] calling Messages form to open....

      Messages msg = new Messages();            
      msg.lblMessage.Text = "Thank You For Choosing Us";  
Question by:kvnsdr
  • 3
LVL 11

Expert Comment

ID: 17063029
implement a timer in your Messages form, which is started just after been shown.
if its count down from 3 seconds, close your Messages form.

Author Comment

ID: 17063550
That sounds like a great idea.

Any idea why the message form doesn't expand to the correct width?
LVL 11

Expert Comment

ID: 17064318
yes, do you want your message shown in one line or in multiline?
LVL 11

Accepted Solution

vo1d earned 1000 total points
ID: 17064417
here is an example, how you can do it. the message is in one line:

public partial class MessageForm : Form
        private Timer _timer = new Timer();
        public MessageForm(string Message, int showTime)
            this.FormBorderStyle      = FormBorderStyle.FixedSingle;
            this._messageLabel.Text = Message;
            this.ClientSize                = new Size(this._messageLabel.Width + 8, this._messageLabel.Height + 8);
            this._timer.Interval         = showTime;
            this._timer.Tick            += new EventHandler(TimerTickHandler);            

        private void MessageForm_Load(object sender, EventArgs e)
        void TimerTickHandler(object sender, EventArgs e)

the messageLabel has the location 4, 4.

in your mainform, you will just have to do this:

MessageForm frm = new MessageForm("Thank You For Choosing Us", 3000);

if you wanna display short messages, you will have to change the line:
this.FormBorderStyle      = FormBorderStyle.FixedSingle;


this.FormBorderStyle      = FormBorderStyle.FixedToolWindow;

otherwise, the window will not size down  its width to the label width on short messages because of the caption bars control boxes.

regards, vo1d
LVL 96

Expert Comment

by:Bob Learned
ID: 17065179


Notice the 'nSecondsToWait'.

1) Add a COM reference to 'Windows Script Host Object Model'
2) Instantiate a WshShell object
3) Call the Popup method


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question