Here's what I'm trying to do. I have a form with several checkboxes. The user will check 1 or 2 or 12 of them, and then click a button and then several things will happen, depending on which boxes were checked.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
checkBox1.Tag = "IP";
Console.WriteLine(checkBox1.Tag);
}
else
{
checkBox1.Tag = null;
}
}
private void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == true)
{
checkBox2.Tag = "PR";
Console.WriteLine(checkBox2.Tag);
}
else
{
checkBox2.Tag = null;
}
}
So that's just a sample of the code for 2 of the 24 checkboxes I have on this form. Once the user clicks the button, the form will save a file using the checkboxX.Tag variable:
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(myFile) &
string.IsNullOrWhiteSpace(selectDate) &
string.IsNullOrWhiteSpace(selectDept))
{
MessageBox.Show("Well, that didn't work. Check your info and try again!");
return;
}
else File.Exists(myFile);
{
MessageBox.Show("Document(s) added! " + selectDept + selectWorker +
" for " + dateTime.Year + "-" + dateTime.Month + "-" + dateTime.Day);
MessageBox.Show("To add more docs, re-open this program");
if (checkBox1.Checked)
{
string finalName = @"C:\testing\" + selectDept + selectWorker + @"\" +
checkBox1.Tag + dateTime.Year + dateTime.Month + dateTime.Day + ".pdf";
textBox2.Text = finalName;
File.Copy(myFile, finalName, true);
}
if (checkBox2.Checked)
{
string finalName = @"C:\testing\" + selectDept + selectWorker + @"\" +
checkBox2.Tag + dateTime.Year + dateTime.Month + dateTime.Day + ".pdf";
textBox2.Text = finalName;
File.Copy(myFile, finalName, true);
}
So, as it stands, it works for the first and only the first checkbox. Like, the file will copied and saved just like it's supposed to, but instead of having 5 identical files named "IP20181012", "PR20181012", "FS20181012" and so on, I end up with one file named "IP20181012", and then one other file just named "20181012", making me believe that the checkBox2.Tag variable is never set. I've tried using a separate variable (other than the built-in checkBox1.Tag one), and the exact same problem happens. It seems like only checkBox1.Tag will get set properly while the other checkBoxX.Tag variables all stay null. Even when I declared separate varaibles and then tried to set them in the checkboxes, only the one for checkBox1 will ever be set, no matter what I do.
Also, is this an okay way to use if statements? I don't need them nested. Can I just kinda line them up so that clicking the button will just run through all of the if statements underneath it?