private void txtSearch_TextChanged(object sender, EventArgs e)
  {
      string searchText = txtSearch.Text.ToLower();

      // Check whether the search should be recursive through subdirectories
      bool searchSubdirectories = chbSearchAll.Checked;

      // Perform search in the current directory or recursively based on checkbox
      if (searchSubdirectories)
      {
          SearchInAllDirectories(searchText);
      }
      else
      {
          SearchInCurrentDirectory(searchText, new DirectoryInfo(initialPath));
      }
  }

  // Search only in the current directory
  private void SearchInCurrentDirectory(string searchText, DirectoryInfo currentDirectory)
  {
      // Clear current pictureBoxes
      pictureBoxes.Clear();
      tableLayoutPanel01.Controls.Clear();

      foreach (FileInfo file in currentDirectory.GetFiles())
      {
          if (file.Name.ToLower().Contains(searchText.ToLower()))
          {
              CreatePictureBoxForFile(file);
          }
      }
  }

  // Search recursively through subdirectories (this should be implemented with actual directory structure)
  private void SearchInAllDirectories(string searchText)
  {
      // Assuming you have a method to get all files from directories and subdirectories in your TreeView
      List<string> allFiles = GetAllFilesFromSubdirectories();

      foreach (var pictureBox in pictureBoxes)
      {
          string fileName = pictureBox.Tag.ToString().ToLower(); // Assuming file name is stored in Tag
          if (fileName.Contains(searchText) || allFiles.Any(f => f.ToLower().Contains(searchText)))
          {
              pictureBox.Visible = true; // Show matching PictureBoxes
          }
          else
          {
              pictureBox.Visible = false; // Hide non-matching PictureBoxes
          }
      }
  }

  private List<string> GetAllFilesFromSubdirectories()
  {
      List<string> allFiles = new List<string>();

      // Get the root directory from the TreeView's root node
      if (treeView1.Nodes.Count > 0)
      {
          TreeNode rootNode = treeView1.Nodes[0];  // Assuming there's only one root node
          DirectoryInfo rootDirectory = rootNode.Tag as DirectoryInfo;

          if (rootDirectory != null)
          {
              // Recursively get all files from the directory and its subdirectories
              GetFilesRecursive(rootDirectory, allFiles);
          }
      }

      return allFiles;
  }

  // Recursive function to retrieve files from directories and subdirectories
  private void GetFilesRecursive(DirectoryInfo directory, List<string> fileList)
  {
      try
      {
          // Add files from the current directory
          foreach (FileInfo file in directory.GetFiles())
          {
              fileList.Add(file.FullName);
          }

          // Recursively call for each subdirectory
          foreach (DirectoryInfo subDirectory in directory.GetDirectories())
          {
              GetFilesRecursive(subDirectory, fileList);
          }
      }
      catch (UnauthorizedAccessException ex)
      {
          // Handle exceptions where access is denied
          Console.WriteLine($"Access to the path is denied: {ex.Message}");
      }
      catch (Exception ex)
      {
          // Handle other potential errors
          Console.WriteLine($"An error occurred while accessing: {ex.Message}");
      }
  }

  private void SearchInAllDirectories(string rootPath, string searchText)
  {
      List<FileInfo> matchingFiles = new List<FileInfo>();

      // Recursively search all directories
      DirectoryInfo rootDirectory = new DirectoryInfo(rootPath);
      GetMatchingFilesRecursive(rootDirectory, searchText, matchingFiles);

      // Clear current pictureBoxes
      pictureBoxes.Clear();
      tableLayoutPanel01.Controls.Clear();

      // Now create PictureBox objects for matching files
      foreach (FileInfo file in matchingFiles)
      {
          CreatePictureBoxForFile(file);
      }
  }
  private void PerformSearch(string rootPath, string searchText, DirectoryInfo currentDirectory)
  {
      if (chbSearchAll.Checked)
      {
          // Search all directories and subdirectories
          SearchInAllDirectories(rootPath, searchText);
      }
      else
      {
          // Search only in the current directory
          SearchInCurrentDirectory(searchText, currentDirectory);
      }
  }
  private void CreatePictureBoxForFile(FileInfo file)
  {
      PictureBox pictureBox = new PictureBox
      {
          Tag = file.Name, // Store the file name in the Tag
          SizeMode = PictureBoxSizeMode.Zoom,
          Size = new Size(100, 100) // Adjust size as needed
      };

      // Optionally load image or preview if applicable
      // pictureBox.Image = Image.FromFile(file.FullName);

      tableLayoutPanel01.Controls.Add(pictureBox); // Add the PictureBox to the table layout
      pictureBoxes.Add(pictureBox); // Add to the list of PictureBoxes
  }
  // Recursive function to get all matching files
  private void GetMatchingFilesRecursive(DirectoryInfo dir, string searchText, List<FileInfo> matchingFiles)
  {
      try
      {
          // Search in current directory files
          foreach (FileInfo file in dir.GetFiles())
          {
              if (file.Name.ToLower().Contains(searchText.ToLower()))
              {
                  matchingFiles.Add(file);
              }
          }

          // Recursively search subdirectories
          foreach (DirectoryInfo subDir in dir.GetDirectories())
          {
              GetMatchingFilesRecursive(subDir, searchText, matchingFiles);
          }
      }
      catch (UnauthorizedAccessException ex)
      {
          // Handle any access exceptions
          Console.WriteLine($"Access denied to directory: {dir.FullName}, {ex.Message}");
      }
  }