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.
Related
I am new in c# and trying to develop Scary Screamer Application. It is an joke windows forms application which is running on the PC and invisible in taskbar.
There is timer running in this application. If system datetime.now.minute = 15
It should play scary sound and show scary picture on the screen. After 1-2 seconds picture should disappear from the screen.
But i am stuck and don't know how to make picture disappear. Any Ideas how to do that?
Below is my code:
namespace screamer2
{
public partial class Form1 : Form
{
SoundPlayer pla = new SoundPlayer(Properties.Resources._3);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TransparencyKey = this.BackColor;
this.Left = 0;
this.Top = 0;
this.Width = Screen.PrimaryScreen.Bounds.Width/2;
this.Height = Screen.PrimaryScreen.Bounds.Height/2;
this.TopMost = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Minute == 15)
{
BackgroundImage = Properties.Resources._1;
System.Threading.Thread.Sleep(500);
pla.Play();
}
}
}
}
I would propose to set image once in Form1_Load and then control any showing and hiding of window using Form.Opacity variable. I have tested the code below and should work as you wanted.
SoundPlayer pla = new SoundPlayer(Properties.Resources._3);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.TransparencyKey = this.BackColor;
this.Left = 0;
this.Top = 0;
this.Opacity = 0; //This line added
this.Width = Screen.PrimaryScreen.Bounds.Width / 2;
this.Height = Screen.PrimaryScreen.Bounds.Height / 2;
this.BackgroundImage = Properties.Resources._1; //We set the image once here
this.TopMost = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Minute == 15)
{
this.Opacity = 1; //We show the window
System.Threading.Thread.Sleep(500);
pla.Play();
this.Opacity = 0; //We hide the window
}
}
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;
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();
}
}
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.
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();
}
}