0
private void SplitString()
{
    ArrayList splitted = new ArrayList();

    string[] words = richTextBox1.Text.Split(new char [] { ' ' },
    StringSplitOptions.RemoveEmptyEntries);

    var word_query =
        (from string word in words
         orderby word
         select word).Distinct();

    string[] result = word_query.ToArray();

    foreach(string results in result)
    {
        richTextBox2.Text = results;
    }         
}

I wrote a form application for taking a text file and splitting strings at the file. And the final purpose is writing unique strings on textbox. The problem is strings flow quickly on textbox but i like to stand them on textbox.

2
  • 2
    change richTextBox2.Text = results; to richTextBox2.Text += results; Commented Feb 9, 2018 at 20:25
  • You can do that whole method in one line: richTextBox2.Text = string.Join(" ", richTextBox1.Text.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Distinct().OrderBy(w => w)); Commented Feb 10, 2018 at 1:41

2 Answers 2

3

You are iterating your array and assigning each time so textbox will have only last item of the array.

You just to join your array and display in the textbox in following way :

string[] result = word_query.ToArray();
richTextBox2.Text = String.Join(",",result); // You can use any string as separator.
Sign up to request clarification or add additional context in comments.

3 Comments

Also, you don't need to convert word_query to an array - string.Join works with IEnumerable
@RufusL Unless you're on .Net 3.5 or earlier.
@RufusL : Fixed the typo. Thanks.
1

If you want each string to appear in the text box on a separate line, just assign the array of strings to the Lines property, like so:

richTextBox2.Lines = result;

Comments

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.