TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Giovanbattista Barra
NA
98
18.5k
C# Moving Label in Windows Forms
Jan 31 2019 2:01 AM
Hallo dear Team,
for a new project I have to develop a a moving Label on a WinForm.
I've already started like this (here the code):
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.
Reply
Answers (
3
)
Update data using Entity model
How to send parameter using post method in asp.net