How do I get each name on it's own line?

357mag

Well-known member
Joined
Mar 31, 2023
Messages
75
Programming Experience
3-5
I'm trying to populate a textbox with 5 names. Each name needs to go on it's own line like this:

Tim
Lance
Richard...

...and so on.

This is not a console app. It's a GUI and I'm having problems getting each name to go on it's own line. Here is my current code:

C#:
private void btnAddNames_Click(object sender, EventArgs e)
        {
            string[] names = { "Tim, Shawn, Lance, Jerry, Richard" };
            int x;

            for (x = 0; x < names.Length; x++)
                txtUnsortedNames.Text = names[0] + "\r\n";
        }
 
Firstly, have you set the Multiline property of the TextBox to true? If not, it won't show multiple lines.

Assuming you have, the next thing to do is actually provide multiple lines. Your code replaces the existing Text value every time, so you'll never see more than one value. To do it the way you are, you'd need to get the current Text value first, concatenate the new value and then replace the old value:
C#:
txtUnsortedNames.Text = txtUnsortedNames.Text + names[0] + "\r\n";
You might want to think about using the current array element too, rather than the first one every time:
C#:
txtUnsortedNames.Text = txtUnsortedNames.Text + names[x] + "\r\n";
You can also do it more succinctly:
C#:
txtUnsortedNames.Text += names[x] + "\r\n";
That said, given that there's an AppendText method, you should use that instead:
C#:
txtUnsortedNames.AppendText(names[x] + "\r\n");
BTW, while what you have done is not objectively wrong, it is customary to use i as a loop counter so you should probably do so unless you have a good reason to do otherwise, especially when it's being used as an index. Also, don't declare the loop counter outside the loop unless you need to use it outside the loop:
C#:
for (var i = 0; i < names.Length; i++)
    txtUnsortedNames.AppendText(names[i] + "\r\n");
Having said all that, unless you want to include other text as well, don't do any of that. A TextBox has a Lines property that is type string[], so simply assign your string array to that:
C#:
txtUnsortedNames.Lines = names;
That last option won't append an unnecessary line break after the last value either.
 
Does not work any differently.
When I write:

txtUnsortedNames.Text = txtUnsortedNames.Text + names[x] + "\r\n"

The same thing happens. The carriage return and line feed thing does not work.

When I write txtUnsortedNames.AppendText(names[x] + "\r\n")

That doesn't work either.

The Lines thing does not work either. And I do have Multi-Line set to true. I don't know why these are not working.
Screenshot.jpg
 
Last edited:
Does the same thing happen if you set the Text with multiple lines in the designer? Does the same thing happen in the other TextBox on the same form? Does the same thing happen in a TextBox on a different form? Does the same thing happen in a separate test project? The line breaks seem to be being stripped for some reason, but I'm not sure what that reason would be.
 
I fixed it. It works now. The problem was that there were no quotation marks around each individual name. I took another screenshot showing the correct info:

New Screenshot.jpg
 
Back
Top Bottom