for(int a=0;a<10;a++)
{
txtblck =txtblk+ a.ToString();
}
In this, txtbox show all the text at the last.
I want to show the text one by one.
You can try this
for (int a = 0; a < 10; a++)
{
txtblck.Text = txtblck.Text + a.ToString();
Application.DoEvents();
System.Threading.Thread.Sleep(1000);
}
This is happening because the for loop is running so fast that you cannot actually see the text of your TextBox changing. Use the System.Threading.Thread.Sleep method to pause the loop for a while so you can watch the text changing:
for(int a = 0; a < 10; a++)
{
txtblck =txtblk + a.ToString();
System.Threading.Thread.Sleep(1000);
}
Try something like this: (I assumed you were using WPF but any timer will do)
System.Windows.Threading.DispatcherTimer timer;
int a, count;
void start() {
timer = new System.Windows.Threading.DispatcherTimer();
a = 0;
count = 10;
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}
void timer_Tick(object sender, EventArgs e) {
updateString();
}
void updateString() {
if (a < count) {
txtblck.Text += a.toString();
a++;
}
else {
timer.Stop();
}
}
Related
Im creating a transition for my form in Visual Studio,
i'm coding a slide effect for the width but it ends up to slow
is there anyway to make it more faster?
btw here is the code :
`int check = 0;
private void button1_Click(object sender, EventArgs e)
{
this.button1.Text = "Hide";
if (check == 0)
{
for (int i = 350; i <= 824; ++i)
{
this.Size = new Size(i, 507);
Thread.Sleep(10);
this.CenterToScreen();
}
check = 1;
}
else if (check == 1)
{
this.button1.Text = "Key";
for (int i = 824; i >= 351; i--)
{
this.Size = new Size(i, 507);
Thread.Sleep(5);
this.CenterToScreen();
}
check = 0;
}
}
By using your existing code, you can tune it for the sake of speed like;
private int check = 0;
private void button1_Click(object sender, EventArgs e)
{
this.button1.Text = "Hide";
if (check == 0)
{
for (int i = 350; i <= 824; i += 2)
{
this.Size = new Size(i, 507);
Thread.Sleep(1);
this.CenterToScreen();
}
check = 1;
}
else if (check == 1)
{
this.button1.Text = "Key";
for (int i = 824; i >= 351; i -= 2)
{
this.Size = new Size(i, 507);
Thread.Sleep(1);
this.CenterToScreen();
}
check = 0;
}
}
You can change Thread.Sleep lines as above and increase or decrease the loop variables for faster an animation.
I'm trying to make a simple countdown timer program. There are two timer objects. Once timer1 runs out of time, it stops and timer2 starts counting down. When timer2 runs out of time, timer1 starts again and so on. Here is my code:
private void timer1_Tick(object sender, EventArgs e)
{
milli1--;
if(milli1 == -1)
{
sec1--;
milli1 = 59;
if (sec1 == -1)
{
min1--;
sec1 = 59;
if (min1 == -1)
{
min1 = 0;
sec1 = 0;
milli1 = 0;
Console.WriteLine("Timer1 stops!");
timer1.Stop();
timer2.Start();
}
}
}
//updates displayed time
}
However, when timer1 stops, timer2 doesn't seem to start. Somehow, timer1 continues ticking and continuously outputs "Timer1 Stops!" to console. How do I fix this?
EDIT: Here is my timer2_Tick():
private void timer2_Tick(object sender, EventArgs e)
{
milli2--;
if (milli2 == -1)
{
sec2--;
milli2 = 59;
if (sec2 == -1)
{
min2--;
sec2 = 59;
if (min2 == -1)
{
min2 = 0;
sec2 = 0;
milli2 = 0;
timer2.Stop();
timer1.Start();
}
}
}
//updates displayed time
}
EDIT 2: Two timers with same interval is a trivial matter. My code also doesn't work when the timers have different intervals.
I am not sure why you want to use a Timer for a console application as it is not event driven as a Windows.Forms.Timer is. You may be using a Threading timer but your code won’t work as it is using this timer. So below I created a windows form application and set the output type to the console and used the timers as you described and it works as expected. I do not think you can use a timer the way you are in a console application. Again this code makes little sense as ONE timer will do the same thing. If you must use a console application then you may want to check the following post: How do you add a timer to a C# console application
int milli1 = 0;
int milli2 = 0;
int sec1 = 0;
int min1 = 0;
int sec2 = 0;
int min2 = 0;
public Form1() {
InitializeComponent();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e) {
Console.WriteLine("Timer1 tick!");
milli1--;
if (milli1 == -1) {
sec1--;
milli1 = 59;
if (sec1 == -1) {
min1--;
sec1 = 59;
if (min1 == -1) {
min1 = 0;
sec1 = 0;
milli1 = 0;
//Console.WriteLine("Timer1 stops!");
timer1.Stop();
timer2.Start();
}
}
}
//updates displayed time
}
private void timer2_Tick(object sender, EventArgs e) {
Console.WriteLine("Timer2 tick!");
milli2--;
if (milli2 == -1) {
sec2--;
milli2 = 59;
if (sec2 == -1) {
min2--;
sec2 = 59;
if (min2 == -1) {
min2 = 0;
sec2 = 0;
milli2 = 0;
timer2.Stop();
timer1.Start();
}
}
}
//updates displayed time
}
}
I am trying to use two timers to emulate the fading of a label on my form. Once the color values reach 255 in the first timer, the second timer is activated to reverse what the first timer did and bring the color values back to 0. When debugging, it works as expected for two 1 and a half rounds and then gives the error that the value has exceeded 255. This is because after going: timer1>timer2>timer1, it doesn't go to timer2, so the values keep increasing. What could be the issue?
This is in public Form1():
timer1.Tick += new EventHandler(timer1_Tick);
timer2.Tick += new EventHandler(timer2_Tick);
And here's the rest:
Timer timer1 = new Timer();
Timer timer2 = new Timer();
int r = 0;
int g = 0;
int b = 0;
int fade = 0;
private void timer1_Tick(object sender, EventArgs e)
{
fade++;
if (fade <= 500) //just a number above 255
{
r++;
g++;
b++;
lblReboot.ForeColor = Color.FromArgb(255, r, g, b);
if (r == 255)
{
fade = 0;
r = 255;
g = 255;
b = 255;
timer1.Stop();
timer1.Enabled = false;
timer2.Enabled = true;
timer2.Interval = 10;
timer2.Start();
}
}
}
private void timer2_Tick(object sender, EventArgs e)
{
fade++;
if (fade <= 500)
{
r--;
g--;
b--;
lblReboot.ForeColor = Color.FromArgb(255, r, g, b);
}
if (r == 0)
{
fade = 0;
r = 0;
g = 0;
b = 0;
timer2.Stop();
timer2.Enabled = false;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;
timer1.Interval = 10;
timer1.Start();
}
}
This is because you add multiple timer1.Tick event handlers when you change direction in the timer2_Tick. When you reach r == 255 in the second pass through - although you stop timer1 & start timer2 you still have another tick event waiting to be processed which will increment the count to 256.
Remove this line
timer1.Tick += new EventHandler(timer1_Tick);
from timer2_Tick.
I have five dice labels and I have a method Roll() that sets their
text value. At the moment when the roll method happens it just makes the numbers appear, but I would like them to look like they are rolling.
Here is what I have
//Roll() simulates the rolling of this die.
public void Roll()
{
if (Active) {
faceValue = random.Next(1, 7);
label.Text = faceValue.ToString();
}
}
Roll() is called from another class like this:
for (int i = 0; i < dice.Length; i++){
dice[i].Roll();
}
My Question is:
How can I let it looks like they are rolling through a set of numbers then stop on a number ?
Try this
public async void Roll()
{
if (Active)
{
for (int i=0;i<20;i++) //Number of rolls before showing final
{
await Task.Delay(100);
label.Text = random.Next(1, 7).ToString();
}
}
}
I tested a Little and this Looks good done in WPF with a DispatcherTimer:
DispatcherTimer timer = new DispatcherTimer();
private void Button_Click(object sender, RoutedEventArgs e)
{
timer.Tick += Roll;
timer.Interval = new TimeSpan(1);
Random r = new Random();
timer.Start();
}
Random r = new Random();
public void Roll(object sender, EventArgs e)
{
increment++;
if (increment >= 250)
{
timer.Stop();
increment = 0;
}
DiceLabel.Content = r.Next(1, 7).ToString();
timer.Interval = new TimeSpan(increment*3000);
}
If you want it faster or longer you can play with the value from:
timer.Interval = new TimeSpan(increment*3000);
here to set the time between the new number is shown.
And for the time to Dice is rolling, you can play with:
if (increment >= 250)
Hope that helps, have fun.
I have:
private void button1_MouseEnter(object sender, EventArgs e)
{
for (int i = 0; i > 2; i++)
{
button1.Content = Convert.ToString(i);
System.Threading.Thread.Sleep(1000);
}
tekst.Text = "Mouse Enter";
}
When I enter on Button I see only Mouse Enter, but Content on Button don't change. Why? What I can do wrong?
Hi is your for loop correct? It should be i<2 instead of i>2
for (int i = 0; i < 2; i++)
{
Your for loop never execute because you have wrong condition, change it to following code:
for (int i = 0; i < 2; i++)
Also you should use BackgroundWorker (msdn) to update your GUI dynamicly.
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
for (int i = 0; i < 2; i++)
{
this.Dispatcher.Invoke((Action)(() => { btn.Content = Convert.ToString(i); }));
System.Threading.Thread.Sleep(1000);
}
};
worker.RunWorkerCompleted += delegate { tekst.Text = "Mouse Enter"; };
worker.RunWorkerAsync();
}