简体   繁体   中英

display content of selected item from listbox into textbox

Screenshot

I have a "simple" Question: How can i display the content of a selected item from a Listbox into a Textbox. I tried it with

//string value1 = listBox1.SelectedItem.ToString();//textBox1.Text = value1;

but it will show me just the filename of the selcted item(i already find out why).

And i also tried somthing like:

//string value1 = listBox1.SelectedItem.ToString();//textBox1.Text = File.ReadAllLines(value1);

I know that i'll need the actual path of the selected file to "ReadAllLines"

And here is the Problem i have no clue how to get it, may someone can help me please.

If the file that you want to read is located relatively to the app path then use AppDomain.CurrentDomain.BaseDirectory get the app path, and then System.IO.Path.Combine to combine that path with your target relative path (or just the file name, if the file is located in the same folder as the app).

My Solution with the help from Dialecticus:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value1 = listBox1.SelectedItem.ToString();
            string path1 = System.IO.Path.Combine(dataPath, value1);
            textBox1.Text = System.IO.File.ReadAllText(path1);

        }

*dataPath contains the path with the actual path

string dataPath = @"C: \Users\....;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM