if (listBox1.GetSelected(0))
            {
                richTextBox1.AppendText(Environment.NewLine + "upgrade cottage" + listBox2.SelectedItem);
            }

WHen this code is executed, it prints into the richTextBox1 the words upgrade cottage + the selection of listbox 2, I was wondering how do I put a space between the two?

Recommended Answers

Add a space to the string literal:

"upgrade cottage " + listBox2.SelectedItem

Will the selected item ever have a leading space? If so, you can add a condition to work with that and avoid two adjacent spaces. However, that's probably not necessary as leading spaces in list boxes …

Jump to Post

Please read my previous post again. I did answer your question.

Jump to Post

Since your items don't contain spaces, it's irrelevant, but I'd start with something simple and straightforward:

richTextBox1.AppendText(Environment.NewLine + "upgrade cottage " + listBox2.SelectedItem.ToString().Trim());
Jump to Post

All 12 Replies

Add a space to the string literal:

"upgrade cottage " + listBox2.SelectedItem

Will the selected item ever have a leading space? If so, you can add a condition to work with that and avoid two adjacent spaces. However, that's probably not necessary as leading spaces in list boxes are rare.

all the selectable items in listBox2 are "level1-level10" << All 10 options, when I run it and I select for example "level1" from listBox2 I get "upgrade cottagelevel2....but I want it to be "upgrade cottage level2", Help very much appreciated

Please read my previous post again. I did answer your question.

would you mind explaining this condition or giving me a link to a explanation?

Since your items don't contain spaces, it's irrelevant, but I'd start with something simple and straightforward:

richTextBox1.AppendText(Environment.NewLine + "upgrade cottage " + listBox2.SelectedItem.ToString().Trim());

Alright thx much, If you could still post a link or an example with the this condition it would be nice as I might as well learn it now

It didnt work btw

Then you did something wrong.

I have exactly what was posted in my code and it didnt work but I guess Ill just try to find a way to figure it out myself

You don't or it would have worked. That simple.

What seems weird to me is that you understand how to concatenate two string and append them to a richTextBox and that you have trouble with appending a space. My two esteemed colleages above have already explained this well.
I will add to this:

const string aSpace = " ";
//
richTextBox1.AppendText(Environment.NewLine + "upgrade cottage" + aSpace + listBox2.SelectedItem);

I sometimes forget the little things while im working with more advanced code, which is the reason for me asking as I was in a bit of a hurry and needed to find this out before I reach too far in my program. Thanks all of you for your assistance, will check this later ddanbe if it works ill make sure to mark this as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.