我是这个领域的新手,我正在尝试开发应用程序,并且在搜索后尝试任何可能性,但我仍然无法解决我的问题...:(
我有一个Windows窗体。因为我有 toolStripMenu , treeView 和 pictureBox 。单击菜单项后,会出现 FolderBrowserDialog (FolderBrowserDialog )来输入目录路径。然后,在选择目录路径并单击“加载目录”按钮后,目录和文件将显示在treeView中。这是我的代码,用于在treeView控件中显示目录和文件(图像):
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, "");
}
}
此代码可以正常工作,但现在我无法从 treeView 将图像加载到图片框中。我尝试了多种方法来解决此问题,但我坚持下去...
任何带有示例代码的建议都会很棒! 谢谢!