I'm new to this domain and I'm trying to develop an application and after searching, trying any posibillities i still couldn't resolve my problem...:(
I have a windows forms. In that i have toolStripMenu, treeView and pictureBox. On click of the menu item there is FolderBrowserDialog to take input for directory path. Then, after selecting the Directory path and click 'Load Directory' button, directories and files will display in treeView. Here's my code to display directories and files (images) in treeView control:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string strSelectedPath;
FolderBrowserDialog FBD = new FolderBrowserDialog();
private void wczytajŚcieżkęToolStripMenuItem_Click(object sender, EventArgs e)
{
*// Code for the FolderBrowserDialog button*
DialogResult drResult = FBD.ShowDialog();
if (drResult == System.Windows.Forms.DialogResult.OK)
strSelectedPath = FBD.SelectedPath;
}
*// Code for the 'Load Directory'*
private void DirectoryLoad_btn_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
treeView1.Nodes.Clear();
toolTip1.ShowAlways = true;
if (strSelectedPath != "" && Directory.Exists(FBD.SelectedPath))
LoadDirectory(FBD.SelectedPath);
else
MessageBox.Show("Wczytaj folder!", "Brak ścieżki do załadowania");
}
public void LoadDirectory(string Dir)
{
DirectoryInfo di = new DirectoryInfo(Dir);
DirectoryInfo[] directories = di.GetDirectories();
progressBar1.Maximum = Directory.GetFiles(Dir, "*.*", SearchOption.AllDirectories).Length + Directory.GetDirectories(Dir, "**", SearchOption.AllDirectories).Length;
TreeNode tds = treeView1.Nodes.Add(di.Name);
tds.Tag = di.FullName;
tds.StateImageIndex = 0;
LoadFiles(Dir, tds);
LoadSubDirectories(Dir, tds);
}
private void LoadSubDirectories(string dir, TreeNode td)
{
string[] subdirectoryEntries = Directory.GetDirectories(dir);
foreach (string subdirectory in subdirectoryEntries)
{
DirectoryInfo di = new DirectoryInfo(subdirectory);
TreeNode tds = td.Nodes.Add(di.Name);
tds.StateImageIndex = 0;
tds.Tag = di.FullName;
LoadFiles(subdirectory, tds);
LoadSubDirectories(subdirectory, tds);
UpdateProgress();
}
}
private void LoadFiles(string dir, TreeNode td)
{
string[] Files = Directory.GetFiles(dir, "*.*");
foreach (string file in Files)
{
FileInfo fi = new FileInfo(file);
TreeNode tds = td.Nodes.Add(fi.Name);
tds.Tag = fi.FullName;
tds.StateImageIndex = 1;
UpdateProgress();
}
}
private void UpdateProgress()
{
if (progressBar1.Value < progressBar1.Maximum)
{
progressBar1.Value++;
int percent = (int)
(((double)progressBar1.Value / (double)progressBar1.Maximum) * 100);
progressBar1.CreateGraphics().DrawString(percent.ToString() + "%", new Font("Adobe Caslon Pro", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
}
Application.DoEvents();
}
private void treeView1_MouseMove(object sender, MouseEventArgs e)
{
TreeNode theNode = this.treeView1.GetNodeAt(e.X, e.Y);
if (theNode != null && theNode.Tag != null)
{
if (theNode.Tag.ToString() != this.toolTip1.GetToolTip(this.treeView1))
this.toolTip1.SetToolTip(this.treeView1, theNode.Tag.ToString());
}
else
{
this.toolTip1.SetToolTip(this.treeView1, "");
}
}
This code works fine but Now I can't loading images into a pictureBox from treeView. I tried many ways to resolve this and I'm stuck with it ...
Any suggestion with sample code will be great! Thank You!