Results 1 to 4 of 4

Thread: [c#] Help with ElapsedEventHandler();

  1. #1
    Join Date
    Oct 2007
    Location
    #srl
    Posts
    6,102
    Mentioned
    39 Post(s)
    Quoted
    62 Post(s)

    Default [c#] Help with ElapsedEventHandler();

    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()
    c# Code:
    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;
                }
            }

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    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.


    C# Code:
    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);
        }
    }



    C# Code:
    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;
                }
            }
        }
    }
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Oct 2007
    Location
    #srl
    Posts
    6,102
    Mentioned
    39 Post(s)
    Quoted
    62 Post(s)

    Default

    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 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()?

  4. #4
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    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.
    I am Ggzz..
    Hackintosher

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •