C# Programming
11-08-2009, 12:10 PM
Hi everybody!
I have a DirectoryTreeView app to display all images in the selected directory, including:
A TreeView include drives and directories.
All images have put into a FlowLayoutPanel.
When i click an image within it, this image will be display in a PictureBox.
My key matter is that when click an image in FlowLayoutPanel (called from FolderTreeViewAfterSelect method to pic_Click method by using EventHandler)
i also want to gain filename of this image to do more something in pic_Click method.
How could i get it? Please help me! Thank you so much!
Here's my code.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace DirectoryTreeView
{
///
/// Description of MainForm.
///
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void FolderTreeViewBeforeExpand(object sender, TreeViewCancelEventArgs e)
{
MainForm.ActiveForm.Cursor = System.Windows.Forms.Cursors.WaitCursor;
TreeNode currentnode= e.Node;
if (currentnode.Nodes.Count==1) currentnode.Nodes[0].Remove();
if (currentnode.Nodes.Count==0)
{
int step = 0;
DirectoryInfo di = new DirectoryInfo(currentnode.FullPath);
DirectoryInfo[] dirs = null;
try
{
dirs = di.GetDirectories();
}
catch(Exception ex)
{
}
foreach (DirectoryInfo dir in dirs)
{
currentnode.Nodes.Add(dir.Name);
DirectoryInfo[] subfolder;
try
{
subfolder = dir.GetDirectories();
}
catch (Exception ex)
{
continue;
}
if (subfolder.Length > 0)
{
currentnode.Nodes[step].Nodes.Add("");
}
step++;
}
}
MainForm.ActiveForm.Cursor = System.Windows.Forms.Cursors.Default;
}
void MainFormLoad(object sender, EventArgs e)
{
int step = 0;
foreach(string str in Directory.GetLogicalDrives())
{
folderTreeView.Nodes.Add(str);
folderTreeView.Nodes[step].Nodes.Add("");
step++;
}
}
void FolderTreeViewBeforeSelect(object sender, TreeViewCancelEventArgs e)
{
MainForm.ActiveForm.Text = e.Node.FullPath;
}
void FolderTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
MainForm.ActiveForm.Cursor=System.Windows.Forms.Cursors.WaitCursor;
thumbnailsFLP.Controls.Clear();
displayPictureBox.Image = null;
string[] Files = Directory.GetFiles(e.Node.FullPath);
thumbnailsFLP.Controls.Clear();
foreach (String fn in Files)
{
PictureBox pic = new PictureBox();
pic.SizeMode = PictureBoxSizeMode.StretchImage;
if (fn.ToLower().EndsWith(".jpg") || fn.ToLower().EndsWith(".GIF") || fn.ToLower().EndsWith(".png") || fn.ToLower().EndsWith(".bmp") || fn.ToLower().EndsWith(".jpeg"))
{
pic.Image = Image.FromFile(fn);
pic.Height = 100;
pic.Width = 100;
pic.Cursor = Cursors.Hand;
thumbnailsFLP.Controls.Add(pic);
pic.Click += new EventHandler(pic_Click);
}
}
MainForm.ActiveForm.Cursor=System.Windows.Forms.Cursors.Default;
}
void pic_Click(object sender, EventArgs e)
{
PictureBox pic = (PictureBox)sender;
displayPictureBox.SizeMode=PictureBoxSizeMode.StretchImage;
displayPictureBox.Image = pic.Image;
}
}
}
Code link:
http://www.fileden.com/files/2007/12/28/1667604/DirectoryTreeView.rar
I have a DirectoryTreeView app to display all images in the selected directory, including:
A TreeView include drives and directories.
All images have put into a FlowLayoutPanel.
When i click an image within it, this image will be display in a PictureBox.
My key matter is that when click an image in FlowLayoutPanel (called from FolderTreeViewAfterSelect method to pic_Click method by using EventHandler)
i also want to gain filename of this image to do more something in pic_Click method.
How could i get it? Please help me! Thank you so much!
Here's my code.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace DirectoryTreeView
{
///
/// Description of MainForm.
///
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void FolderTreeViewBeforeExpand(object sender, TreeViewCancelEventArgs e)
{
MainForm.ActiveForm.Cursor = System.Windows.Forms.Cursors.WaitCursor;
TreeNode currentnode= e.Node;
if (currentnode.Nodes.Count==1) currentnode.Nodes[0].Remove();
if (currentnode.Nodes.Count==0)
{
int step = 0;
DirectoryInfo di = new DirectoryInfo(currentnode.FullPath);
DirectoryInfo[] dirs = null;
try
{
dirs = di.GetDirectories();
}
catch(Exception ex)
{
}
foreach (DirectoryInfo dir in dirs)
{
currentnode.Nodes.Add(dir.Name);
DirectoryInfo[] subfolder;
try
{
subfolder = dir.GetDirectories();
}
catch (Exception ex)
{
continue;
}
if (subfolder.Length > 0)
{
currentnode.Nodes[step].Nodes.Add("");
}
step++;
}
}
MainForm.ActiveForm.Cursor = System.Windows.Forms.Cursors.Default;
}
void MainFormLoad(object sender, EventArgs e)
{
int step = 0;
foreach(string str in Directory.GetLogicalDrives())
{
folderTreeView.Nodes.Add(str);
folderTreeView.Nodes[step].Nodes.Add("");
step++;
}
}
void FolderTreeViewBeforeSelect(object sender, TreeViewCancelEventArgs e)
{
MainForm.ActiveForm.Text = e.Node.FullPath;
}
void FolderTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
MainForm.ActiveForm.Cursor=System.Windows.Forms.Cursors.WaitCursor;
thumbnailsFLP.Controls.Clear();
displayPictureBox.Image = null;
string[] Files = Directory.GetFiles(e.Node.FullPath);
thumbnailsFLP.Controls.Clear();
foreach (String fn in Files)
{
PictureBox pic = new PictureBox();
pic.SizeMode = PictureBoxSizeMode.StretchImage;
if (fn.ToLower().EndsWith(".jpg") || fn.ToLower().EndsWith(".GIF") || fn.ToLower().EndsWith(".png") || fn.ToLower().EndsWith(".bmp") || fn.ToLower().EndsWith(".jpeg"))
{
pic.Image = Image.FromFile(fn);
pic.Height = 100;
pic.Width = 100;
pic.Cursor = Cursors.Hand;
thumbnailsFLP.Controls.Add(pic);
pic.Click += new EventHandler(pic_Click);
}
}
MainForm.ActiveForm.Cursor=System.Windows.Forms.Cursors.Default;
}
void pic_Click(object sender, EventArgs e)
{
PictureBox pic = (PictureBox)sender;
displayPictureBox.SizeMode=PictureBoxSizeMode.StretchImage;
displayPictureBox.Image = pic.Image;
}
}
}
Code link:
http://www.fileden.com/files/2007/12/28/1667604/DirectoryTreeView.rar