To open a text file that you already have shown in the listbox then handle the listbox's double click event.
//Hook this up to the `DoubleClick` event of the listbox in the designer...
private void OnDoubleClickFile ( object sender, EventArgs e )
{
var listBox = sender as ListBox;
//If a file is selected
var selectedFile = listBox.SelectedItem as string;
if (String.IsNullOrEmpty(selectedFile))
return;
//Open the file
var fileWindow = new TextWindow() { FileName = selectedFile };
fileWindow.ShowDialog(this);
}
I'm assuming here you want to open the file in your app and not in the system provided editor. So you'll need to create a new form class (I'm calling it TextWindow
). Create that window to have whatever functionality you want (save, close, find, etc). It is in this window that you'd implement any text search functionality I believe.
public class TextWindow : Form
{
public string FileName { get; set; }
//Assuming here that the form has a TextBox set to multiline called `textBox1`...
protected override void OnLoad ( EventArgs e )
{
base.OnLoad(e);
if (!String.IsNullOrEmpty(FileName))
{
//Not handling errors here...
textBox1.Text = File.ReadAllText(FileName);
};
}
}
This would show the new window with the file contents inside a text box. The user can then edit. Other functionality can be added. Google for Notepad C#
and you'll get sample code on how to build a text editor.
Note that you said you could search for any file extensions so this code would only work for text files. PNGs and non-text files wouldn't work. If you want to open the OS-provided editor for a file then that requires different code.