Reputation: 75
I have a c# winform with tabControl (it has 5 Tabs), textboxes, datetimepicker and checkboxes, connected via Entity Framework to a table. All my checkboxes have their property Checked set to False, CheckState set to Unchecked and ThreeState set to False.
When I'm adding a new record, my checkboxes encounter a very strange behaviour. At adding a new record, my program calls a subroutine called EmptyCheckBoxes to make sure, that they are all set to false and their texts are set to "Ne". So far so good --> they are set to false and texts are correct(see image1).
Here are 2 scenarios with my problem:
1. I click one checkbox to make him checked (set to true and set its text to "Da" --> look at Ponedeljek on image2). Till here, everything went as planned.
But, if I use my mouse and try click other checkbox (look at Torek on image3), this checkbox remains unchecked and all other checkboxes on my form have become grayed and checked!
2. At adding a new record checkboxes are set to false and texts are correct(see image1). But, this time I'm not going to touch my checkboxes, I just click into my datetimepicker and then into some other textbox. Bam! All checkboxes on my form have become grayed and checked!
How can this be, if their ThreeState property is set to False? This is really annoying and I'm totaly lost, where to look to find the cause of this problem. Because, if I save this new record, all checkboxes lose their gray color and stay checked with texts "Ne" (values true are saved into table!).
All checkboxes have this code (this is from one of them):
private void checkBox49_Click(object sender, EventArgs e)
{
if (checkBox49.Checked == true)
{
checkBox49.Text = "Da";
}
else
{
checkBox49.Text = "Ne";
}
}
private void checkBox49_CheckedChanged(object sender, EventArgs e)
{
if (checkBox49.Checked == true)
{
checkBox49.Text = "Da";
}
else
{
checkBox49.Text = "Ne";
}
}
This is a code for setting checkboxes to false and texts to "Ne":
private void EmptyCheckBoxes()
{
TabPage tabPage1 = tabControl1.TabPages[0];
TabPage tabPage2 = tabControl1.TabPages[1];
TabPage tabPage3 = tabControl1.TabPages[2];
TabPage tabPage4 = tabControl1.TabPages[3];
TabPage tabPage5 = tabControl1.TabPages[4];
tabControl1.SelectedTab = tabPage1;
UncheckCheckBoxes(tabPage1);
tabControl1.SelectedTab = tabPage2;
UncheckCheckBoxes(tabPage2);
tabControl1.SelectedTab = tabPage3;
UncheckCheckBoxes(tabPage3);
tabControl1.SelectedTab = tabPage4;
UncheckCheckBoxes(tabPage4);
tabControl1.SelectedTab = tabPage5;
UncheckCheckBoxes(tabPage5);
}
private void UncheckCheckBoxes(Control ctrl)
{
CheckBox chkBox = ctrl as CheckBox;
if (chkBox == null)
{
foreach (Control child in ctrl.Controls)
{
UncheckCheckBoxes(child);
}
}
else
{
chkBox.Checked = false;
chkBox.Text = "Ne";
}
}
Thank you for any hint, clue or solution to this problem.
Vladimir
Upvotes: 0
Views: 693
Reputation: 75
Fabio, you were right about _Click eventhandler. But it did not solve my problem. My problem are causing groupboxes on my winform. Why, I don't know. This is my workaround for CheckStateChanged event that works like charm:
private void My_checkBox_CheckStateChanged(object sender, EventArgs e)
{
if (_addnew == true)
{
CheckBox my_cb = (CheckBox)sender;
if ((my_cb.CheckState == CheckState.Indeterminate) && (my_cb.Text == "Da"))
{
my_cb.Checked = false;
my_cb.Text = "Ne";
}
}
}
Upvotes: 1