简体   繁体   English

C#Winform上怪异的复选框行为

[英]Weird checkbox behaviour on c# winform

I have ac# winform with tabControl (it has 5 Tabs), textboxes, datetimepicker and checkboxes, connected via Entity Framework to a table. 我有一个带有tabControl的ac#winform(它具有5个选项卡),文本框,datetimepicker和复选框,它们通过Entity Framework连接到表。 All my checkboxes have their property Checked set to False , CheckState set to Unchecked and ThreeState set to False . 我所有的复选框的属性Checked设置为FalseCheckState设置为UncheckedThreeState设置为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". 添加新记录时,我的程序将调用一个名为EmptyCheckBoxes的子例程,以确保将它们都设置为false,并将其文本设置为“ Ne”。 So far so good --> they are set to false and texts are correct(see image1). 到目前为止,一切都很好->将它们设置为false并且文本正确(请参见image1)。

图片1

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). 1.我单击一个复选框以使他处于选中状态(设置为true并将其文本设置为“ Da”->查看image2上的Ponedeljek)。 Till here, everything went as planned. 到这里,一切都按计划进行。

image2

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! 但是,如果我使用鼠标并尝试单击其他复选框(请查看image3上的Torek),则此复选框将保持未选中状态,并且表单上的所有其他复选框都将变为灰色并处于选中状态!

image3

2. At adding a new record checkboxes are set to false and texts are correct(see image1). 2.在添加新记录时,将复选框设置为false,并且文本正确(请参见image1)。 But, this time I'm not going to touch my checkboxes, I just click into my datetimepicker and then into some other textbox. 但是,这次我不打算触摸复选框,我只是单击进入datetimepicker,然后单击其他文本框。 Bam! am! All checkboxes on my form have become grayed and checked! 我表单上的所有复选框均已变灰并选中!

How can this be, if their ThreeState property is set to False? 如果将它们的ThreeState属性设置为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!). 因为,如果我保存此新记录,则所有复选框都将失去灰色,并保持选中“ Ne”文本的值(将true值保存到表中!)。

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": 这是用于将复选框设置为false并将文本设置为“ 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 弗拉基米尔

Fabio, you were right about _Click eventhandler. Fabio,关于_Click事件处理程序您是对的。 But it did not solve my problem. 但这并不能解决我的问题。 My problem are causing groupboxes on my winform. 我的问题导致Winform上出现分组框。 Why, I don't know. 为什么,我不知道。 This is my workaround for CheckStateChanged event that works like charm: 这是我对CheckStateChanged事件的解决方法,该事件的工作方式类似于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";
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM