TreeView problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • feel

    TreeView problem

    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.C ompare(s[ix].ToString(),Del );

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

    if (answer==0)

    {

    p=ix;

    break;

    }

    }

    if (p< 0)

    {

    node= null;

    }

    else

    {

    LoadNode(s.Subs tring(0,p));

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

    }


    treeView1.Nodes .Add(s);

    treeView1.Selec tedNode=node;


    myAL1.Add(s);

    }

    }


  • Frank Eller [MVP]

    #2
    Re: TreeView problem

    Hi,
    [color=blue]
    > Goin' crazy with this recursive function ported from delphi...[/color]

    There's the problem - Delphis TreeView works completely different from the
    ..NET one.
    [color=blue]
    > 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
    >[/color]

    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.Collecti ons;
    using System.Componen tModel;
    using System.Windows. Forms;
    using System.Data;
    using System.IO;

    namespace ListDirectories {
    /// <summary>
    /// Zusammendfassen de 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.Componen tModel.IContain er components;

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

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

    /// <summary>
    /// Die verwendeten Ressourcen bereinigen.
    /// </summary>
    protected override void Dispose( bool disposing ) {
    if( disposing ) {
    if (components != null) {
    components.Disp ose();
    }
    }
    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 InitializeCompo nent() {
    this.components = new System.Componen tModel.Containe r();
    System.Resource s.ResourceManag er resources = new
    System.Resource s.ResourceManag er(typeof(FrmMa in));
    this.trvDirecto ries = new System.Windows. Forms.TreeView( );
    this.imlDrives = new System.Windows. Forms.ImageList (this.component s);
    this.label1 = new System.Windows. Forms.Label();
    this.btnReadAll = new System.Windows. Forms.Button();
    this.chkRootLin es = new System.Windows. Forms.CheckBox( );
    this.chkPlusMin us = 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.txtDirCoun t = new System.Windows. Forms.TextBox() ;
    this.SuspendLay out();
    //
    // trvDirectories
    //
    this.trvDirecto ries.ImageList = this.imlDrives;
    this.trvDirecto ries.Location = new System.Drawing. Point(12, 32);
    this.trvDirecto ries.Name = "trvDirectories ";
    this.trvDirecto ries.Size = new System.Drawing. Size(296, 336);
    this.trvDirecto ries.TabIndex = 0;
    this.trvDirecto ries.MouseDown += new
    System.Windows. Forms.MouseEven tHandler(this.t rvDirectories_M ouseDown);
    this.trvDirecto ries.DoubleClic k += new
    System.EventHan dler(this.trvDi rectories_Doubl eClick);
    //
    // imlDrives
    //
    this.imlDrives. ColorDepth =
    System.Windows. Forms.ColorDept h.Depth24Bit;
    this.imlDrives. ImageSize = new System.Drawing. Size(16, 16);
    this.imlDrives. ImageStream =
    ((System.Window s.Forms.ImageLi stStreamer)(res ources.GetObjec t("imlDrives.Im a
    geStream")));
    this.imlDrives. TransparentColo r = System.Drawing. Color.Transpare nt;
    //
    // label1
    //
    this.label1.Aut oSize = true;
    this.label1.Loc ation = new System.Drawing. Point(12, 16);
    this.label1.Nam e = "label1";
    this.label1.Siz e = new System.Drawing. Size(78, 13);
    this.label1.Tab Index = 1;
    this.label1.Tex t = "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.EventHan dler(this.btnRe adAll_Click);
    //
    // chkRootLines
    //
    this.chkRootLin es.Checked = true;
    this.chkRootLin es.CheckState =
    System.Windows. Forms.CheckStat e.Checked;
    this.chkRootLin es.Location = new System.Drawing. Point(316, 64);
    this.chkRootLin es.Name = "chkRootLin es";
    this.chkRootLin es.Size = new System.Drawing. Size(168, 24);
    this.chkRootLin es.TabIndex = 3;
    this.chkRootLin es.Text = "Wurzellini en";
    this.chkRootLin es.Click += new
    System.EventHan dler(this.chkRo otLines_Click);
    //
    // chkPlusMinus
    //
    this.chkPlusMin us.Checked = true;
    this.chkPlusMin us.CheckState =
    System.Windows. Forms.CheckStat e.Checked;
    this.chkPlusMin us.Location = new System.Drawing. Point(316, 88);
    this.chkPlusMin us.Name = "chkPlusMin us";
    this.chkPlusMin us.Size = new System.Drawing. Size(168, 24);
    this.chkPlusMin us.TabIndex = 4;
    this.chkPlusMin us.Text = "Plus-/Minus";
    this.chkPlusMin us.Click += new
    System.EventHan dler(this.chkPl usMinus_Click);
    //
    // chkLines
    //
    this.chkLines.C hecked = true;
    this.chkLines.C heckState = System.Windows. Forms.CheckStat e.Checked;
    this.chkLines.L ocation = new System.Drawing. Point(316, 112);
    this.chkLines.N ame = "chkLines";
    this.chkLines.S ize = new System.Drawing. Size(168, 24);
    this.chkLines.T abIndex = 5;
    this.chkLines.T ext = "Hierarchielini en";
    this.chkLines.C lick += new System.EventHan dler(this.chkLi nes_Click);
    //
    // btnExit
    //
    this.btnExit.Lo cation = new System.Drawing. Point(316, 344);
    this.btnExit.Na me = "btnExit";
    this.btnExit.Si ze = new System.Drawing. Size(168, 23);
    this.btnExit.Ta bIndex = 6;
    this.btnExit.Te xt = "Beenden";
    this.btnExit.Cl ick += new System.EventHan dler(this.btnEx it_Click);
    //
    // label2
    //
    this.label2.Aut oSize = true;
    this.label2.Loc ation = new System.Drawing. Point(316, 144);
    this.label2.Nam e = "label2";
    this.label2.Siz e = new System.Drawing. Size(126, 13);
    this.label2.Tab Index = 7;
    this.label2.Tex t = "Gelistete Verzeichnisse:" ;
    //
    // txtDirCount
    //
    this.txtDirCoun t.Location = new System.Drawing. Point(316, 164);
    this.txtDirCoun t.Name = "txtDirCoun t";
    this.txtDirCoun t.ReadOnly = true;
    this.txtDirCoun t.Size = new System.Drawing. Size(52, 20);
    this.txtDirCoun t.TabIndex = 8;
    this.txtDirCoun t.Text = "";
    //
    // FrmMain
    //
    this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
    this.ClientSize = new System.Drawing. Size(492, 381);
    this.Controls.A ddRange(new System.Windows. Forms.Control[] {

    this.txtDirCoun t,

    this.label2,

    this.btnExit,

    this.chkLines,

    this.chkPlusMin us,

    this.chkRootLin es,

    this.btnReadAll ,

    this.label1,

    this.trvDirecto ries});
    this.Name = "FrmMain";
    this.Text = "Verzeichni sse einlesen";
    this.Load += new System.EventHan dler(this.FrmMa in_Load);
    this.ResumeLayo ut(false);

    }
    #endregion

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

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

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

    try {
    foreach (DirectoryInfo di in dir.GetDirector ies()) {
    try {
    tn.Nodes.Add(ne w TreeNode(di.Nam e, iconFolderClose d,
    iconFolderOpen) );
    } catch {
    // Fehler ignorieren - Zugriff verweigert
    }
    }
    } catch {
    MessageBox.Show ("Es ist kein Datenträger im Laufwerk","Fehl er",
    MessageBoxButto ns.OK, MessageBoxIcon. Stop);
    }
    UpdateDirectory Count();
    }

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

    foreach (TreeNode t in tn.Nodes)
    GetAllDirectori es(t);
    }

    private void UpdateDirectory Count() {
    txtDirCount.Tex t = trvDirectories. GetNodeCount(tr ue).ToString();
    }

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

    private void trvDirectories_ MouseDown(objec t sender,
    System.Windows. Forms.MouseEven tArgs e) {
    this.mousePosit ion.X = e.X;
    this.mousePosit ion.Y = e.Y;
    }

    private void trvDirectories_ DoubleClick(obj ect sender, System.EventArg s
    e) {
    TreeNode tn = trvDirectories. GetNodeAt(this. mousePosition);
    if ( (tn != null) && (tn.Nodes.Count ==0) )
    GetDirectories( tn);
    tn.Expand();
    }

    private void btnReadAll_Clic k(object sender, System.EventArg s e) {
    // Alle Unterverzeichni sse des Knotens einlesen
    TreeNode tn = trvDirectories. SelectedNode;

    if ( tn != null) {
    trvDirectories. BeginUpdate();
    this.Cursor = Cursors.WaitCur sor;
    GetAllDirectori es(tn);
    this.Cursor = Cursors.Default ;
    trvDirectories. EndUpdate();
    tn.ExpandAll();
    }
    }

    private void chkRootLines_Cl ick(object sender, System.EventArg s e) {
    trvDirectories. ShowRootLines = chkRootLines.Ch ecked;
    }

    private void chkPlusMinus_Cl ick(object sender, System.EventArg s e) {
    trvDirectories. ShowPlusMinus = chkPlusMinus.Ch ecked;
    }

    private void chkLines_Click( object sender, System.EventArg s e) {
    trvDirectories. ShowLines = chkLines.Checke d;
    }

    private void btnExit_Click(o bject sender, System.EventArg s e) {
    this.Close();
    }
    }
    }

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

    Hope this helps you a bit.


    Regards,

    Frank Eller




    Comment

    • feel

      #3
      Re: TreeView problem

      [color=blue]
      > 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[/color]
      you[color=blue]
      > 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[/color]
      wrote[color=blue]
      > for a book. It's in german, but you should understand the C#-Code anyway.[/color]
      If[color=blue]
      > you want, create a new App and replace the whole code of Form1 with the[/color]
      code[color=blue]
      > below and it should work.[color=green]
      > > Regards,[/color]
      >
      > Frank Eller
      >
      >
      >[/color]
      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.


      Comment

      • Frans Bouma [C# MVP]

        #4
        Re: TreeView problem

        feel wrote:
        [color=blue]
        >[color=green]
        > > 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[/color]
        > you[color=green]
        > > 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[/color]
        > wrote[color=green]
        > > for a book. It's in german, but you should understand the C#-Code anyway.[/color]
        > If[color=green]
        > > you want, create a new App and replace the whole code of Form1 with the[/color]
        > code[color=green]
        > > below and it should work.[color=darkred]
        > > > Regards,[/color]
        > >[/color]
        > 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.[/color]

        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(nod eObject) method of the hashtable.

        FB


        --
        Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
        My .NET Blog: http://weblogs.asp.net/fbouma
        Microsoft C# MVP

        Comment

        Working...