I want to move a car first horizontally then after some time like after 100 sec that car should be hidden and from that point a new car display and move vertically from that point.
here I am done with horizontally moving of car.
public Form1()
{
InitializeComponent();
timer1.Interval = 40;
timer1.Start();
}
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
time_for_car1_to_B();
}
public void time_for_car1_to_B()
{
i++;
pictureBox1.Left += 2;
label1.Text = i + "sec";
if (i == 100)
{
timer1.Stop();
pictureBox1.Hide();
}
}
Related
I have a class assignment to move a picturebox randomly across the form. Once you click on the picturebox, it is supposed to scream and change the picture then change it back to the original picture. When you click again, it is supposed to go faster. I have it working up to the point of making it go faster. Here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tm1.Interval = 1000;
tm1.Tick += new EventHandler(tm_Tick);
}
Timer tm1 = new Timer();
int X = 0;
int Y = 0;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (timer1.Enabled)
{
timer1.Stop();
pictureBox1.Image = Properties.Resources.Mimikyu;
Application.DoEvents();
pictureBox1.WaitOnLoad = true;
System.Threading.Thread.Sleep(10);
SoundPlayer sp = new SoundPlayer(Properties.Resources.screa);
sp.PlaySync();
pictureBox1.Image = Properties.Resources.Evee;
}
else
timer1.Start();
}
private void tm_Tick(object sender, EventArgs e)
{
int X = ((int)(new Random().Next(0, 1000)));
int Y = ((int)(new Random().Next(0, 500)));
if (X > 1025 - pictureBox1.Width)
{
X = 1025 - pictureBox1.Width;
}
if (Y > 545 - pictureBox1.Height)
{
Y = 545 - pictureBox1.Height;
}
pictureBox1.Location = new Point(X, Y);
}
}
}
Point me to where I need to go to get the interval to move faster and faster after each click Thank you.
decreasing the tm1.Interval should do it
...
else
if (tm1.Interval>10){tm1.Interval -= 10;}
timer1.Start();
Using C#, windows form.
I am trying to make the program automatically select next item on combo box every x seconds and once it reaches the last one, it goes back to the first one on list. i got pretty much everything minus auto combo box selection part. :(
help please.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//pictureBox1.Image = Image.FromFile(#"Z:\DSCF1661.jpg");
DirectoryInfo test = new DirectoryInfo(#"C:\temp");//Assuming Test is your Folder
FileInfo[] Files = test.GetFiles("*.pdf"); //Getting Text files
comboBox1.DataSource = Files;
comboBox1.DisplayMember = "Name";
timerset();
}
public void axSetting()
{
axAcroPDF1.setShowToolbar(false);
axAcroPDF1.setView("FitH");
axAcroPDF1.setPageMode("none");
axAcroPDF1.setLayoutMode("SinglePage");
axAcroPDF1.Show();
}
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
axAcroPDF1.LoadFile(#"C:\temp\" + comboBox1.Text);
axAcroPDF1.src = #"C:\temp\" + comboBox1.Text;
axSetting();
}
//private System.Windows.Forms.Timer timer1;
public void comboBoxSelect()
{
if (comboBox1.SelectedIndex < comboBox1.Count) // this part... :(
{
comboBox1.SelectedIndex += 1;
}
else
{
comboBox1.SelectedIndex = 0;
}
}
public void timerset()
{
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 5000; // in miliseconds
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
comboBoxSelect();
}
This should work:
if(combobox.SelectedIndex < (combobox.Items.Count -1))
{
combobox.SelectedIndex += 1;
}
else
{
combobox.SelectedIndex = 0;
}
I created 5 PictureBox "Shapes" and I want them to move to the left automatically when the program is launched. So in the timer1_Tick method, I use "Shapes[i].Left -= 2", it's said that "Shapes" isn't in the actual context, so How can I make the Shapes[i] global from the "CreatePipes" method?
public partial class Form1 : Form
{
int i = 0;
int N = 5;
int yspeed;
int gravity = 2;
public Form1()
{
InitializeComponent();
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
yspeed = -15;
}
}
private void Form1_Load(object sender, EventArgs e)
{
CreatePipes(1);
}
public void CreatePipes(object Number)
{
PictureBox[] Shapes = new PictureBox[N];
for (i = 0; i < N; i++)
{
Shapes[i] = new PictureBox();
Shapes[i].Name = "ItemNum_" + i.ToString();
Shapes[i].Location = new Point(300 + 120 * i, 250);
Shapes[i].Size = new Size(30, 1000 );
Shapes[i].BackColor = Color.Green;
Shapes[i].Visible = true;
this.Controls.Add(Shapes[i]);
}
}
private void bird_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
for (i = 0; i < N; i++)
{
Shapes[i].Left -= 2; //So the problem is here. Shapes[i] isn't in the actual context. But I don't know to to make it global from CreatePipes
}
yspeed += gravity;
bird.Top += yspeed;
}
}
}
You have to declare PictureBox[] Shapes above CreatePipes function, in Form1 class. Then in CreatePipes func, change PictureBox[] Shapes = new PictureBox[N]; to Shapes = new PictureBox[N];
I have a Button in my form and a PictureBox with width 290 and height 145 that is hidden at first . I want to show the PictureBox little by little while the button's MouseEnter event happens . So I tried this code :
private void button1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Size = new Size(0, 145);
pictureBox1.Show();
for (int i = 0; i < 290; i++)
pictureBox1.Size = new Size(i, 145);
}
But it shows the PictureBox immediately with the primary size .
I found a similar question in this site ( PictureBox does not change its size ) , but actually its answers couldn't help me , too .
Your code executes all at once, so all you will see is a sudden change.
Use a timer and gradually increase the size when the timer ticks.
timer = new Timer(16); //~60 FPS
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
...
private void button1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Size = new Size(0, 145);
pictureBox1.Show();
timer.Enabled = true; // Enable it
}
...
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (pictureBox1.Width < 290)
pictureBox1.Width++; //Increment
else
timer.Enabled = false; //Disable
}
You must use the Update method of your pictureBox in order to redraw it. Also a little delay will change the size more smoothly on Faster computers.
I changed your code like this:
private void button1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Size = new Size(145, 145);
for (int i = 145; i < 290; i++)
{
pictureBox1.Size = new Size(i, i);
pictureBox1.Update();
System.Threading.Thread.Sleep(1);
}
}
1) define a new int as public :
public partial class Form1 : Form
{
int counter = 0;
.
.
.
2) use a timer :
private void timer1_Tick(object sender, EventArgs e)
{
counter = counter + 10;
timer1.Interval = 10;
pictureBox1.Show();
if (counter <= 290)
{ pictureBox1.Width += 1; }
}
3) enable the timer in mouse event:
private void button1_MouseEnter(object sender, EventArgs e)
{
counter = 0;
pictureBox1.Width = 0;
timer1.Enabled = true;
}
I have been trying to code this for a while and after a couple of weeks of searching the web for an answer I decided to ask
All I want to do is gradually resize pictureBox1 to a set limit from a variable starter value when the mouse hovers over it, the furthest I got was using a forloop which made it instantly change size. I would like it to also change height and width at the same time (pictureBox1 will be a square and i just want it to be a bigger square with a bit of smooth movement)
Also I need it to gradually change back to the original size once the mouse moves off of pictureBox1.
I have been toying about with a couple of solutions found on websites but none seem to work properly, also you might need to know that I have two forms involved in this code; Form1 and frmMenu and because of a mass amount of errors I commented out the bottom two methods.
I do not get any errors but it just doesn't work.
public partial class frmMenu : Form
{
//private int size = 100;
public Timer timer1;
public frmMenu()
{
InitializeComponent();
pictureBox1.MouseEnter += new EventHandler(pictureBox1_MouseEnter);
//pictureBox1.MouseLeave += new EventHandler(pictureBox1_MouseLeave);
}
private string frmMenu_Load
{
set
{
timer1.Interval = 1;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
//for (int i = 140; i > size; size++)
//{
//}
{
timer1.Interval = 1;
}
timer1.Enabled = true;
if (pictureBox1.Height <= 140)
{
pictureBox1.Size = new Size(pictureBox1.Size.Width, pictureBox1.Size.Height + 1);
}
else
{
timer1.Enabled = false;
}
}
// private void pictureBox1_MouseLeave(object sender, EventArgs e)
// {
// if (size > 100)
// for (int i = size; i > 100; i--)
// {
// size = i;
// }
// pictureBox1.Height = pictureBox1.Width = size;
// }
// private void pictureBox1_Click(object sender, EventArgs e)
// {
// var Form1 = new Form1();
// Form1.Show();
// var Menu = new frmMenu();
// Menu.Close();
// }
}
This is my first time asking so sorry if I haven't given enough information ^.^
Try this code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
bool mouseHover;
int width;
int height;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 25;
timer1.Tick += timer1_Tick;
width = pictureBox1.Width;
height = pictureBox1.Height;
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
if (mouseHover)
{
pictureBox1.Width += (pictureBox1.Width < 100) ? 5 : 0;
pictureBox1.Height += (pictureBox1.Height < 100) ? 5 : 0;
}
else
{
pictureBox1.Width += (pictureBox1.Width > width) ? -5 : 0;
pictureBox1.Height += (pictureBox1.Height > height) ? -5 : 0;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
mouseHover = true;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
mouseHover = false;
}
}
}
You can adjust the interval to how you like it, but increasing at 5 pixels horizontally/vertically every 25 milliseconds is pretty smooth. You need to set the initial height and width so you can go back to that size after the mouse leaves the picture box. I use the null coalescing operator so you don't have to stop the timer. As long as the condition on the left side of the ? is true, it will evaluate to the value on the left side of the :. When the condition is false, it evaluates to the right side of the :.