I want to make a program where a picturebox is moving inside of the panel. If a button up is pressed, then it moves only up, if the button down is pressed it moves only down etc. Then there are two buttons true and false, if true is pressed then the timer is working, if the button false is pressed then the timer isn't working.
The problem is that the program does do all of the above, but I can't go and press the button up, then left etc. It only works for one command at a time, and once one of them is pressed, it looks like all of the others aren't working(excluding true and false buttons, those always work).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool up = false;
bool down = false;
bool left = false;
bool right = false;
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
bool up = false;
bool down = false;
bool left = false;
bool right = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
if(up){
down = false;
left = false;
right = false;
while (!(pictureBox1.Top==panel1.Top))
{
pictureBox1.Top -= 1;
}
}
else if (down)
{
up = false;
left = false;
right = false;
while (!(pictureBox1.Top+pictureBox1.Height == panel1.Top+panel1.Height-10))
{
pictureBox1.Top += 1;
}
}
else if (right)
{
up = false;
down = false;
left = false;
while (!(pictureBox1.Left + pictureBox1.Width == panel1.Left + panel1.Width-10))
{
pictureBox1.Left += 1;
}
}
else if (left)
{
up = false;
right = false;
down = false;
while (!(pictureBox1.Left == panel1.Left))
{
pictureBox1.Left -= 1;
}
}
}
private void button6_Click(object sender, EventArgs e)
{
up = true;
}
private void button5_Click(object sender, EventArgs e)
{
down = true;
}
private void button4_Click(object sender, EventArgs e)
{
left = true;
}
private void button3_Click(object sender, EventArgs e)
{
right = true;
}
I'm not sure where the problem is. Is it in the bool statements?