TreeView problem

F

feel

Goin' crazy with this recursive function ported from delphi...
I send a string like DirA/ DirB /DirC but i get in the treeView
each one in a new node.Cant get the child node....!!
-DirA
-DirB
-DirC
instead of:
DirA
|
- DirB
|
-DirC

Could anyone have a look of this code?.Thanks.
There'a listarray to store the path to avoid double nodes(dont works).

private void LoadNode(string path)

{

string s;

int p,ix;


if (path != "")

{s=path;

}

else

{

s="<EmptyPath>";

}

p=myAL1.IndexOf(s);

if(p >= 0)

{

treeView1.Nodes.Add(myAL1[p].ToString());

}

else

{

p=-1;

//find pos of final separator

string Del = @"\";

for (ix=s.Length-1;ix >=0;ix--)

{

int answer = System.String.Compare(s[ix].ToString(),Del);

string mm=s[ix].ToString();

if (answer==0)

{

p=ix;

break;

}

}

if (p< 0)

{

node= null;

}

else

{

LoadNode(s.Substring(0,p));

s=s.Substring(p,s.Length-p);

}


treeView1.Nodes.Add(s);

treeView1.SelectedNode=node;


myAL1.Add(s);

}

}
 
F

Frank Eller [MVP]

Hi,
Goin' crazy with this recursive function ported from delphi...

There's the problem - Delphis TreeView works completely different from the
..NET one.
I send a string like DirA/ DirB /DirC but i get in the treeView
each one in a new node.Cant get the child node....!!
-DirA
-DirB
-DirC
instead of:
DirA
|
- DirB
|
-DirC

Have you ever worked with a list, where each element has a pointer to the
next and the previous element? That's exactly how the TreeView works. If you
add a string to the Nodes-Collection, it gives you the TreeNode back you
just added (well, as a TreeNode-object of course). You must use this
TreeNode's "Nodes"-Collection to add a subnode.

The following code is the complete (!) code of a little Application I wrote
for a book. It's in german, but you should understand the C#-Code anyway. If
you want, create a new App and replace the whole code of Form1 with the code
below and it should work.

========================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace ListDirectories {
/// <summary>
/// Zusammendfassende Beschreibung für Form1.
/// </summary>
public class FrmMain : System.Windows.Forms.Form {
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnReadAll;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.ImageList imlDrives;
private System.Windows.Forms.TreeView trvDirectories;
private System.Windows.Forms.CheckBox chkRootLines;
private System.Windows.Forms.CheckBox chkPlusMinus;
private System.Windows.Forms.CheckBox chkLines;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtDirCount;
private System.ComponentModel.IContainer components;

public FrmMain() {
//
// Erforderlich für die Windows Form-Designerunterstützung
//
InitializeComponent();

//
// TODO: Fügen Sie den Konstruktorcode nach dem Aufruf von
InitializeComponent hinzu
//
}

/// <summary>
/// Die verwendeten Ressourcen bereinigen.
/// </summary>
protected override void Dispose( bool disposing ) {
if( disposing ) {
if (components != null) {
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert
werden.
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(FrmMain));
this.trvDirectories = new System.Windows.Forms.TreeView();
this.imlDrives = new System.Windows.Forms.ImageList(this.components);
this.label1 = new System.Windows.Forms.Label();
this.btnReadAll = new System.Windows.Forms.Button();
this.chkRootLines = new System.Windows.Forms.CheckBox();
this.chkPlusMinus = new System.Windows.Forms.CheckBox();
this.chkLines = new System.Windows.Forms.CheckBox();
this.btnExit = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.txtDirCount = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// trvDirectories
//
this.trvDirectories.ImageList = this.imlDrives;
this.trvDirectories.Location = new System.Drawing.Point(12, 32);
this.trvDirectories.Name = "trvDirectories";
this.trvDirectories.Size = new System.Drawing.Size(296, 336);
this.trvDirectories.TabIndex = 0;
this.trvDirectories.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.trvDirectories_MouseDown);
this.trvDirectories.DoubleClick += new
System.EventHandler(this.trvDirectories_DoubleClick);
//
// imlDrives
//
this.imlDrives.ColorDepth =
System.Windows.Forms.ColorDepth.Depth24Bit;
this.imlDrives.ImageSize = new System.Drawing.Size(16, 16);
this.imlDrives.ImageStream =
((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imlDrives.Ima
geStream")));
this.imlDrives.TransparentColor = System.Drawing.Color.Transparent;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(78, 13);
this.label1.TabIndex = 1;
this.label1.Text = "Verzeichnisse:";
//
// btnReadAll
//
this.btnReadAll.Location = new System.Drawing.Point(316, 32);
this.btnReadAll.Name = "btnReadAll";
this.btnReadAll.Size = new System.Drawing.Size(168, 23);
this.btnReadAll.TabIndex = 2;
this.btnReadAll.Text = "Alle Verzeichnisse einlesen";
this.btnReadAll.Click += new
System.EventHandler(this.btnReadAll_Click);
//
// chkRootLines
//
this.chkRootLines.Checked = true;
this.chkRootLines.CheckState =
System.Windows.Forms.CheckState.Checked;
this.chkRootLines.Location = new System.Drawing.Point(316, 64);
this.chkRootLines.Name = "chkRootLines";
this.chkRootLines.Size = new System.Drawing.Size(168, 24);
this.chkRootLines.TabIndex = 3;
this.chkRootLines.Text = "Wurzellinien";
this.chkRootLines.Click += new
System.EventHandler(this.chkRootLines_Click);
//
// chkPlusMinus
//
this.chkPlusMinus.Checked = true;
this.chkPlusMinus.CheckState =
System.Windows.Forms.CheckState.Checked;
this.chkPlusMinus.Location = new System.Drawing.Point(316, 88);
this.chkPlusMinus.Name = "chkPlusMinus";
this.chkPlusMinus.Size = new System.Drawing.Size(168, 24);
this.chkPlusMinus.TabIndex = 4;
this.chkPlusMinus.Text = "Plus-/Minus";
this.chkPlusMinus.Click += new
System.EventHandler(this.chkPlusMinus_Click);
//
// chkLines
//
this.chkLines.Checked = true;
this.chkLines.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkLines.Location = new System.Drawing.Point(316, 112);
this.chkLines.Name = "chkLines";
this.chkLines.Size = new System.Drawing.Size(168, 24);
this.chkLines.TabIndex = 5;
this.chkLines.Text = "Hierarchielinien";
this.chkLines.Click += new System.EventHandler(this.chkLines_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(316, 344);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(168, 23);
this.btnExit.TabIndex = 6;
this.btnExit.Text = "Beenden";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(316, 144);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(126, 13);
this.label2.TabIndex = 7;
this.label2.Text = "Gelistete Verzeichnisse:";
//
// txtDirCount
//
this.txtDirCount.Location = new System.Drawing.Point(316, 164);
this.txtDirCount.Name = "txtDirCount";
this.txtDirCount.ReadOnly = true;
this.txtDirCount.Size = new System.Drawing.Size(52, 20);
this.txtDirCount.TabIndex = 8;
this.txtDirCount.Text = "";
//
// FrmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(492, 381);
this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.txtDirCount,

this.label2,

this.btnExit,

this.chkLines,

this.chkPlusMinus,

this.chkRootLines,

this.btnReadAll,

this.label1,

this.trvDirectories});
this.Name = "FrmMain";
this.Text = "Verzeichnisse einlesen";
this.Load += new System.EventHandler(this.FrmMain_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main() {
Application.Run(new FrmMain());
}

/*
* Felder
*/
int iconDrive = 0;
int iconFolderClosed = 1;
int iconFolderOpen = 2;
Point mousePosition = new Point(0,0);

/*
* Eigene Methoden
*/
private void GetDirectories(TreeNode tn) {
DirectoryInfo dir = new DirectoryInfo(tn.FullPath);

try {
foreach (DirectoryInfo di in dir.GetDirectories()) {
try {
tn.Nodes.Add(new TreeNode(di.Name, iconFolderClosed,
iconFolderOpen));
} catch {
// Fehler ignorieren - Zugriff verweigert
}
}
} catch {
MessageBox.Show("Es ist kein Datenträger im Laufwerk","Fehler",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
UpdateDirectoryCount();
}

private void GetAllDirectories(TreeNode tn) {
if (tn.Nodes.Count==0)
GetDirectories(tn);

foreach (TreeNode t in tn.Nodes)
GetAllDirectories(t);
}

private void UpdateDirectoryCount() {
txtDirCount.Text = trvDirectories.GetNodeCount(true).ToString();
}

/*
* Ereignisse
*/
private void FrmMain_Load(object sender, System.EventArgs e) {
string[] drives = Environment.GetLogicalDrives();
trvDirectories.Nodes.Clear();
foreach ( string s in drives )
trvDirectories.Nodes.Add(new TreeNode(s, iconDrive, iconDrive));
}

private void trvDirectories_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e) {
this.mousePosition.X = e.X;
this.mousePosition.Y = e.Y;
}

private void trvDirectories_DoubleClick(object sender, System.EventArgs
e) {
TreeNode tn = trvDirectories.GetNodeAt(this.mousePosition);
if ( (tn != null) && (tn.Nodes.Count==0) )
GetDirectories(tn);
tn.Expand();
}

private void btnReadAll_Click(object sender, System.EventArgs e) {
// Alle Unterverzeichnisse des Knotens einlesen
TreeNode tn = trvDirectories.SelectedNode;

if ( tn != null) {
trvDirectories.BeginUpdate();
this.Cursor = Cursors.WaitCursor;
GetAllDirectories(tn);
this.Cursor = Cursors.Default;
trvDirectories.EndUpdate();
tn.ExpandAll();
}
}

private void chkRootLines_Click(object sender, System.EventArgs e) {
trvDirectories.ShowRootLines = chkRootLines.Checked;
}

private void chkPlusMinus_Click(object sender, System.EventArgs e) {
trvDirectories.ShowPlusMinus = chkPlusMinus.Checked;
}

private void chkLines_Click(object sender, System.EventArgs e) {
trvDirectories.ShowLines = chkLines.Checked;
}

private void btnExit_Click(object sender, System.EventArgs e) {
this.Close();
}
}
}

===========================

Hope this helps you a bit.


Regards,

Frank Eller
 
F

feel

Have you ever worked with a list, where each element has a pointer to the
next and the previous element? That's exactly how the TreeView works. If you
add a string to the Nodes-Collection, it gives you the TreeNode back you
just added (well, as a TreeNode-object of course). You must use this
TreeNode's "Nodes"-Collection to add a subnode.

The following code is the complete (!) code of a little Application I wrote
for a book. It's in german, but you should understand the C#-Code anyway. If
you want, create a new App and replace the whole code of Form1 with the code
below and it should work.

Frank Eller
Thanks for you reply....yes your code is working :)
but still fighting with recursive function and i need to check if a node is
already existing in the treeview.
Will study better your example and nodecollection.
Thanks anyway.
 
F

Frans Bouma [C# MVP]

feel said:
Thanks for you reply....yes your code is working :)
but still fighting with recursive function and i need to check if a node is
already existing in the treeview.
Will study better your example and nodecollection.
Thanks anyway.

Simply add every node you add to a hashtable as the KEY with null as the
value. You can then simply check if a node is already present in the tree by
calling the ContainsKey(nodeObject) method of the hashtable.

FB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top