Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I NEED IT WHEN THE LISTBOX SELECTED ITEM TO DISPLAY IT ON TEXTBOX AND ITS MULTISELECT TXT MY CODE BELOW THE RESULT WAS IT REAPETS THE FILE PLEASE HELP!!!

What I have tried:

OpenFileDialog openFileDialog = new OpenFileDialog();
private void btnOpenFiles_Click(object sender, RoutedEventArgs e)
{

openFileDialog.Multiselect = true;
openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog() == true)
{
foreach (string filename in openFileDialog.FileNames)
{
lbFiles.Items.Add(System.IO.Path.GetFileName(filename));

}

}
}

private void lbFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
object item = lbFiles.SelectedItem;

if (item == null)
{

txtEditor.Text = "No Item Selected";

}
else
{

txtEditor.Text = File.ReadAllText(openFileDialog.FileName);


}
}
Posted
Updated 29-Nov-16 22:20pm
Comments
Mehdi Gholam 30-Nov-16 3:39am    
Please edit your question, because it makes little sense.
Member 11893772 30-Nov-16 3:41am    
i need the .txt in the listbox to display it on textbox on selectionchanged and its multiselect openfiledialoge sir.

1 solution

C#
txtEditor.Text = File.ReadAllText(openFileDialog.FileName);

You are opening the file from the openFileDialog instead of the one in the selected item of the ListBox.

[edit]
The code should be something like:
C#
private void lbFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    object item = lbFiles.SelectedItem;
    if (item == null)
    {
        txtEditor.Text = "No Item Selected";
    }
    else
    {
        txtEditor.Text = File.ReadAllText(item.ToString());
    }
}

[/edit]
 
Share this answer
 
v2
Comments
Member 11893772 30-Nov-16 4:36am    
Show how should i do it?
Richard MacCutchan 30-Nov-16 4:45am    
What do you mean how? You just select the name from the ListBox item.
Member 11893772 30-Nov-16 4:52am    
Its an openfiledialoge in witch you can select multiple text documnents to display it on textbox on selection. that my question?
Richard MacCutchan 30-Nov-16 4:56am    
I have just told you what to do; you use the filename that the user has selected in the ListBox. You do not need the OpenFileDialog any more.
Member 11893772 30-Nov-16 4:59am    
after i did that sir when in run it the file is repeated on selecting files on textbox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900