Moving a picture box in timer C# Winforms - c#

Ok so please keep answers very direct and i must say i am very new to C#, i don't know a lot of stuff. Without further adieu my problem.
I am trying to move a picture box horizontally across the screen on a timer.The timer must go infinitely. I have tried all i currently know in C# and searched around quite a lot but nothing answered my exact question which is what i need because of my lesser knowledge of C#. For the last two weeks i worked on graphics mostly and the rest of that was trying to get this to work, So i have no code in my game. This is because for anything to work i need this part to be working. My game is 2D topdown. Any and all help is appreciated.
Thank you for taking the time to read.
Edit
No more answers needed, Thank you Odrai for the answer, it helped me a lot.

Use pictureBox.Location = new Point(x, y) or set pictureBox.Left/Top/Right. You can define x and y as variabels and initialize them with a default value. Increment x on timer tick.
Sample 1:
public partial class Form1 : Form
{
private Random _random
public Form1()
{
InitializeComponent();
_random = new Random();
}
private void timer1_Tick(object sender, EventArgs e)
{
int x = _random.Next(0, 500);
int y = _random.Next(0, 500);
pictureBox1.Top += y;
pictureBox1.Left += x;
}
}
Sample 2:
private void timer1_Tick(object sender, EventArgs e)
{
this.SuspendLayout();
pictureBox.Location = new Point(picust.Location.X + 10, picust.Location.Y);
this.ResumeLayout();
}

Add two buttons with title LEFT and RIGHT to a form and write the following code.
It might give you an idea, how to do simple moving animations.
public partial class Form1 : Form
{
int difference = 0;
Timer timer = new Timer();
public Form1()
{
InitializeComponent();
timer.Interval = 15;
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
pictureBox1.Left += difference;
}
private void btnLeft_Click(object sender, EventArgs e)
{
difference = -2;
}
private void btnRight_Click(object sender, EventArgs e)
{
difference = 2;
}
}

Try This Code it will work :
private void timer1_Tick(object sender, EventArgs e)
{
int width = this.Width; // get the width of Form.
if(pictureBox1.Location.X > width - pictureBox1.Width) //to check condition if pic box is touch the boundroy of form width
{
pictureBox1.Location = new Point(1, pictureBox1.Location.Y); // pic box is set to the new point. here 1 is indicate of X coordinate.
}
else
{
pictureBox1.Location = new Point(pictureBox1.Location.X + 100, pictureBox1.Location.Y); // to move picture box from x coordinate by 100 Point.
}
}

//Try This //
picturebox1.Location = 0,0;

Related

Draw Path Of Shape C#

So I basically have have a picturebox set to a certain location (61, 361) and I have the below code in a timer, so whenever it is enabled it will increment the location of the x and y axis with a certain amount. I just need help to code it so it will trace a path preferably like a dotted line if possible. Thanks in advance for the help. It moves in a parabolic shape btw.
private void SimulationTimer_Tick(object sender, EventArgs e)
{
Ball.Location = new Point(Ball.Location.X + x, Ball.Location.Y - y);
}
Hope this helps:
private void SimulationTimer_Tick(object sender, EventArgs e)
{
System.Drawing.Point current =new System.Drawing.Point();
current = Ball.Location;
Ball.Location = new Point(Ball.Location.X + x, Ball.Location.Y - y);
PictureBox dot = new PictureBox();
dot.BackColor = Color.Red;
dot.Location = current;
dot.Height= 5;
dot.Width = 5;
this.Controls.Add(dot);
}
you can just modify the dot above to what you need
To achieve a path following the changes you have applied to your picturebox you
save each point in a List
NOTE: As you wrote "pictureBox" i assumed you are using Forms and not WPF
public partial class Form1 : Form
{
private List<Point> _points; // List of Points
private Timer _timer; // The Timer
private Graphics _g; // The Graphics object which is responsible for drawing the anything onto the Form
public Form1()
{
_points = new List<Point>();
_timer = new Timer();
_timer.Tick += Timer_Tick;
InitializeComponent();
_g = CreateGraphics();
}
private void Form1_Load(object sender, EventArgs e)
{
_timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
_points.Add(pictureBox1.Location); // Add the current location to the List
// Invoke the GUI Thread to avoid Exceptions
pictureBox1.Invoke(new Action(() =>
{
pictureBox1.Location = new Point(pictureBox1.Location.X + 2, pictureBox1.Location.Y + 2);
}));
Pen blackPen = new Pen(Color.Black, 3);
Invoke(new Action(() =>
{
for (int i = 1; i < _points.Count; i++) // Start at 1 so if you hav less than 2 points it doesnt draw anything
{
_g.DrawLine(blackPen, _points[i - 1], _points[i]);
}
}));
}
}
For a dotted Line you could only draw every second segment of the Line, but that's something you can figure out yourself.
Preview:

Fading an Image - C# WPF

I don't quite understand why it is so complicated as in a standard windows form the code below works just fine. But anyway.
I am trying to just fade an image in, and then fade it back out. At the moment I can't even get it to fade in, I feel pretty dumb because I'm sure there is something I am doing wrong. The for loop works but the image opacity does not change until it gets to 99 and then it suddenly changes. Please help because this is driving me mad.
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
for (int i = 1; i <+ 100; i++)
{
Logo.Opacity = i;
label1.Content = i;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 10);
dispatcherTimer.Start();
}
}
}
I don't know exactly what behaviour you want to get, but in WPF you should use animations. Probably you have to adapt the parameters:
private void Button_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation da = new DoubleAnimation
{
From = 0,
To = 1,
Duration = new Duration(TimeSpan.FromSeconds(1)),
AutoReverse = true
};
Logo.BeginAnimation(OpacityProperty, da);
}
Opacity is double with 0.0 - 1.0 range. So the loop should be something like this.
for (double i = 0.0; i <= 1.0; i+=0.01)
{
Logo.Opacity = i;
label1.Content = i;
}
But as Clemens pointed out it also won't work. You're doing entire loop in one short burst. You should do one increment per timer tick:
double CurrentOpacity = 0.0;
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
CurrentOpacity += 0.01;
if(CurrentOpacity <= 1.0)
{
Logo.Opacity = CurrentOpacity;
label1.Content =CurrentOpacity;
}
else
{
dispatcherTimer.Stop();
}
}

Simple Movement in C#

I want a label to move from the left of the form and stop at the center
I have been able to do this using
Timer tmr = new Timer();
int locx = 6;
public Form1()
{
InitializeComponent();
tmr.Interval = 2;
tmr.Tick += new EventHandler(tmr_Tick);
}
void tmr_Tick(object sender, EventArgs e)
{
label1.Location = new Point(locx, 33);
locx++;
if (locx == 215)
{
tmr.Stop();
}
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "QUICK SPARK";
tmr.Start();
}
I want to know if there is any better way to do this???...Any help will be appreciated
If you are using VS 2012 and C# 5, you can do this simply via await/async:
private async void Form1_Load(object sender, EventArgs e)
{
label1.Text = "QUICK SPARK";
for (int locx = 6; locx < 215; ++locx)
{
await Task.Delay(2);
label1.Location = new Point(locx, 33);
}
}
WinForm Animation Library [.Net3.5+]
A simple library for animating controls/values in .Net WinForm (.Net
3.5 and later). Key frame (Path) based and fully customizable.
https://falahati.github.io/WinFormAnimation/
new Animator2D(
new Path2D(new Float2D(-100, -100), c_control.Location.ToFloat2D(), 500))
.Play(c_control, Animator2D.KnownProperties.Location);
This moves the c_control control from -100, -100 to the location it was in first place in 500 ms.

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.

Why is this marquee code not working?

I'm using this code in order to make a marquee label that scrolls from right to left.
private int xPos = 651;
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Width == xPos)
{
//repeat marquee
xPos = 651;
this.Label7.Location = new System.Drawing.Point(651, 334);
xPos--;
}
else
{
this.Label7.Location = new System.Drawing.Point(xPos, 334);
xPos--;
}
}
651 is the width of the form.
This code makes the label go from right to left, scroll off the form like it should, but doesn't restart at the right again.
I'm pretty sure you might have seen this : using Label control to create looping marquee text in c# winform
Read the first answer.
I assume this.Width is a positive value, therefore if you are doing repeated subtraction, you'll only ever achieve this value at the beginning.
Try if (xPos == 0) instead
that code for me make no sense to me
try this instead, tweak it like you want.
private bool goLeft;
private void timer1_Tick(object sender, EventArgs e)
{
if (Label7.Width + Label7.Left >= this.Width)
{
goLeft = true;
}
else if (Label7.Left < 0)
{
goLeft = false;
}
Label7.Left += goLeft ? -10 : 10;
}
While I agree that it would be easier to just use the marquee from the client side, that wasn't what he was asking...
You are starting xPos at this.Width and decrementing it so, you need to reset xPos when it reaches 0 not this.Width. xPos is only equal to this.Width for the first iteration of each run.
private int xPos = this.Width;
private void timer1_Tick(object sender, EventArgs e) {
if (xPos == 0) { xPos = this.Width; }
this.Label7.Location = new System.Drawing.Point(xPos, 334);
xPos--;
}

Categories