for a new project I have to develop a a moving Label on a WinForm.
- namespace FormsMovingLabel
- {
- public partial class Form1 : Form
- {
- int x, cnt = 0;
- float y = 1f;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- label1.Top = 10;
- label1.ForeColor = Color.RoyalBlue;
- label1.Font = new Font("", 26, FontStyle.Italic | FontStyle.Bold);
- timer1.Interval = 1;
- timer1.Start();
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- label1.SetBounds(x, (int)y, 15, 20);
- if (cnt == 0)
- {
- x++;
- y = y + 0.9f;
- }
- if (cnt == 1)
- {
- x--;
- y = y - 0.9f;
- }
- if (x == 300)
- {
- cnt = 1;
- }
- if (x == 1)
- {
- cnt = 0;
- }
- }
- }
- }
The code works and the label moves diagonally
but I have a couple of questions because I would like to limit the moving at 10 times.
Any Tip? I' ve already tried with for-loop but the programm doesnt work anymore.
1)Maybe do I need for-loop?
If yes, how shall I use the for-loop? If you consider that:
- if (cnt == 0)
- {
- x++;
- y = y + 0.9f;
- }
- if (cnt == 1)
- {
- x--;
- y = y - 0.9f;
- }
- if (x == 300)
- {
- cnt = 1;
- }
- if (x == 1)
- {
- cnt = 0;
The Label should move with coordinates from 0 to 300.
2) If you look at the code, is it enough that x ==300?
Thanks in advance for your help.