samedi 14 décembre 2019

How can I put a checkBox in the form and not to break the control loop?

I have been working on a little game and I encountered with this problem and I can't figure out how to solve it. Everything worked fine until I put a checkBox on the form. How can I reach that to use the checkBox and the control at the same time and not to break the control loop with the checkBox.

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Mozgás_Gyakorlás
{
    public partial class Form1 : Form
    {
        enum Position
        {
            Left, Right, Up, Down
        }
        public int x = 262;
        public int y = 318;
        private Position pozíció;
        public Form1()
        {
            InitializeComponent();
            pozíció = Position.Left;
        }

        public void pictureBox4_Paint(object sender, PaintEventArgs e)
        {
            timer2.Start();
            e.Graphics.FillRectangle((Brushes.Blue), x, y, 20, 20);
            checkBox1.PerformLayout();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if(pozíció == Position.Right)
            {
                x += 3;
            }
            if(pozíció == Position.Left)
            {
                x -= 3;
            }
            if(pozíció == Position.Up)
            {
                y -= 3;
            }
            if(pozíció == Position.Down)
            {
                y += 3;
            }
            pictureBox4.Invalidate();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Up)
            {
                pozíció = Position.Up;
            }
            if(e.KeyCode == Keys.Down)
            {
                pozíció = Position.Down;
            }
            if(e.KeyCode == Keys.Left)
            {
                pozíció = Position.Left;
            }
            if(e.KeyCode == Keys.Right)
            {
                pozíció = Position.Right;
            }
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                pictureBox4.Image = Image.FromFile(@"D:\Táblázat2.JPG");
            }
            else
            {
                pictureBox4.Image = null;
            }
        }

    }
}




Aucun commentaire:

Enregistrer un commentaire