0

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?

  • Inside your `button2_Click` you are declaring the local bool variables, I think the intent is to set global variables to false? – Aadittya Jun 18 '21 at 07:17
  • @Aadittya yes I have declared them both globally and in the button2. Button2 is the false button, the one that sets the timer1.Enabled to false. That way I thought I could set the bool values back to the 'default' values. – Katarina Brkljač Jun 18 '21 at 07:29
  • 1
    Start by changing all your "while" to "if" and by removing the four "bool" keywords in button2_Click and see where that takes you. – Dan Byström Jun 18 '21 at 08:15

0 Answers0