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();
}
}
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();
}
}