topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday December 13, 2024, 10:41 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: C# TreeView problem  (Read 8621 times)

Ruffnekk

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 332
  • Uhm yeah...
    • View Profile
    • RuffNekk's Crypto Pages
    • Donate to Member
C# TreeView problem
« on: February 15, 2007, 03:18 AM »
Hi,

I'm having a bit of a problem in C#. I have an MDI Form that contains a Treeview (directly on the form, docked left). The remaining space contains some MDI child windows.

When I programmatically add a treenode to the treeview at runtime, I want the user to be able to edit the default label text of the new node immediately. This saves the trouble of either a dialog to prompt for the name of the new element, or having the user click the node to change the name manually. The code I have so far:

        private void SelectAndEditNode(string id)
        {
            TreeNode[] temp = tvwProjects.Nodes.Find(id, true);
            if (temp != null && temp.Length > 0)
            {
                tvwProjects.SelectedNode = temp[0];
                tvwProjects.SelectedNode.EnsureVisible();
                tvwProjects.SelectedNode.BeginEdit();
            }
        }

The string id is the unique key and the Find function theoretically always returns one node. The code works fine, the node gets selected, is visible and the label of the node is in edit mode so the user can start typing a name.

The problem is, when I start typing a name, the label accepts one character and then the last active MDI child window is activated and the rest of what I type is put in the MDI child window:

Screenshot - 15-2-2007 , 10_16_48.pngC# TreeView problem

If there are no MDI child windows then it works fine. I tried tvwProjects.Focus() before calling BeginEdit() but that makes no difference.

Any one got an idea?
Regards,
RuffNekk

Programming is an art form that fights back.
« Last Edit: February 15, 2007, 03:20 AM by Ruffnekk »

wmain

  • Participant
  • Joined in 2006
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: C# TreeView problem
« Reply #1 on: February 22, 2007, 09:43 AM »
What if you disabled the MDI forms, if any, before enabling the edit mode of the new node. Then on the node's changed event you re-enable the MDI forms?

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TreeviewNodeEditMDI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Form2 childform = new Form2();
            childform.MdiParent = this;
            childform.Visible = true;
            childform.Enabled = false;
            TreeNode node = this.treeView1.Nodes.Add("Test");
            treeView1.LabelEdit = true;
            node.EnsureVisible();
            node.BeginEdit();
        }

        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            foreach (Form child in this.MdiChildren)
                child.Enabled = true;
        }
    }
}

Ruffnekk

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 332
  • Uhm yeah...
    • View Profile
    • RuffNekk's Crypto Pages
    • Donate to Member
Re: C# TreeView problem
« Reply #2 on: February 22, 2007, 09:52 AM »
Thanks for the tip! I'll check it out when I have time :) I might work I think!
Regards,
RuffNekk

Programming is an art form that fights back.