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?