-
Feb 6th, 2012, 03:59 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] timer on winform
Hi,
In my windows form project, every time there is a call to a long running method, I show a small form which has a label control that says, please wait and then after the long running call I hide that form . i.e.
ShowForm();
LongRunningMethod();
HideForm();
Question:
How can I make the label control to blink every second while the showForm is being shown?
Thanks
-
Feb 6th, 2012, 04:50 AM
#2
Re: timer on winform
I have to ask, why would you want to? I suggest that you display a for with "Please wait..." in the title bar and a ProgressBar with its Style set to Marquee. That is a UI that users are familiar with for that type of thing.
-
Feb 6th, 2012, 05:45 AM
#3
Thread Starter
Fanatic Member
Re: timer on winform
Do you mean, not to have a timer but instead to have a progress bar?
Note that I do not know how long the long running tasks can take?
Thanks
-
Feb 6th, 2012, 07:14 AM
#4
Re: timer on winform
Originally Posted by arkiboys
Do you mean, not to have a timer but instead to have a progress bar?
Note that I do not know how long the long running tasks can take?
Thanks
That's exactly what I mean. When I ask a question and someone says ProgressBar, Style and Marquee, the first thing I do is look up those terms. As a developer, you should always be thinking "look first, ask questions later". Having done so here would have rendered your statement (which has been curiously ended with a question mark) redundant.
-
Feb 6th, 2012, 07:43 AM
#5
Thread Starter
Fanatic Member
Re: timer on winform
If I use the progress bar, then how can the values be added to the progress bar?
I mean, I do not know how long one of the long processes may take and so not sure how to place this information on the progress bar.
Any thoughts please?
Thanks
-
Feb 6th, 2012, 09:00 AM
#6
Lively Member
Re: timer on winform
Could you just not breakdown the process into stages and once each is reached it just updates the ProgressBar?
-
Feb 6th, 2012, 09:03 AM
#7
Thread Starter
Fanatic Member
Re: timer on winform
Originally Posted by Madcat1981
Could you just not breakdown the process into stages and once each is reached it just updates the ProgressBar?
No, because say a stored procedure has to be run and each time it takes different length of time.
Thanks
-
Feb 6th, 2012, 05:53 PM
#8
Re: timer on winform
You keep asking questions that are answered in the documentation. That would be the same documentation that you should already have read on your own but you apparently still havem't read even after I told you to in post #4. Are you not reading it for a specific reason or are you just too lazy? I genuinely want to know because I'm trying to help but you're making no effort to help yourself.
-
Feb 7th, 2012, 03:06 AM
#9
Re: timer on winform
While I agree with everything jmc said, I'll still provide this incase you still want to use it.
csharp Code:
#region "BlinkLabel" public class BlinkLabel : Label { private System.Windows.Forms.Timer g_objTimer = new System.Windows.Forms.Timer(); private int g_iBlinkTime = 0; private bool g_bVisible = true; private bool g_bIsBlinking = false; private bool g_bDefaultVisible = true; [System.ComponentModel.DefaultValue(true)] public new bool Visible { get { return g_bDefaultVisible; } set { g_bDefaultVisible = value; if (base.DesignMode == true) { base.Visible = true; } else { base.Visible = value; } if (g_bDefaultVisible == true & g_bIsBlinking == true) { g_objTimer.Interval = g_iBlinkTime; g_objTimer.Enabled = true; //Remove any previous handlers g_objTimer.Tick -= Elapsed; g_objTimer.Tick += Elapsed; } if (g_bDefaultVisible == false & g_bIsBlinking == true) { g_objTimer.Enabled = false; g_objTimer.Tick -= Elapsed; } } } [System.ComponentModel.DefaultValue(0)] public int BlinkTime { get { return g_iBlinkTime; } set { g_iBlinkTime = value; if (base.DesignMode == false) { if (g_iBlinkTime != 0) { //If the control's visible property is false //go into blink mode but dont show the control if (g_bDefaultVisible == true) { g_objTimer.Interval = g_iBlinkTime; g_objTimer.Enabled = true; g_objTimer.Tick += Elapsed; } g_bIsBlinking = true; } else { g_objTimer.Enabled = false; g_objTimer.Tick -= Elapsed; base.Visible = g_bDefaultVisible; g_bIsBlinking = false; } } } } private void Elapsed(object sender, System.EventArgs e) { if (g_bVisible) g_bVisible = false; else g_bVisible = true; base.Visible = g_bVisible; } } #endregion
Its a BlinkLabel I wrote some months back for the exact same reason that you want it. I used it for a "Please wait" message while waiting for an SMO query for database instances to complete. Just use it like a normal label and set its BlinkTime property to something greater than zero for it to start blinking. I suggest 500(which is 500 milliseconds). Anything else seems too slow or too fast but then thats a matter of preference.
Trivia: This was written in VB.Net....just used an online converter to get C# code.
Last edited by Niya; Feb 7th, 2012 at 03:08 AM.
Reason: Trivia
-
Feb 7th, 2012, 11:51 AM
#10
Thread Starter
Fanatic Member
Re: timer on winform
Originally Posted by jmcilhinney
You keep asking questions that are answered in the documentation. That would be the same documentation that you should already have read on your own but you apparently still havem't read even after I told you to in post #4. Are you not reading it for a specific reason or are you just too lazy? I genuinely want to know because I'm trying to help but you're making no effort to help yourself.
Solved and Thank you
-
Feb 7th, 2012, 11:52 AM
#11
Thread Starter
Fanatic Member
Re: timer on winform
Originally Posted by Niya
While I agree with everything jmc said, I'll still provide this incase you still want to use it.
csharp Code:
#region "BlinkLabel"
public class BlinkLabel : Label
{
private System.Windows.Forms.Timer g_objTimer = new System.Windows.Forms.Timer();
private int g_iBlinkTime = 0;
private bool g_bVisible = true;
private bool g_bIsBlinking = false;
private bool g_bDefaultVisible = true;
[System.ComponentModel.DefaultValue(true)]
public new bool Visible {
get { return g_bDefaultVisible; }
set {
g_bDefaultVisible = value;
if (base.DesignMode == true) {
base.Visible = true;
} else {
base.Visible = value;
}
if (g_bDefaultVisible == true & g_bIsBlinking == true) {
g_objTimer.Interval = g_iBlinkTime;
g_objTimer.Enabled = true;
//Remove any previous handlers
g_objTimer.Tick -= Elapsed;
g_objTimer.Tick += Elapsed;
}
if (g_bDefaultVisible == false & g_bIsBlinking == true) {
g_objTimer.Enabled = false;
g_objTimer.Tick -= Elapsed;
}
}
}
[System.ComponentModel.DefaultValue(0)]
public int BlinkTime {
get { return g_iBlinkTime; }
set {
g_iBlinkTime = value;
if (base.DesignMode == false) {
if (g_iBlinkTime != 0) {
//If the control's visible property is false
//go into blink mode but dont show the control
if (g_bDefaultVisible == true) {
g_objTimer.Interval = g_iBlinkTime;
g_objTimer.Enabled = true;
g_objTimer.Tick += Elapsed;
}
g_bIsBlinking = true;
} else {
g_objTimer.Enabled = false;
g_objTimer.Tick -= Elapsed;
base.Visible = g_bDefaultVisible;
g_bIsBlinking = false;
}
}
}
}
private void Elapsed(object sender, System.EventArgs e)
{
if (g_bVisible)
g_bVisible = false;
else
g_bVisible = true;
base.Visible = g_bVisible;
}
}
#endregion
Its a BlinkLabel I wrote some months back for the exact same reason that you want it. I used it for a "Please wait" message while waiting for an SMO query for database instances to complete. Just use it like a normal label and set its BlinkTime property to something greater than zero for it to start blinking. I suggest 500(which is 500 milliseconds). Anything else seems too slow or too fast but then thats a matter of preference.
Trivia: This was written in VB.Net....just used an online converter to get C# code.
Thank you
-
Feb 7th, 2012, 06:40 PM
#12
Re: [RESOLVED] timer on winform
Also, if you're gonna use my BlinkLabel or even a marquee progress bar as jmc suggested, make sure that your long running method doesnt lock up the UI. Use timers or a different thread or even DoEvents so that the control, which ever one you're gonna use can redraw themselves.
-
Feb 8th, 2012, 11:43 AM
#13
Thread Starter
Fanatic Member
Re: timer on winform
Originally Posted by Niya
While I agree with everything jmc said, I'll still provide this incase you still want to use it.
csharp Code:
#region "BlinkLabel"
public class BlinkLabel : Label
{
private System.Windows.Forms.Timer g_objTimer = new System.Windows.Forms.Timer();
private int g_iBlinkTime = 0;
private bool g_bVisible = true;
private bool g_bIsBlinking = false;
private bool g_bDefaultVisible = true;
[System.ComponentModel.DefaultValue(true)]
public new bool Visible {
get { return g_bDefaultVisible; }
set {
g_bDefaultVisible = value;
if (base.DesignMode == true) {
base.Visible = true;
} else {
base.Visible = value;
}
if (g_bDefaultVisible == true & g_bIsBlinking == true) {
g_objTimer.Interval = g_iBlinkTime;
g_objTimer.Enabled = true;
//Remove any previous handlers
g_objTimer.Tick -= Elapsed;
g_objTimer.Tick += Elapsed;
}
if (g_bDefaultVisible == false & g_bIsBlinking == true) {
g_objTimer.Enabled = false;
g_objTimer.Tick -= Elapsed;
}
}
}
[System.ComponentModel.DefaultValue(0)]
public int BlinkTime {
get { return g_iBlinkTime; }
set {
g_iBlinkTime = value;
if (base.DesignMode == false) {
if (g_iBlinkTime != 0) {
//If the control's visible property is false
//go into blink mode but dont show the control
if (g_bDefaultVisible == true) {
g_objTimer.Interval = g_iBlinkTime;
g_objTimer.Enabled = true;
g_objTimer.Tick += Elapsed;
}
g_bIsBlinking = true;
} else {
g_objTimer.Enabled = false;
g_objTimer.Tick -= Elapsed;
base.Visible = g_bDefaultVisible;
g_bIsBlinking = false;
}
}
}
}
private void Elapsed(object sender, System.EventArgs e)
{
if (g_bVisible)
g_bVisible = false;
else
g_bVisible = true;
base.Visible = g_bVisible;
}
}
#endregion
Its a BlinkLabel I wrote some months back for the exact same reason that you want it. I used it for a "Please wait" message while waiting for an SMO query for database instances to complete. Just use it like a normal label and set its BlinkTime property to something greater than zero for it to start blinking. I suggest 500(which is 500 milliseconds). Anything else seems too slow or too fast but then thats a matter of preference.
Trivia: This was written in VB.Net....just used an online converter to get C# code.
Hi,
Created a class inside winform project.
Named it BlinkLabel which inherits from Label.
Question:
How do I use this for a form which has a label and needs to blink?
Thanks
-
Feb 8th, 2012, 07:33 PM
#14
Re: timer on winform
Originally Posted by arkiboys
Hi,
Created a class inside winform project.
Named it BlinkLabel which inherits from Label.
Question:
How do I use this for a form which has a label and needs to blink?
Thanks
It's a control. You use it like any other control. You use it INSTEAD of a regular Label.
-
Feb 9th, 2012, 08:13 AM
#15
Thread Starter
Fanatic Member
Re: [RESOLVED] timer on winform
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|