Click here to Skip to main content
Licence CPOL
First Posted 17 Feb 2008
Views 14,811
Downloads 97
Bookmarked 10 times

Scrolling Message Bar Control

By | 17 Feb 2008 | Article
A message bar control that scrolls the text if it's too much to display all at once.

Introduction

While discussing how irritating a message box is displaying error messages and warnings (to us at least), I decided to write a simple control to display any relevant information to the user.

Background

The control was going to be very simple, just a label docked at the top of the window, with some properties to change the background color and message. But after using it in our app for a while, I realized most of our error messages were longer than the window! So, I set out to make it scroll.

Using the code

In a nutshell, the code works like this...

As the control is constructed, a timer starts, and status and message are set to "normal" and "", respectively. Every time the label is repainted (every time it changes), the event checks if the string actually fits in the label or not, and sets a flag accordingly. When the timer ticks, it checks the flag set by the Paint event, and if true, creates a new string, taking out the first char and saving it in a temporary variable, then adds the rest of the string to the new string variable, adding the char that used to be the first, last.

public partial class MessageBar : UserControl
{
    private string status = "";
    private string message = "";
    char charTemp;
    string tempMessage = "";
    string myMessage = "";
    bool scrollFlag = false;

    public StatusBar()
    {
        InitializeComponent();
        timer1.Start();
        this.Status = "normal";
        this.Message = "";
    }
    public void scrollText()
    {
        if (scrollFlag == true)
        {
            myMessage = lblMessage.Text;
            if ((myMessage.Length == 0) != true)
            {
                charTemp = myMessage[0];
                for (int i = 0; i < myMessage.Length - 1; i++)
                {
                    tempMessage += myMessage[i + 1];
                }
                tempMessage += charTemp;
                Message = tempMessage;
                tempMessage = "";
            }
        }
    }
    public string Status
    {
        get
        {
            return status;
        }
        set
        {
            status = value;
            if (status == "error")
            {
                lblMessage.BackColor = System.Drawing.Color.Red;
                lblMessage.ForeColor = System.Drawing.Color.White;
            }
            else if (status == "warning")
            {
                lblMessage.BackColor = System.Drawing.Color.Yellow;
                lblMessage.ForeColor = System.Drawing.Color.Black;
            }
            else if (status == "normal")
            {
                lblMessage.BackColor = System.Drawing.SystemColors.Control;
                lblMessage.ForeColor = System.Drawing.Color.Black;
            }
            else
            {
                lblMessage.BackColor = System.Drawing.Color.Black;
                lblMessage.ForeColor = System.Drawing.Color.White;
                lblMessage.Text = "An Internal Error Has Occured (StatusBar.Status)";
            }
        }
    }
    public string Message
    {
        get
        {
            return message;
        }
        set
        {
            message = value;
            lblMessage.Text = message;
        }
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        scrollText();
    }
    private void lblMessage_Paint(object sender, PaintEventArgs e)
    {
        if (lblMessage.Width < e.Graphics.MeasureString(lblMessage.Text, 
                                                           lblMessage.Font).Width)
        {
            scrollFlag = true;
        }
        else
        {
            scrollFlag = false;
        }
    }
}

History

None... yet!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Harvey Saayman

Software Developer
Osiris Trading
South Africa South Africa

Member

Harvey is in the process of completing his BSc in Information Systems Engineering. He finished the 1st two years in less than a year, he is currently doing the last year part time over two years while working full time as a C# developer. The modules he is currently studying covers business & project management.
 
He is getting his degree from Cambridge university UK, all though hes studying at CTi South Africa.
 
He has been a programmer in professional capacity since Jan 2008, but has been exposed to programming from childhood.
 
He currently works for Uniclox Technologies (Pty) Ltd as the lead project developer. Hes a junior on paper, but more senior developers who has seen his work confirmed that his skill level is way beyond that of a junior.
 
Other interests include music, he has been playing guitar for about four years now. He wishes he can afford a proper 7 string ibanez.
 
Harvey's pet of choice isn't of your common 4 legged variety, he prefers reptiles and currently owns a South African brown house snake and an Australian breaded dragon.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSuggestions! PinmvpMartin#0:15 19 Feb '08  
GeneralRe: Suggestions! PinmemberHarveySaayman0:49 19 Feb '08  
GeneralRe: Suggestions! PinmemberHarveySaayman23:08 24 Feb '08  
GeneralRe: Suggestions! PinmvpMartin#23:26 24 Feb '08  
GeneralRe: Suggestions! PinmemberHarveySaayman23:50 24 Feb '08  
GeneralRe: Suggestions! PinmvpMartin#1:39 25 Feb '08  
GeneralRe: Suggestions! PinmemberHarveySaayman1:50 25 Feb '08  
GeneralRe: Suggestions! PinmvpMartin#2:01 25 Feb '08  
GeneralRe: Suggestions! PinmemberHarveySaayman2:11 25 Feb '08  
GeneralRe: Suggestions! PinmemberHarveySaayman23:22 24 Feb '08  
GeneralRe: Suggestions! PinmvpMartin#0:05 25 Feb '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 17 Feb 2008
Article Copyright 2008 by Harvey Saayman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid