0

is it possible to add numbers to existing strings in a listbox. I can do it in a treeview, like below;

treeView1.Nodes[0].Text = treeView1.Nodes[0].Text + "2,";

So the string on the text would be "JOHN DOE - A" after a button click

EDIT 1 : I have it kind of working;

 var words = new List<string>();

        if (ckbAnswerA.Checked)
            words.Add("-A,");

        if (ckbAnswerB.Checked)
            words.Add("B,");

        if (ckbAnswerC.Checked)
            words.Add("C");

        treeView1.SelectedNode.Text =  string.Join(" ", words);

but it completely removes the original text, it should be

JOHN DOE - A,B,C but it returns -A,B,C

1
  • Its not quite clear what you want for me, please show the before - after more detailed. Commented Mar 24, 2016 at 9:36

1 Answer 1

1

There are quite a few options for doing this, one being outlined in Roma's answer, another is to use the += operator, like so

treeView1.SelectedNode.Text += String.Join(" ", words);

Using the method in Roma's answer, is would instead be

treeView1.SelectedNode.Text = treeView1.SelectedNode.Text + String.Join(" ", words);

The part that you were missing is that you are assigning the text to the joined string, but forgetting to first add the original string to the start of it.

This is what the += does for you.

In addition to this, though not a syntax error, if both boxes B and C are ticked but A is not, the dash will not be displayed because it is added one when box A is ticked, it may be worth adding it before you check instead.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, but when i click answer B, it turns A,B ?
What do you mean by that? could you give more information on what happens now, maybe with your updated code?
it returns JOHN DOE - A, A,B when i have both ckbAnswerA and ckbAnswerB checked.
Based on the code shown that would suggest that ticking box A produces - A, but ticking box B mistakenly produces A,B
Correct, that is what happens?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.