The goal of my program is to load a directory into my treeview node, which takes more 10 sec(since the directory resides in a remote PC). During this time I shall add a pop up image of waiting. I copied the answer of this post: How can i show an image while my application is loading to my code, like:
private void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
{
Form f = new Form();
f.Size = new Size(20, 25);
Image im = Image.FromFile(@"C:\Documents and Settings\JiangC\Documenti\Immagini\loader.gif");
f.FormBorderStyle = FormBorderStyle.None;
f.MinimizeBox = false;
f.MaximizeBox = false;
f.AutoSize = true;
PictureBox pb = new PictureBox();
//pb.Dock = DockStyle.Fill;
pb.SizeMode = PictureBoxSizeMode.AutoSize;
pb.Image = im;
pb.Location = new Point(50, 50);
f.Controls.Add(pb);
f.Show();
// Application.DoEvents();
BuildTree(directory, treeView2.Nodes, treeView2.SelectedNode);
f.Close();
}
What I want to do is that during the loading (when Buildtree() method is implementing) the form f with the picturebox pb will be shown, and after loading they just disappear.
The first problem is in "pb.Dock = DockStyle.Fill;"
If I uncomment it, this form f will not be shown during the loading(but I can see its minimized window in the bottom of the screen). Only if I take off this line the form f could be shown out.
The second problem is "f.Show();" When the form f is shown, the picturebox pb isn't shown at all(just an empty hole). If I modify it into "f.ShowDialog();", the form f with the picture could be shown.
However here comes the third problem, which is the form f will be always there, my function "BuildTree();" isn't implemented at all!
The forth problem is if I add the line "Application.DoEvents();", it works quite fine, during the loading the form f with the picturebox will be shown and after the loading the f will disappear, but the picture in f is a gif, in this case the gif doesn't animate at all!
So anybody could help me solve the problem?
Here's my code of BuildTree function:
private void BuildTree(DirectoryInfo[] directoryInfo, TreeNodeCollection addInMe, TreeNode clicked)
{
for (int i = 0; i < directoryInfo.Length; i++)
{
var files = directoryInfo[i].GetFiles();
int length = files.Length;
clicked.Nodes.Add(directoryInfo[i].Name);
List<TreeNode> dateNode = new List<TreeNode>();
string[] allDates = new string[length];
try
{
for (int j = 0; j < length; j++)
{
allDates[j] = files[j].Name.Substring(11, 6);
}
}
catch (Exception e)
{ }
string[] date = allDates.Distinct().ToArray();
for (int k = 0; k < date.Length; k++) //From here to the end
{
// is my code loading file
dateNode.Add(clicked.Nodes.Add(date[i]));
for (int j = 0; j < length; j++)
{
// curNode.Nodes.Add(file.FullName, file.Name);
if (files[j].Name.Substring(11, 6) == date[i])
{
dateNode[i].Nodes.Add(files[j].FullName, files[j].Name);
}
}
}
foreach (DirectoryInfo subdir in directoryInfo[i].GetDirectories())
{
BuildTree(subdir, clicked.Nodes);
}
}
}