简体   繁体   中英

Using a Treeview with Checkboxes

I have treeView that has several Nodes like this

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 . The answer to the question could mean all,one,two,three or none of the checkboxes are clicked. What im trying to do is remove the letter if a checkbox is unchecked My Code:

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 ...

The Code above works fine when selecting a checkBoxes but not when 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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