I was able to get the ComboBox to load a list of files from a directory.
but, now i am trying to get the contents of that file output to a TextBox or richTextBox.

I am able to use this to output to another ComboBox(however, this is not what i need...):
//code for codebox to codeBox output

string[] files = System.IO.Directory.GetFiles(@"c:\scripts");

        comboBox2.Items.Clear();
        string[] lineOfContents = File.ReadAllLines(comboBox1.Text);
        foreach (var line in lineOfContents)
        {
            string[] tokens = line.Split(',');
            comboBox2.Items.Add(tokens[0]);
            //end of codebox to codebox output.

However, I can't get the entire file to go to a Textbox.
When i use <this.richTextBox1.Text = comboBox2.Text;>
it only pulls the selected line of th text from the combobox and doesnt pull all lines of the .TXT as its split into seperate lines.

Summary: I cant seem to to pull all Lines of text from the textfile listed within the combobox into a Richtextbox by using the combobox to retrieve the list of files first.

  • am I making this too complicated and just missing something very simple??

I can use radio buttons to push the contents of the file to the RichTextBox.... something about loading from combobox to TextBox is not working.

Thank you in Advance!!!

-Joe

Dani AI

Generated

Brief practical summary and a robust pattern that keeps the simple fixes from this thread but avoids UI freezes and path confusion. ’s direct-lines suggestion and the foreach approach noted by @Taafa are fine for small experiments; for a reliable app, let the ComboBox hold the full file path (display the file name) and read the file asynchronously when the selection changes.

Populate the ComboBox with FileInfo objects (or bind a list) so the displayed text is the file name while the SelectedValue is the full path. That keeps UI logic simple and avoids having to reconstruct paths later. The ComboBox.DataSource pattern and Directory.EnumerateFiles are useful here for efficiency when the folder has many files. ComboBox.DataSource documentation Directory.EnumerateFiles documentation. (learn.microsoft.com)

Read the selected file on the UI thread using an async API (avoids blocking mouse/keyboard and keeps the form responsive). Wrap the read call in a try/catch and verify SelectedValue is not null before calling. Async I/O guidance and examples are on Microsoft Docs. Asynchronous file access guide Read text from a file (StreamReader / encoding). (learn.microsoft.com)

Small, practical tips: prefer streaming (ReadToEndAsync / ReadLineAsync or a FileStream with useAsync) for very large files and use RichTextBox.AppendText when appending lines incrementally to avoid creating enormous intermediate strings. If the files are CSV-ish and contain quoted commas, use a proper CSV parser instead of a naive split. The patterns above combine the quick fixes already shown in the thread with safer, production-friendly handling.

Thanks to and @Taafa for the working snippets that validated the approach; confirmed the foreach approach and pointed out the duplicated post.

Recommended Answers

All 4 Replies

Why you use the second combobox???

Simply

    string[] lineOfContents = File.ReadAllLines(comboBox1.Text);
    this.richTextBox1.Lines = lineOfContents;

or

    // Now you have a file content in a single string
    string lineOfContents = File.ReadAllText(comboBox1.Text);
    // Split string to tokens
    string[] tokens = lineOfContents.Split(',');
    // Now tokens is an array of all strings from the file w/o commas
    this.richTextBox1.Lines = tokens;

I may have missed some point but I hope this helps.

Hi, I was using the second comboBox because that is all i could figure out to get it to transfer the entire contents of the file to the ComboBox.

I cant see it now.. but, there was another post earlier today from : I think... Taafa?

  foreach (string line in File.ReadAllLines(comboBox1.Text))
            {
                richTextBox1.AppendText(line.Split(',')[0] + "\n");
            }

and this worked. - I FEEL stupid. because i have been trying to figure this out for about 2 days.

I didnt use LineOfContents.. .I was trying to use StreamWriter/Reader ...

Thank you all so much for providing me with the answer.!
If i could see the other guys answer. i would like to vote both of you guys up!

Kind regards,
Joe

also Teme64, Thank you! works like a charm! this encourages me to keep on keepin' on!

Have a great day!

Somehow your original post got duplicated. My answer is in the other post. If your question is answered please remember to mark them solved. Thanks

Be a part of the DaniWeb community

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