how to open a txt file in listbox C#

max stell 21 Reputation points
2022-09-07T14:38:06.087+00:00

hello guys please i need help
i make a file searcher ( i give directory of folder and i search for any extension) like (txt or html or png,etc... ) and i get result in listbox but i can't open any files in the listbox.
i want just to click 2 times and open the txt files and see what inside it what the code need for this ?
also when i get result in listbox i can select any file and write any word to search it inside the txt file but i want when i get all result of txt from the directory i want to just write a term and search for it in all txt files.

picture of the File Searcher : 238649-searcher.png

this is the code : please help me with the coe i need to add it here to do what i need

public partial class Form1 : Form
{
private Searcher searcher;
public Form1()
{
InitializeComponent();
this.searcher = new Searcher(null, null,null);
this.searcher.onFileFound += FileFound;
bgWorker.DoWork += WorkInBackground;
bgWorker.RunWorkerCompleted += WorkerCompleted;
}
private void FileFound(string path)
{
listBox1.BeginInvoke((Action)delegate ()
{
listBox1.Items.Add(path);
});
}
private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
{
MessageBox.Show("Search is Done");
}
private void WorkInBackground(object sender, DoWorkEventArgs args)
{
searcher.Search();
}
private void button1_Click(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex == -1 || string.IsNullOrEmpty(textBox1.Text)|| string.IsNullOrEmpty(textBox2.Text) )
{
MessageBox.Show("Please Select An Extension Or Select A Directory Or Fill a word");
return;
}
listBox1.Items.Clear();
this.searcher.Term = textBox2.Text;
this.searcher.Dir = textBox1.Text;
this.searcher.Ext = comboBox1.SelectedItem.ToString();
bgWorker.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
dialog.RestoreDirectory = true;
if(dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
textBox1.Text = dialog.FileName;
}
}

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,811 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,097 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,540 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 45,986 Reputation points
    2022-09-07T14:53:46.407+00:00

    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.


0 additional answers

Sort by: Most helpful