Change status bar text back to default 5 seconds after every change?

  • Thread starter Thread starter sherifffruitfly
  • Start date Start date
S

sherifffruitfly

Hi,

I'm using StatusBar text to inform the user what's going on in the
application. I pass a reference to from the main form's class to other
(instances of) classes that are doing the actual work, and they update
the Text property in various ways.

It would be slick (imo) if, after the user is informed about the antics
of some class-instance, if the StatusBar text would revert back to some
default text-variable in the main form class after some specified
interval of time (like 5 seconds).

Problem is, that I can't use the StatusBar's OnChange event (or
whatever's it's called) - if I did, then every time the "reversion" was
perform, the event would fire again.

So how would I go about implementing this? Even just a skeleton
code-example would be helpful.


Thanks for any ideas,

cdj
 
Have not tried but you might be able to over load the status bar class and
add you on update event to change the text, something like SetTextTimed.
Save the old text, set a timer, and when the timer fired revert to the saved
text. You can debug it.

public class MyStatusBar : StatusBar
{
private string OldText = string.Empty;
private System.Windows.Forms.Timer timer;

public MyStatusBar()
: base()
{
timer = new System.Windows.Forms.Timer();
timer.Tick += new EventHandler(timer_Tick);
}

void timer_Tick(object sender, EventArgs e)
{
this.text = this.OldText;
}

public void SetTextTimed(string text)
{
this.timer.Stop();
this.OldText = this.Text;
this.Text = text;
this.timer.Interval = 5000;
this.timer.Start();
}
}

Regards,
John
 
cdj,

Create a custom control. Inside it, do what you're currently doing. I
assume you're doing something like using OnChange to start a timer and
using the timer's callback to reset the status bar's data. To prevent
the endless loop, add a boolean member variable. Try this:

public class myControl : StatusBar
{

private boolean bDefault = true;
private Timer timeReset = new Timer(...);

public void OnChange(...)
{
if (!this.bDefault)
this.timeReset.Reset();
}

public void OnTimerTick(...)
{
this.bDefault = true;
// set text back to default
Text = "Ready.";
}

public void SetText(string text)
{
this.bDefault = false;
Text = text;
}

}

I don't guarantee this code compiles, it's more to demonstrate a point.
You probably want to use a property override instead of SetText(), but
I wrote it that way for clarity. Also, I've not dealt with thread
safety.


Stephan
 
I don't guarantee this code compiles, it's more to demonstrate a point.
You probably want to use a property override instead of SetText(), but
I wrote it that way for clarity. Also, I've not dealt with thread
safety.


Stephan

Stephan - thanks for the approach - I'll give it a shot!

So I get the idea that, in general terms, when you want a control to
have a lil more functionality than it currently has, you just derive
off of it, and code up the functionality you're after - is that right
in general?

Thanks again,

cdj
 
cdj,

Yes, that's the general idea. It's neater if you encapsulate it in a
new control. Even better, it makes it so much easier to use it in your
other forms and projects.

Another way to do this without inheriting is to create a new control,
place a status bar in it, and build a fascade for it. That gives much
more control over the status bar, but also forces you to implement a
whole bunch of stuff yourself. Which implementation is best for you
usually depends on things like time and budget, although sometimes also
just on your personal interest in going through the exercise.


Stephan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top