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

A few quick clarifications that address what tried and what suggested: the ComboBox Text (or a selected ComboBox’s single item) is only the displayed item — it will never contain the entire file contents. The usual pattern is (1) keep the file’s full path available from the ComboBox (either as the item, the ValueMember, or via a map), and then (2) read the file when the user selects it and put that string/lines into the RichTextBox. For small files ReadAllText (or ReadAllTextAsync) is simplest; for large files stream and append.

Example: bind the file list but display only the filename; keep the full path as the value, then load on selection.

// Populate once (e.g. Form.Load)
var folder = @"C:\scripts";
var files = new System.IO.DirectoryInfo(folder).GetFiles("*.txt").ToList();
comboBox1.DataSource = files;
comboBox1.DisplayMember = "Name";     // shown to the user
comboBox1.ValueMember = "FullName";  // use this to read the file
// SelectedIndexChanged handler (async to keep UI responsive)
private async void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var path = comboBox1.SelectedValue as string;
    if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path)) { richTextBox1.Clear(); return; }
    try
    {
        richTextBox1.Text = await System.IO.File.ReadAllTextAsync(path);
    }
    catch (Exception ex)
    {
        richTextBox1.Text = "Unable to load file: " + ex.Message;
    }
}

Troubleshooting notes: if your ComboBox only contains filenames (not full paths) use Path.Combine(folder, (string)comboBox1.SelectedItem). If files use a special encoding provide the Encoding overload. For very large files read with a StreamReader and AppendText in chunks to avoid UI freezes or memory spikes — that’s where ’s line-by-line AppendText approach is appropriate. Also handle IO/permission exceptions so the UI remains stable.

Recommended Answers

All 2 Replies

It looks like you need to use the AppendText method of the TextBox class:

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

Hi Tinstaafl, i look forward to plugging this in when i get back home. Thank you very much for your response and time taken to answer my question!

I will update soon.

Joe

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.