Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Please Don't blame me. I'm New New Newbie for Programming and Self Learning..
so........... anyway Please Help.


I need to pass Progressbar ID as Parameter on an Timer_tick event.
I have 3 Progressbars(Progressbar1, Progressbar2, Progressbar3), 3 buttons(Button1, Button2, Button3) and 1 timer(Timer1) in project.
Only I need to configure Timer1 for each 3 buttons. Like this.

below is normal configuration.

VB
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
       Timer1.Interval = 1000
       ProgressBar1.Increment(incre)
   End Sub


in this case I can code only for Button1 (because the button1 is related to Progressbar1)
If I can Create Progressbar1 as a parameter. I can use Timer1 for every button. like this.

VB
Private Sub Timer1_Tick(ProgressVar as Progressbar ,sender As Object, e As EventArgs) Handles Timer1.Tick
       Timer1.Interval = 1000
       ProgressVar.Increment(incre)
   End Sub

If I can code Buttons as this.

VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Red_BTN.Click
       Timer1_Tick(ProgressBar1)
       Timer1.Start()
   End Sub


and Button2 as this

VB
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Red_BTN.Click
       Timer1_Tick(ProgressBar2)
       Timer1.Start()
   End Sub
Posted
Comments
[no name] 9-May-14 11:45am    
No you cannot do that. But we have no idea what it is that you are trying to do to offer alternatives.
Nipuna S Perera 12-May-14 3:35am    
I'm so sorry if I'm wasting your time, But simply I likes to know. Assume, I have 2 progressbars (ProgressBar1, ProgressBar2) and 2 buttons(Button1, Button2), and must be only one timer(Timer1). no matter which button press, all buttons must use same timer, But when the timer ticks it must change the increment of appropriate progress bar. Like this, If I press Button1 timer must increase the increment value of ProgressBar1. If I press Button2, Timer must increase the increment value of ProgerssBar2. Two buttons for two progressbars but one timer. Did you get the idea? again I beg your pardon for my crasy Ideas.

1 solution

You need to extend the EventArgs class like this:

C#
using System;
using System.Windows.Forms;

namespace TickerIssue
{
    public class MyEventArgs : EventArgs
    {
        private readonly ProgressBar _progressBar = null;

        public ProgressBar ProgressBar
        {
            get { return _progressBar; }
        }

        public MyEventArgs(ProgressBar progressBar)
        {
            _progressBar = progressBar;
        }
    }
}


Then you can call this way:

C#
private void button1_Click(object sender, System.EventArgs e)
{
    timer1_Tick(this, new MyEventArgs(progressBar1));
    timer1.Start();
}

private void button2_Click(object sender, System.EventArgs e)
{
    timer1_Tick(this, new MyEventArgs(progressBar2));
    timer1.Start();
}

private void button3_Click(object sender, System.EventArgs e)
{
    timer1_Tick(this, new MyEventArgs(progressBar3));
    timer1.Start();
}

private void timer1_Tick(object sender, System.EventArgs e)
{
    var myEventArgs = (MyEventArgs)e;
    var progressBar = myEventArgs.ProgressBar;
    //...
}


JAFC
 
Share this answer
 
Comments
Maciej Los 9-May-14 13:14pm    
+5!
Only one note: tag is VB.NET, not C# ;)
José Amílcar Casimiro 9-May-14 14:38pm    
Thanks. I'm more used to writing code in C # than in VB.
Maciej Los 9-May-14 15:08pm    
;)
Nipuna S Perera 12-May-14 3:37am    
I appreciate very much on your solution. It seems very perfect solution. but Buddy I'm Still learning VB. This is the best solution exactly what I want. I wish this code is for VB.NET, my problem completely solved. soon I will come for C#. this post will surely help me in the future.<br>
Very very thanks for your time and attention.<br>
Thanks a lot.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900