I am beginner C# user using WFA.Below is my code to get images to rotate every 4 ticks. please could I have some assistance to what I might have done incorrect.
private void timer1_Tick(object sender, EventArgs e)
{
int time = 0;
int pic = 0;
{
if (time >= 4)
pic++;
this.picBox1.Image = ImageList.Images[pic];
pic++;
if (time == 4 || time == 8 || time == 12 || time == 16)
this.lblCompany.Visible = true;
this.lsbHistory.ValueMember = ("User Clicks on at ___+ DateTime.Now.ToshortDateString");
every time your tick event gets called you set time to 0, that means it is lower then 4. you should take both time and pic values out of the event and reset time to 0 after.
I'd reccomend reading this https://msdn.microsoft.com/en-us/library/5557y8b4.aspx
on how to use breakpoints to have a beter look at what's happening in your code
private int time = 0;
private int pic = 0;
private void timer1_Tick(object sender, EventArgs e)
{
time++;
if (time >= 4)
{
pic++;
time = 0;
this.picBox1.Image = ImageList.Images[pic];
//other logic you might have
Related
I'm working with a Winforms app and I'm tyring to make a label "glide" up when a user clicks on it. To do this, I've created a click event that starts a timer, which then gradually moves the label up the form. I then have a counter that I increment, and when the counter reaches a certain position, the timer should stop. The timer is not stopping, however. I've deliberately set the counter limit to 2 for testing purposes, but the label continues to fly off the form. Here is the code:
private void DrawerTimer_Tick(object sender, EventArgs e)
{
int counter = 0;
newsLabel.Top -= 10;
counter++;
if (counter == 2)
drawerTimer.Stop();
}
private void News_Click(object sender, EventArgs e)
{
drawerTimer.Start();
}
int counter = 0; // here you are setting it 0
newsLabel.Top -= 10;
counter++; // here you are incrementing it by 1
if (counter == 2) // here you are checking for 2, its never going to get there
drawerTimer.Stop();
More than likely you will want to do something like this
private int _counter; // instance field, field to remember your count
private void DrawerTimer_Tick(object sender, EventArgs e)
{
newsLabel.Top -= 10;
counter++; // increment it every tick
if (counter == 2)
drawerTimer.Stop();
}
private void News_Click(object sender, EventArgs e)
{
_counter = 0; // set to zero when start
drawerTimer.Start();
}
comboBox selectedindexchanged event:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
updateTime = Convert.ToInt32(comboBox1.SelectedItem);
xpProgressBar1.Position = 0;
counter = 0;
panel1.Select();
}
Update method:
public void Update()
{
counter += 1;
int position = (int)Math.Round((counter / updateTime) * 100);
xpProgressBar1.Text = counter.ToString() + " %";
xpProgressBar1.Position = position;
if (counter == 10)
{
if (!backgroundWorker1.IsBusy)
{
timer1.Stop();
backgroundWorker1.RunWorkerAsync();
}
counter = 0;
}
}
Timer tick event:
private void timer1_Tick(object sender, EventArgs e)
{
Update();
}
In the combBox by default it's on the first item the number 10 then i can change and select the item with the number 30,50,60,120,300 and all this values are in seconds.
The timer1 interval is set to 1000
The problem is when it's on 10 by default when running the program or if i change it back to 10 in the comboBox it's working good. What it does it's counting 10 seconds and updating the progressBar(xpProgressBar1) by 10's i mean each second the progressBar move by 10 percentages. So after 10 seconds it's getting to 100 percentages.
But when i change the comboBox to the second item to 30 it should count now 30 seconds untill 100%
So i'm not sure in what steps it should move and how to do it. Same if i change it to 120 then it should move progress 120 seconds and again i'm not sure what steps and how to do it so it will get to 100%
What it does now for example if i change it to 120 i see it start counting to 120 by steps of 1 but then when it's getting to 10% it's jumping back to the start and not continue.
It should keep counting the whole 120 seconds untill 100%
If i change it to 30 i see it also counting by steps of 1 each time but again in 10% it's jumping to the start and not continue.
When it's on 10 it's counting by steps of 10 untill 100% so i wonder what should i do and how in the others if it's on 120 to step by 120 ? not logic. So tmake them all to step by 1 also the when it's on 10 ? And again how to do it so it will not stop a 10% and start over again.
Now i changed in the Update method the line if (counter == 10) to:
if (counter == updateTime)
So now if i change in the comboBox select 120 it will count in steps of 1 untill 120 but now when it will get the progressBar to 100% it will keep counting untill 120.
There is no sync between the 120 seconds and the 100% of the progressBar.
EDIT
The Update method:
private int _updateCounter;
public void Update()
{
counter += 1;
xpProgressBar1.Text = counter.ToString() + " %";
xpProgressBar1.Position = _updateCounter++ * 10;
if (counter == 10)
{
if (!backgroundWorker1.IsBusy)
{
timer1.Stop();
backgroundWorker1.RunWorkerAsync();
}
counter = 0;
}
}
This is called prescaler (frequency divider). You have single clock source (Timer) with fastest frequency using which you can achieve needed frequencies by skipping certain calls (events).
All you miss is that skipping:
private int _timer1Ticks;
private void timer1_Tick(object sender, EventArgs e)
{
if(_timer1Ticks++ >= int.Parse(comboBox1.Text) / 10)
{
_timer1Ticks = 0;
Update();
}
}
This way Update will be called exactly 10 times, disregard of combobox selection.
And to calculate progress:
private int _updateCounter;
public void Update()
{
xpProgressBar1.Position = _updateCounter++ * 10;
...
// do not forget to stop timer
if(_updateCounter == 10)
{
timer1.Stop();
_updateCounter = 0; // add some cleanup if using timer more than once
_timer1Ticks = 0;
...
}
}
I am making a Q&A program where you have 15 sec to answer a question. The problem is that the progress bar is only filling like 90% and moves to the next question. I can put the code here but I have worked in Dutch so maybe it's difficult to understand.
private void volgendeVraagFormButton_Click(object sender, EventArgs e)
{
//button for going to next question
vraagFormulierProgressBar.Value = 0;
vraagFormulierTimer.Start();
}
private void vraagFormulierTimer_Tick(object sender, EventArgs e)
{
if (vraagFormulierProgressBar.Value < vraagFormulierProgressBar.Maximum)
vraagFormulierProgressBar.PerformStep();
//checks if the value is lower than 15 sec(max)
else
{ //stops the progress if the 15 secs are over and moves to next question
vraagFormulierTimer.Stop();
vraagFormulierProgressBar.Value = 0;
vraagFormulierTimer.Start();
}
}
Here's the same code with variable names in English:
private void nextQuestionFormButton_Click(object sender, EventArgs e)
{
//button for going to next question
questionFormProgressBar.Value = 0;
questionFormTimer.Start();
}
private void questionFormTimer_Tick(object sender, EventArgs e)
{
if (questionFormProgressBar.Value <questionFormProgressBar.Maximum)
questionFormProgressBar.PerformStep();
//checks if the value is lower than 15 sec(max)
else
{ //stops the progress if the 15 secs are over and moves to next question
questionFormTimer.Stop();
questionFormProgressBar.Value = 0;
questionFormTimer.Start();
}
}
Take a look at this: Disabling .NET progressbar animation when changing value?
So try to increment your progress bar by 2, then decrement by 1. That should fix the animation problem
Edit
Also, change this line
if (vraagFormulierProgressBar.Value < vraagFormulierProgressBar.Maximum)
to this
if (vraagFormulierProgressBar.Value + 1 < vraagFormulierProgressBar.Maximum)
Edit 2
OK, I've got it this time. First, set the maximum of your progress bar to 300 and the interval to 1 (You can fix the timing later). Next, replace the timer tick function with this:
if (progressBar1.Value < progressBar1.Maximum - 1)
{
progressBar1.Increment(2);
progressBar1.Increment(-1);
}
else
{
timer1.Stop();
progressBar1.Maximum = 10000;
progressBar1.Value = 10000;
progressBar1.Value = 9999;
progressBar1.Value = 10000;
System.Threading.Thread.Sleep(150);
progressBar1.Value = 0;
progressBar1.Maximum = 300;
timer1.Start();
}
Sorry about using the English names, I copied this out of my test form. Anyway, hope this helps!
i want to develop a video player which has to play different parts in a video (specified by their start n end positions). I am using directx.audiovideoplayback dll for this purpose.
the starting and ending positions are stored in an array.
eg- {2,6,8,19} tells that segment between 2nd to 6th second has to be played and then 8th to 19th second should be played.
my problem is that despite me giving condition that
if(video_obj.CurrentPosition==endtime) video_obj.Stop();
the video isnt stopping..
the video is playing from 2nd position to end of file.
code is
public static int[] seconds = { 3,8,12,16};
public static int start, end;
private void btnPlay_Click(object sender, EventArgs e)
{
vdo.Owner = panel1;
panel1.Width = 300;
panel1.Height = 150;
vdoTrackBar.Minimum = 0;
vdoTrackBar.Maximum = Convert.ToInt32(vdo.Duration);
if (vdo != null)
{
vdo.Play();
timer1.Start();
}
}
private void vdoTrackBar_Scroll(object sender, EventArgs e)
{
if (vdo != null)
{
vdo.CurrentPosition = vdoTrackBar.Value;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int i;
for (i = 0; i < seconds.Length; i = i + 2)
{
start = seconds[i];
vdo.CurrentPosition = start;
end = seconds[i + 1];
vdoTrackBar.Value = (int)vdo.CurrentPosition;
MessageBox.Show("Starts at " + vdo.CurrentPosition + " and Ends at " + end);
if (vdo.Paused)
vdo.Play();
if (vdo.Playing)
{
if (vdo.CurrentPosition == vdo.Duration)
{
timer1.Stop();
vdo.Pause();
vdoTrackBar.Value = 0;
}
if (vdo.CurrentPosition == end)
{
timer1.Stop();
vdo.Pause();
}
vdoTrackBar.Value += 1;
}
}
Help! Somewhere something is wrong and i have no clue about it
How do i correct it?
Video starts playing when i
So there still isn't enough info in the post to really definitively say whats going wrong. I'm guessing that your timer isn't ticking with enough resolution to happen to tick exactly when the for loop in the timer tick would coincide with the video's current position.
How often does the timer tick? What's the precision of the video's current position?
I have 2 timers. One of them is counting each time it ticks; while using steady interval, or one randomly generated. Second timer counts down to next tick in first timer.
As of right now I am doing as such:
private void btnStart_Click(object sender, EventArgs e)
{
nextClick = int.Parse(nudClickInterval.Value.ToString());
if (nudPlusMinus.Value != 0) tmrClickInterval.Interval = random.Next( int.Parse(nudClickInterval.Value.ToString()) - int.Parse(nudPlusMinus.Value.ToString()), int.Parse(nudClickInterval.Value.ToString()) + int.Parse(nudPlusMinus.Value.ToString()));
else tmrClickInterval.Interval = int.Parse(nudClickInterval.Value.ToString());
tmrClickInterval.Start();
}
private void tmrClickInterval_Tick(object sender, EventArgs e)
{
if (nudPlusMinus.Value == 0) tmrClickInterval.Interval = int.Parse(nudClickInterval.Value.ToString());
else tmrClickInterval.Interval = random.Next(int.Parse(nudClickInterval.Value.ToString()) - int.Parse(nudPlusMinus.Value.ToString()), int.Parse(nudClickInterval.Value.ToString()) + int.Parse(nudPlusMinus.Value.ToString()));
tmrNextClick.Interval = tmrClickInterval.Interval / 10;
tmrNextClick.Start();
content++;
nextClick = tmrClickInterval.Interval;
label1.Text = content.ToString();
}
private void tmrNextClick_Tick(object sender, EventArgs e)
{
if (nextClick <= 0) tmrNextClick.Stop();
else
{
nextClick = nextClick - (tmrClickInterval.Interval / 10);
lblNextClickCount.Text = (nextClick / 100).ToString();
}
}
I am setting up an interval of my count down timer by using first timer's interval dividing by 10. The problem is that I keep getting some errors such as: Value '0' is not a valid value for Interval. Interval must be greater than 0. at line: tmrNextClick.Interval = tmrClickInterval.Interval / 10;.
I'm not sure how to avoid my errors so I figure there might be a better way of counting down time until next timer tick. Plus, I'd want a count down in nice steady interval instead but I am getting quite confused and not sure how to manage this problem.
Hope for some help.
System.Windows.Forms.Timer has an int intervall. Dividing a number which is samller than 10 by 10 results in 0 (integer division!).
Try to use System.Timers.Timer, it has an interval of type double, or check for 0 and assign 1 in that case.