简体   繁体   English

将Treeview与复选框一起使用

[英]Using a Treeview with Checkboxes

I have treeView that has several Nodes like this 我的treeView有几个像这样的Nodes

Question 1
Question 2
Question 3
Question 4

Inside each of those node there are 4 checkboxes - Answer A, Answer B, Answer C, Answer D , depending on whichever checkbox is clicked the text of a Node will change to Question1 - A,B,C,D . 在每个节点内有4个checkboxes - Answer A, Answer B, Answer C, Answer D ,具体取决于单击哪个复选框,节点的文本将变为Question1 - A,B,C,D The answer to the question could mean all,one,two,three or none of the checkboxes are clicked. 该问题的答案可能意味着全部,一个,两个,三个或没有一个checkboxes被单击。 What im trying to do is remove the letter if a checkbox is unchecked My Code: 什么即时试图做的是去除信,如果一个checkboxunchecked我的代码:

private void ckbAnswerA_CheckedChanged(object sender, EventArgs e)
    {

        updateAnswerA();
    }
void updateAnswerA()
    {
        var words = new List<string>();


        if (ckbOption1.Checked)
        {
            words.Add("A,");
            treeView1.SelectedNode.Text += string.Join(" ", words);
        }

Etc for the other checkBoxes ... 等等其他checkBoxes ...

The Code above works fine when selecting a checkBoxes but not when deselecting 上面的代码在selecting checkBoxes时工作正常,但在deselecting时则不能正常工作

I manually way, I hope you get the idea. 我以手动方式,希望您能理解。

private void ckbAnswerA_CheckedChanged(object sender, EventArgs e)
{
    if (ckbAnswerA.Checked)
    {
        updateAnswerA(true);
    }
    else
    {
        updateAnswerA(false);
    }
}

private void updateAnswerA(bool flag)
{
    if(flag)
    {
        var words = new List<string>();
        words.Add("A,");
        treeView1.SelectedNode.Text += string.Join(" ", words);
    }
    else
    {
        string update = treeView1.SelectedNode.Text;
        update = update.Replace("A,", "");
        treeView1.SelectedNode.Text = update;
    }
}

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

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