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

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.