• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Custom Width Message Box per Label Length?

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";  
      msg.Show();
      Thread.Sleep(3000);
      msg.Close();
0
kvnsdr
Asked:
kvnsdr
  • 3
1 Solution
 
vo1dCommented:
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.
0
 
kvnsdrAuthor Commented:
That sounds like a great idea.

Any idea why the message form doesn't expand to the correct width?
0
 
vo1dCommented:
yes, do you want your message shown in one line or in multiline?
0
 
vo1dCommented:
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)
        {
            InitializeComponent();                        
            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)
        {            
            this._timer.Start();
        }
       
        void TimerTickHandler(object sender, EventArgs e)
        {
            this.Close();
        }
    }

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);
frm.ShowDialog();


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

to

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
0
 
Bob LearnedCommented:
WshShell.Popup:

http://www.ss64.com/wsh/popup.html

Notice the 'nSecondsToWait'.

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

Bob
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now