Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I want output is : 
hello
mynameis
lam 
But it only show "lam" in the textBox1. I don't know why. Can you help me to fix it. Thank you so much!!


What I have tried:

I have this code:
C#
private void Form1_Load(object sender, EventArgs e)
{
    string s="hello\amynameis\alam";
    string[] arr = s.Split('\a');
    foreach (string str in arr)
    {
        textBox1.Text = str + "\n";
    }
}
Posted
Updated 1-Oct-16 12:07pm
v4

Try it with :

C#
private void Form1_Load(object sender, EventArgs e)
{
string s="hello\amynameis\alam";
string[] arr = s.Split('\a');
foreach (string str in arr)
{
textBox1.Text += str + "\n"; 
}
} 
 
Share this answer
 
v2
Comments
Member 12711085 1-Oct-16 10:08am    
Thank you for your help. But it only connect string together, like this: "hellomynameislam". It's not enter.
A person helped me. You can see:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string s="hello\amynameis\alam";
textBox1.Text = s.Replace("\a", Environment.NewLine);
}
}
Ralf Meier 1-Oct-16 14:27pm    
Yes ... of course. Your (new) Solution is much easier (and a good idea too).
Nevertheless - my Solution and also the 2 other Solutions to your question are basing on your original code.
My opinion was to make your own code-snippet functional ...
Look at your code:
C#
foreach (string str in arr)
    {
    textBox1.Text = str + "\n";
    }

Each time round the loop, you overwrite the content of the textbox with the current string, discarding all previous data.
You could try:
C#
foreach (string str in arr)
    {
    textBox1.Text += str + "\n";
    }
But it will ignore the newline.
If it's a Multiline text box, then you're better off doing this:
C#
foreach (string str in arr)
    {
    textBox1.AppendText(str + "\n" );
    }
 
Share this answer
 
This will work for both normal and multi-line textbox.
C#
string s = "hello\amynameis\alam";
string[] arr = s.Split('\a');
textBox1.Text = string.Join(" "+Environment.NewLine, arr);
 
Share this answer
 
If only you had used the debugger, you would have seen that you where replacing the contain of your textbox every time.

-----------------
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900