My variable is not changing as it is supposed to - c#

My purpose is to change drawlines gradient each second in the form application. However it doesn't work. valueble counter " changing in label but not changing in form paint ..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int counter = 1;
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter >= 10)
timer1.Stop();
lblCountDown.Text = counter.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
counter = 0;
timer1.Tick += new EventHandler(timer1_Tick);
counter = new int();
timer1.Interval = 1000; // 1 second
timer1.Start();
lblCountDown.Text = counter.ToString();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(Brushes.Crimson),200,200,counter ,300) ;
}
}
}
I intend to change my drawings gradient with time but variable is not changing when its
come to form paint... but it does change in lbl ...
help me if u can guys . dont know what to do.

Here, this one works. The answer is to call this.Invalidate() on form every timer tick.
public partial class Form1 : Form
{
int counter = 0;
public Form1()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
counter = 1;
timer1.Interval = 1000; // 1 second
timer1.Start();
lblCountDown.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter >= 10)
timer1.Stop();
lblCountDown.Text = counter.ToString();
this.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(Brushes.Crimson), 200, 200, counter*10, 300);
}
}
Also changed several things:
Event handler is set only once – to avoid multiple handlers if user clicks button several times.
Removed counter = new int() – no need, you have already set it to =1.
In Form1_Paint set x2 coordinate to counter*10 so it is easier to see the movement.

I will recommend following:
Use Panel control for Drawing and its paint event e.g. Panel_Paint
In Timer_Tick use Panel.Invalidate();
In the paint event Dispose for graphic object that is Pen.
Add a Panel control named panel1 in the form.
Keep every other control outside the panel.
Example of Panel Paint and Timer event:
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Brushes.Crimson))
{
e.Graphics.DrawLine(pen, 200, 200, counter, 300);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter >= 10)
timer1.Stop();
lblCountDown.Text = counter.ToString();
panel1.Invalidate();
}

Related

Display different numbers in a textBox

I want to write a code that displays numbers 1 to 10 in a textBox. Following code has been written by me. But unfortunately only number 10 is displayed in textBox. What is wrong in my code? Thanks.
public partial class Form1 : Form
{
int i,j;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (i = 1; i <= 10; i++)
{
textBox1.Text = Convert.ToString(i);
for (j = 0; j < 10000000; j++) ;
}
}
}
textBox1.Text = Convert.ToString(i);
Overwrites the textbox text each loop. You want:
textBox1.Text += Convert.ToString(i) + " ";
Note, there are others ways of doing this
Set default value of text box to 0 and then just increment it on button click
private void button1_Click(object
sender, EventArgs e)
{
textBox1.Text = Convert.ToString(Convert.Int32(textBox1.Text)+1);
for (j = 0; j < 10000000; j++) ;
{
}
}
You should not do work in GUI thread. When you have some simple work (small calculation), then there is not a problem. But when you have a loner work, move the work to background worker.
This is the correct way how to do this:
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace BackgroundWorkerExample
{
public partial class Form1 : Form
{
private BackgroundWorker worker;
public Form1()
{
InitializeComponent();
this.worker = new BackgroundWorker();
this.worker.DoWork += Worker_DoWork;
this.worker.ProgressChanged += Worker_ProgressChanged;
this.worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
this.worker.WorkerReportsProgress = true;
}
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) => this.button1.Enabled = true;
private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.textBox1.Text = e.ProgressPercentage.ToString();
}
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 10; i++)
{
this.worker.ReportProgress(i);
// do work
Thread.Sleep(1000);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.button1.Enabled = false;
this.worker.RunWorkerAsync();
}
}
}
The button (after the operation starts) is disabled to be inactive. The reason is, to prevent try second execution of the background thread. When the background worker ends, the button is enabled.

Increment with timer.Tick not working

I have a metronome project set up. I have a tap button which should check the tempo of your beat and average it out. Every bit of math works properly because I checked it with a calculator. Here is the 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;
using System.Media;
namespace Metronome
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer3_Tick(object sender, EventArgs e)
{
// Convert tempo to timer1.Tick (miliseconds between each beat)
timer1.Interval = Convert.ToInt32(60000 / numericUpDown1.Value);
}
private void button1_Click(object sender, EventArgs e)
{
// Play / Pause button
if (button1.Text == "Go!") { timer1.Enabled = true; button1.Text = "Stop!"; }
else if (button1.Text == "Stop!") { timer1.Enabled = false; button1.Text = "Go!"; }
}
private void timer1_Tick(object sender, EventArgs e)
{
// The 'ding' sound for the metronome
SystemSounds.Beep.Play();
}
private void button2_Click(object sender, EventArgs e)
{
// Set the tempo to be the average of the convertion from miliseconds between 2 beats and the current tempo
if (timer2.Enabled) { numericUpDown1.Value = ((60000 / Tap) + numericUpDown1.Value) / 2; Tap = 0; }
else timer2.Enabled = true;
}
int Tap = 0;
private void timer2_Tick(object sender, EventArgs e)
{
// Get the amount of miliseconds between each beat
Tap++;
}
private void button3_Click(object sender, EventArgs e)
{
// Reset the tap timer
timer2.Enabled = false;
Tap = 0;
}
}
}
The problem is in timer2_Tick, because it should add 1 to Tap every milisecond, instead, when I tried it it goes to a tiny number like 20 or 30. How can I fix this?
There is a really good article I always rely on when selecting which timer to use:
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
I would suggest using one of the threaded options. Specifically, the article says of the the windows forms timer (System.Windows.Forms.Timer):
If you're looking for a metronome, you've come to the wrong place.
If you only need to check the amount of time passed between button taps, use a StopWatch. It gives you a high precision timing mechanism. There is no need for you to count milliseconds yourself.

How to animate a PictureBox in a Win Forms application?

I have a windows form application with a PictureBox control containing an image. I want to move the PictureBox control to the right in a slow movement. Here is my code:
Point currentPoint = pictureBox_Logo.Location;
for (int i = 0; i < 25; i++)
{
pictureBox_Logo.Location = new Point(pictureBox_Logo.Location.X + 1, pictureBox_Logo.Location.Y);
Thread.Sleep(30);
}
The problem here is that when the code executes instead of seeing the picture move, I see a white picture move and the moving stops until the picture appears.
What am I missing and what can I do about it?
Code:
public partial class Form1 : Form
{
void timer_Tick(object sender, EventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
if (x > this.Width)
timer1.Stop();
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Show();
timer1.Start();
}
}
original thread is here Move images in C#
Try to use pictureBox_Logo.Refresh() after Thread.Sleep(30);
Or look for standard Timer control.
My code is good written, but what I did wrong was putting the code in an event:
private void Form1_Shown(object sender, EventArgs e);
But when I put my code in a button it works without any problems.

Visual C# Windows Form Textbox Not Auto-updating

I'm attempting to code an application that reads in the values from an IMU. I'm trying to get the different values of the attitude (i.e. direction) of the IMU for 1 second when using the getAtr_Click method. However, while this is calling the Get_Attitude function, it only changes the textbox values once on my form. How do I make it change each time? (I want to see 10 different values flash by on the textbox).
Here's my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Timers;
using VectorNav.Devices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public static Vn100 vn100 = new Vn100("COM5", 9600);
// New Vn100 on COM5
private void Get_Attitude()
//gets the current yaw, pitch, roll in degrees, and displays
{
var attitude = vn100.CurrentAttitude;
yaw.Text = Convert.ToString(attitude.Ypr.YawInDegs);
pitch.Text = Convert.ToString(attitude.Ypr.PitchInDegs);
roll.Text = Convert.ToString(attitude.Ypr.RollInDegs);
}
public Form1()
//connect to the Vn100, set its output to YPR, output at 10Hz
{
InitializeComponent();
vn100.Connect();
vn100.SetAsyncDataOutputType(Vn100.AsyncOutputType.Ypr, true);
vn100.SetAsyncDataOutputFreq(10, true);
}
private void Form1_Load(object sender, EventArgs e)
{
Get_Attitude();
}
private void tare_Click(object sender, EventArgs e)
{
vn100.Tare(true);
vn100.Tare(true); //for some reason it doesn't display the correct Attitude values w/out a double Tare
Get_Attitude();
}
private void getAtr_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
while (vn100.CurrentAttitude == null)
Thread.Sleep(10);
Get_Attitude();
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
//disconnect from the Vn100 when the box is closed
{
vn100.Disconnect();
base.OnFormClosing(e);
}
}
}
This is because you are haulting the UI thread:
private void getAtr_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
while (vn100.CurrentAttitude == null)
Thread.Sleep(10); // < - UI can't respond in a sleep
Get_Attitude();
}
}
You could do this:
private void getAtr_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
while (vn100.CurrentAttitude == null)
{
Thread.Sleep(10); // < - UI can't respond in a sleep
Application.DoEvents();
}
Get_Attitude();
}
}
Or you could force the update to happen synchronously:
private void tare_Click(object sender, EventArgs e)
{
vn100.Tare(true);
vn100.Tare(true); //for some reason it doesn't display the correct Attitude values w/out a double Tare
Get_Attitude();
this.Update();
}
Or you could use a timeout to update the form instead of a loop.
private void tare_Click(object sender, EventArgs e)
{
System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Tick += new EventHandler(TimerEventProcessor);
myTimer.Interval = 10;
myTimer.Start();
}
void TimerEventProcessor(object sender, EventArgs e)
{
Get_Attitude();
}
Do you want to see 10 (possibly) different values displayed in your textboxes during 1s, at an interval of 10ms, once you call getAttr_Click()?
If yes, then that would be the way to do it:
private void getAtr_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
while (vn100.CurrentAttitude == null)
Thread.Sleep(10);
Get_Attitude();
Thread.Sleep(10);
}
}
In your version, the thread that called the getAttr_Click() method just checks vn100.CurrentAttutude at 10ms intervals. Once that has a non-null value, I suspect it remains non-null, meaning your while() loop will be skipped in every for() iteration so Get_Attitude() will be called 10 times in a row (probably) so fast that you only get to see the last values on the screen.
Thing is, this will keep your UI unresponsive each 10ms during the click, so you might consider calling this getAtr_Click() asynchronously or other trivial solution.
EDIT: Actually, it would help knowing the behaviour of the vn100 component. In the case it is unpredictable, the only thing you can be sure of is displaying 10 different values at no less than 10ms distance in time, regardless if you do everything on the UI thread or on a different thread. It is all related to that vn100.CurrentAttitude behaviour...
change it to:
private void Get_Attitude()
//gets the current yaw, pitch, roll in degrees, and displays
{
var attitude = vn100.CurrentAttitude;
yaw.Text = Convert.ToString(attitude.Ypr.YawInDegs);
pitch.Text = Convert.ToString(attitude.Ypr.PitchInDegs);
roll.Text = Convert.ToString(attitude.Ypr.RollInDegs);
vn100.CurrentAttitude = null;
}
private void getAtr_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
while (vn100.CurrentAttitude == null)
Thread.Sleep(10);
Get_Attitude();
Application.DoEvents();
}
}

Move images in C#

I want to load an small image into a WinForms pictureBox control and then animate it moving to the other side of the form.
I've loaded image and used a timer to move the image, but when I run it the application just shows the final position of the pictureBox and its image.
How I can show image smoothly transition to the final location?
Here is my code so far:
public partial class Form1 : Form
{
private int counter = 0;
void timer_Tick(object sender, EventArgs e)
{
counter++;
if (counter == 1)
{
pictureBox1.Show();
timer1.Stop();
counter = 0;
}
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
while(i<=100){
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
timer1.Start();
}
}
}
Does this work? Sorry, I can't test it where I am right now (on netbook without VS).
public partial class Form1 : Form
{
void timer_Tick(object sender, EventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
if (x > this.Width)
timer1.Stop();
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Show();
timer1.Start();
}
}

Categories