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!
Related
So I'm playing minecraft and I want to make a program that switches the tool on a set time frame, meaning it switches to the second item slot in the toolbar and then the third and so on. If you're curious I'm playing skyblock and have a cobblestone generator. It works perfectly in notepad and other "textboxes" like chrome with the timing and everything but it seems that minecraft does not allow simulated keyboard presses because it does not respond to the input given by the program. Even if i have the game in full screen (f11) it does not seem to work. Do you think the program itself is faulty or do I need to adapt it for minecraft specifically? Any tips at all would be very appreciated.
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
timer1.Start();
if (textBox1.Text == "")
{
int interval = 1; // set to 1 second for testing purposes. This is 200 in practise.
timer1.Interval = interval * 1000;
}
else
{
int interval = int.Parse(textBox1.Text);
timer1.Interval = 1000 * interval;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (i > 9)
{
this.Close();
}
i++;
keyboardPress(i);
}
private void keyboardPress(int i)
{
SendKeys.Send(Convert.ToString(i));
}
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();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am writing a code for a form that should precisely blink a word Label.
It uses an "expositionTime" to show a Label for X millisececonds and an "intervalTime" to hide it for Y milliseconds.
The process will be repeated for Z times 'numExpositions' and starts after a button is clicked.
My code is working without errors, I am using two timers to do the work.
The problem is that the timers get desynchronized after some expositions.
My question is if is there a solution that gives me precise time syncronization, where exposition time (show label) and interval (hide) will start and finish together - maybe using timers elapse on separate threads or monitor wait and how implement them.
Part of my code using two timers:
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Timer timer2;
private static int counter = 0;
private static int numExpositions = 16;
private static int expositionTime = 2000;
private static int intervalTime = 1800;
// starts both timers that get desynchronized after some time
private void button1_Click(object sender, EventArgs e) { startTimer1(); startTimer2(); }
public void startTimer1()
{
timer1 = new System.Windows.Forms.Timer() { Interval = expositionTime + intervalTime };
timer1.Tick += new EventHandler(OnTimer1Event);
timer1.Start();
}
public void startTimer2()
{
timer2 = new System.Windows.Forms.Timer() { Interval = intervalTime };
timer2.Tick += new EventHandler(OnTimer2Event);
timer2.Start();
}
private void OnTimer1Event(object sender, EventArgs e)
{
if (counter >= numExpositions) // stops timers
{
timer1.Stop();
timer2.Stop();
wordLabel.Visible = false;
counter = 0;
this.Close()
}
else
{
wordLabel.Visible = true; // shows wordLabel
}
}
private void OnTimer2Event(object sender, EventArgs e)
{
wordLabel.Visible = false; // hides wordLabel
}
A better way to do this is as like this:
private async Task DoIt()
{
for(int i = 0 ; i < numExpositions; i++)
{
wordLabel.Visible = true;
await Task.Delay(expositionTime); //expositionTime is the number of milliseconds to keep the label visible
wordLabel.Visible = false;
await Task.Delay(intervalTime); //intervalTime is the number of milliseconds to keep the label hidden
}
}
private void button1_Click(object sender, EventArgs e)
{
DoIt();
}
This requires you to use the .NET framework version 4.5 or later.
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 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?