PDA

View Full Version : [c#] Help with ElapsedEventHandler();



NCDS
10-03-2012, 03:56 AM
The following functions are from a little side project I mess with from time to time; mainly for learning purposes. The goal here is have progressBar1.Style set to Marquee while the files are being parsed, then switch back to Style.Blocks (value = 0) to "kill" the progress bar as the process has finished.

The progress bar "activates" and switches to Marquee with no problem at all. However, the style is never switched back to Blocks, even though the files are parsed correctly. Any help?

Note: Time.Enabled is set in parseFile()

private void progressBar1_Click(object sender, EventArgs e)
{
// To ensure the progress bar has adequate time to display progress before execution is complete.
Time = new System.Timers.Timer(10000);
//Time.AutoReset = false;
Time.Elapsed += new ElapsedEventHandler(onTimeEvent);
Time.Interval = 2000;

// Init progress bar 'process'
progressBar1.Style = ProgressBarStyle.Marquee;

if (!parseFile())
{
MessageBox.Show("Unable to parse file @ " + cDir, "Error!");
}

}
//

private void onTimeEvent(object source, ElapsedEventArgs e)
{
//if (parse) //This is just a variable to ensure the respective files have been parsed.
{
progressBar1.Style = ProgressBarStyle.Blocks;
Time.Enabled = false;
}
}

Brandon
10-03-2012, 05:04 AM
Styling Information:

The ForeColor and Backcolor properties have an effect only if
visual styles are disabled for the system or the application.

Style property:
- Visual styles disabled:
Continuous and Blocks are available. Marquee is the same as Blocks.

- Visual styles enabled:
Marquee and Continuous is available. Blocks is the same as Continuous.

It could be because of Visual Styles? Also I do not see a Elapsed Event handler :S Is it not Tick? The only Event for the TimerControl for me is the Tick Event.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForm
{
public class ProgressBarCustom : ProgressBar
{
public ProgressBarCustom()
{
this.Style = ProgressBarStyle.Blocks;
this.Visible = true;
}

public ProgressBarCustom(ProgressBarStyle Style)
{
this.Style = Style;
this.Visible = true;
}

/* Un-comment to disable Theming of the Bar..
protected override void CreateHandle()
{
base.CreateHandle();
try
{
SetWindowTheme(this.Handle, "", "");
}
catch
{
}
}
*/

[System.Runtime.InteropServices.DllImport("uxtheme.dll")]
private static extern int SetWindowTheme(IntPtr hwnd, string appname, string idlist);
}
}





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
//timer1.Stop();
//timer1.Enabled = false;
progressBarCustom1.PerformStep();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void progressBarCustom1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 5000;
timer1.Start();
progressBarCustom1.Step = 10;

if (progressBarCustom1.Style == ProgressBarStyle.Marquee)
{
progressBarCustom1.Style = ProgressBarStyle.Blocks;
}
else
{
progressBarCustom1.Style = ProgressBarStyle.Marquee;
}
}
}
}

NCDS
10-03-2012, 07:31 PM
I have visual styles enabled, so that shouldn't be a problem.

I know what you mean with only having the Tick event on the TimerControl, but this (http://msdn.microsoft.com/en-us/library/system.timers.elapsedeventargs.aspx) is what I'm using.

It's getting late now, but toomorrow I'll try and just create a custom prgoressbar as you demonstrated.

Thanks Brandon.

Edit: Got it working by just switching to using the Tick event. I',m not sure what's up with the examples on msdn, but many of them seem not to work...

Also, in your example, what exactly is the difference between timer.Enabled and timer.Start()?

Brandon
10-03-2012, 07:42 PM
Strictly speaking, timer.start sets enabled to true and starts the timer. Vice-versa for stop.

The difference is that enabled can be used as a parameter to an if statement to check if it's already running.