TreeView 2.0 Help Please - Petzold Code Included

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I'm looking for help solving a particular quirk in Petzold's TreeView
example. (ref. "Programming Microsoft Windows Forms - A Streamlined
Approach Using c# - 2005 Edition", pp. 85,86).

The observed problem:

Collapsing a parent node that has two or more child levels exposed
results in the "+" expander _not_ being displayed upon subsequent
expansion of the parent node.

If I manually collapse each child node up the hierarchy ... then the
"+" expander _is_ available upon the next parent node expansion.

---------------
Two questions please:

#1: What command and location solves the disappearing expander
problem?
(The Petzold code is included below.)

I'm struggling with the TreeView complexity and online searches seem
to jump into WPF and XAML stuff of which I am currently ignorant. :(
Starting my GUI training with Forms makes for fast initial progress
perhaps ... but I feel my skill set is seriously lacking. I'm stuck in
the middle?

#2: To gain skills in the GUI arena after studying Forms and C#
fundamentals ... which direction is recommended? The lengthy list
includes: Win32, MFC, Delphi, AJAX, Python, WPF, XAML, etc. What is
the best subject to focus upon next? I am using VS 2005 and given two
equally valuable paths to follow ... I'd choose the least expensive
path. Hopefully avoiding the outrageous $900 cost of Delphi's
Professional level IDE!

*Thanks* for all advice and solution to the specific TreeView problem.

-- Tom


Following is the code block I am struggling with:


//--------------------------------------------------
// DirectoryTreeView.cs (c) 2005 by Charles Petzold
//--------------------------------------------------
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

class DirectoryTreeView : TreeView
{
public DirectoryTreeView()
{
// Get the images used in the tree.
ImageList = new ImageList();
ImageList.Images.Add(new Icon(GetType(),
"Resource.CLSDFOLD.ICO"));
ImageList.Images.Add(new Icon(GetType(),
"Resource.OPENFOLD.ICO"));
ImageList.Images.Add(new Icon(GetType(),
"Resource.35FLOPPY.ICO"));
ImageList.Images.Add(new Icon(GetType(),
"Resource.CDDRIVE.ICO"));
ImageList.Images.Add(new Icon(GetType(),
"Resource.DRIVENET.ICO"));
ImageIndex = 0;
SelectedImageIndex = 1;

DriveInfo[] drives = DriveInfo.GetDrives();

foreach (DriveInfo drive in drives)
{
// Create the drive node.
TreeNode nodeDrive = new
TreeNode(drive.RootDirectory.Name);

// Set the image index depending on the drive type.
if (drive.DriveType == DriveType.Removable)
nodeDrive.ImageIndex = nodeDrive.SelectedImageIndex =
2;

else if (drive.DriveType == DriveType.CDRom)
nodeDrive.ImageIndex = nodeDrive.SelectedImageIndex =
3;

else
nodeDrive.ImageIndex = nodeDrive.SelectedImageIndex =
4;

// Add the node to the tree and add the subdirectories.
Nodes.Add(nodeDrive);
AddDirectories(nodeDrive);

// Make drive C the selected node.
if (drive.RootDirectory.Name[0] == 'C')
SelectedNode = nodeDrive;
}
}

void AddDirectories(TreeNode node)
{
node.Nodes.Clear();

DirectoryInfo dirinfo = new DirectoryInfo(node.FullPath);
DirectoryInfo[] adirinfo;

try
{
adirinfo = dirinfo.GetDirectories();
}
catch
{
return;
}
// Add node for each subdirectory.
foreach (DirectoryInfo dir in adirinfo)
{
TreeNode nodeDir = new TreeNode(dir.Name);
node.Nodes.Add(nodeDir);
}
}
protected override void OnBeforeExpand(TreeViewCancelEventArgs
args)
{
base.OnBeforeExpand(args);

BeginUpdate();

// Add the subdirectories of each subnode about to be
displayed.
foreach (TreeNode node in args.Node.Nodes)
AddDirectories(node);

EndUpdate();
}
}
 
#1: What command and location solves the disappearing expander

I ran Petzold's code and it seems to work fine. Can you explain the problem
in different words again?
#2: To gain skills in the GUI arena after studying Forms and C#
fundamentals ... which direction is recommended?

I suggest that you find a small programming problem to solve and begin
there. Write a bunch of small programs that you care about. Many of these
types of programming projects will teach you the basics of programming.

Russ-
 
#1: What command and location solves the disappearing expander
I ran Petzold's code and it seems to work fine. Can you explain the problem
in different words again?

Russ -- Thanks for the reply.

I expand 4 levels deep in my directory path:

G:\\folder 1\folder 2\folder 3\folder 4

Then I collapse on the G:\\ drive.

When I expand G:\\ again ... The folder 1 expansion is correct and all
of its contents are displayed; however, the folder 2 "plus" symbol has
vanished and that folder has been collapsed. To reset the expander
plus symbol I then manually collapse folder 1 and the remaining child
nodes expanders become visible and functional.

Unfortunately the absence of the expanders after collapsing several
levels simultaneously indicates no child nodes present and this is a
crucial flaw in my buffered file viewing project. :(

This problem is very repeatable on my configuration. (Win2K, VS 5.0)

I've been viewing MS video tutorials, using the online help, searching
online (mostly CodeProject), and experimenting with the various
methods of the tree class in an attempt to gain some basic skill with
treeview. So far the tree is winning every round!

As a work around approach ... I've been trying to learn how to
programmatically expand nodes and select them. Zero success. :(

I've extracted the node path components using the following:

public String nodeFullPath;
DirectoryTreeView tree = new DirectoryTreeView();
tree.AfterSelect += TreeViewOnSelect; // < my handler name
..
..
..
void TreeViewOnSelect(object objSrc, TreeViewEventArgs args)
{
nodeFullPath = args.Node.FullPath;
..
..
..
// Then in a button handler >>
void btnInstructions_Click(object sender, EventArgs e)
{
string ModFullPath = nodeFullPath.Replace("\\\\","\\");
string PathNodes;
PathNodes = ModFullPath.Split("\\");
foreach (string node in PathNodes)
{
Console.Writeline("node = {0}", node);
}
}
------------------------------

The above nicely produces everything from the drive designation "G:"
on through each folder name.

I follow this up with >

tree.CollapseAll();

And then hit (the wall) my head on the tree again. I am clueless how
to programmatically select and expand the nodes. Seems once you
manually expand a hierarchy of nodes and save the path you should be
able to easily repeat the path programmatically. Sounds simple but I
have yet to discover the methodology.


I suggest that you find a small programming problem to solve and begin
there. Write a bunch of small programs that you care about. Many of these
types of programming projects will teach you the basics of programming.

I've been grinding away countless hours on a buffered file viewer
project and have it about 95% grooved out. It's been one heck of a
learning experience and I have a new and profound appreciation for
windows programming now! It combines dataGridView with gradient
painting, the Petzold treeView, and a richTextBox with some fairly
tricky buffering logic that caused me great grief and loss of sleep!

It turns out to be a very nice way to toggle between UTF8 and RTF for
seeing the embedded RTF coding. It allows one to manually search and
view huge ASCII data files in a blink (I have one data file that takes
7 minutes to open in NotePad.) And it also allows a safe way to view
code and copy snippets without concern of modifying the source. More
than once I have confused myself when opening up a source file for the
purpose of copying a block of code and then editing the wrong code
file. Ouch!! Am I the only one who has done this?

My plans are to post the source and hopefully get some reviews from
the gurus in here that is sure to accelerate my learning curve. I have
been stuck so many times in creating it I've lost count. This
newsgroup has been "THE" single most important source of problem
solving I have found. I hope other's find it a useful utility to have
around. I am already using it because of the speed advantage.
Hopefully within a week or so I can post it. It may end up being a
library type implementation ... but I definitely will post the source.
These folks who post library files without source are NOT helping
anyone learn the inner workings of C#.

Thanks again for looking into my problem with the treeView.

-- Tom
 
Okay, I see your problem now. I added one line of code "node.Collapse();".
Try this and see if the solution is to your liking.

Russ.

protected override void OnBeforeExpand(TreeViewCancelEventArgs args)
{
base.OnBeforeExpand(args);

BeginUpdate();

// Add the subdirectories of each subnode about to bedisplayed.
foreach (TreeNode node in args.Node.Nodes)
{
AddDirectories(node);

node.Collapse(); // New line of code.
}

EndUpdate();
}
 
Russ !!!!!!

That is a beautiful fix !!

I always thought the solution would be minimal ... but it just eluded
me. I had zero treeView experience (other than reading in texts)
before this project.

After the struggle ... just reading your response and seeing the one
line fix was a thrill. Literally! Funny how a solution to a problem
can be so exciting.

---------------------------

I'd still like to be able to go deep into the directory structure
while searching for a file and be able to save the full path. Then
later, after performing some other tasks, be able to regenerate the
tree node expansions and selections programmatically. I consider
Petzold to be among the giants of Windows programming and an awesome
educator. I try to use his coding whenever possible and his
non-designer usage style.

Thus, for my benefit and the many who lurk or search ... if you have
any suggestions for programmatic node expansion using the Petzold code
as the basis >> I, for one, will be even more appreciative. :))

Thanks for your help!!

-- Tom
 
Back
Top