0

I'm creating a to-do list in C# Win Forms. I want to use my own checkboxes since you can't change the size of the checkboxes in visual studio. I'm using two pictures to be placed in a picture box, one empty check box and one checkbox with an X in it. When the user clicks a checkbox I want to use a function to change the picture in the box; if the check box is empty then I want the click to change the picture to the X filled box, and vice versa. There's a database that saves whether the user checks the box, a 0 for empty and a 1 for checked. I know I could just code each click event to respond individually, but I wanted to use a function to cut down on the code.

This is what I have so far. The function to update the picture.

 private static String UpdateCheck(string checkPosition, int picBox)
        {
            ToDo td = new ToDo();

            PictureBox[] pb = { td.checkMark1, td.checkMark2, td.checkMark3, td.checkMark4, td.checkMark5, td.checkMark6, td.checkMark7,
            td.checkMark8, td.checkMark9, td.checkMark10};


            if (checkPosition == "0")
            {
                pb[picBox].Image= Image.FromFile("C:/Users/royet/source/repos/Last Time/checkedCheckbox.png");
                checkPosition = "1";
                MessageBox.Show(checkPosition);
                return checkPosition;

            }
            else if (checkPosition == "1")
            {
                pb[picBox].Image = Image.FromFile("C:/Users/royet/source/repos/Last Time/checkbox111.png");
                checkPosition = "0";
                MessageBox.Show("Hi");
                return checkPosition;
            }
            else
            {
                return checkPosition;
            }
        }

The click event

    private void checkMark1_Click(object sender, EventArgs e)
        {
            ToDo td = new ToDo();
            string cp = td.checkPos1;
            int pb = 5;
            td.checkPos1 = UpdateCheck(cp, pb);  

        }

The main code

 string checkPos1, checkPos2, checkPos3, checkPos4, checkPos5, checkPos6, 
            checkPos7, checkPos8, checkPos9, checkPos10;

        

        public ToDo()
        {
            InitializeComponent();

            //Reads data from db and places it into textboxes
            string constr = @"Data Source=MasterBlaster\SQLEXPRESS;Initial Catalog=Final;Integrated Security=True";
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT Checked1, Checked2, Checked3, Checked4," +
                    "Checked5, Checked6, Checked7, Checked8, Checked9, Checked10 FROM ToDoDB"))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        sdr.Read();
                        checkPos1 = sdr["Checked1"].ToString(); 
                        checkPos2 = sdr["Checked2"].ToString();
                        checkPos3 = sdr["Checked3"].ToString();
                        checkPos4 = sdr["Checked4"].ToString();
                        checkPos5 = sdr["Checked5"].ToString();
                        checkPos6 = sdr["Checked6"].ToString();
                        checkPos7 = sdr["Checked7"].ToString();
                        checkPos8 = sdr["Checked8"].ToString();
                        checkPos9 = sdr["Checked9"].ToString();
                        checkPos10 = sdr["Checked10"].ToString();
                       
                    }
                    con.Close();
                }
            }

            string[] checks = { checkPos1, checkPos2, checkPos3, checkPos4, checkPos5, checkPos6, checkPos7, checkPos8, checkPos9, checkPos10 };

            PictureBox[] boxes = { checkMark1, checkMark2, checkMark3, checkMark4, checkMark5, checkMark6, checkMark7, checkMark8, checkMark9, checkMark10 };

            for (int i = 0; i < checks.Length; i++)
            {
                if (checks[i] == "0")
                {
                    boxes[i].Image = Image.FromFile("C:/Users/royet/source/repos/Last Time/checkbox111.png");
                }
                else if (checks[i] == "1")
                {
                    boxes[i].Image = Image.FromFile("C:/Users/royet/source/repos/Last Time/checkedCheckbox.png");
                }
            }

        }

Everything works great, there are no errors. But when I load the program and try to click the first picturebox, the picture won't change. I added in the messagebox to see if the code was working at all, and it was. Everything in the function works except for the picture getting changed. The picture stays the same. What am I doing wrong?

RAZ
  • 11
  • 1
  • 2
    You're creating a host of **new Instances** of your Form: `ToDo td = new ToDo();`. What you get is, as mentioned, a new Instance of that Form, not related to the existing one, where your PictureBox Contols show their content. -- You cannot hard-code paths like this: `pb[picBox].Image = Image.FromFile("C:/Users/royet/source/repos/Last Time/checkbox111.png");`. You can add these Images to the Project's Resources, though, so you don't need to consider their actual path when you deploy your app. -- This procedure, even if it could work, is quite *complicated*. – Jimi Feb 19 '21 at 04:59
  • I would consider overriding the paint method of the existing checkbox class rather than rolling your own. I would also suggest you learn how to use the debugger so you don't have to insert messageboxes to see if some code is reached. – JonasH Feb 19 '21 at 07:41

0 Answers0