C# Treeview Indexing

I've got a bit of a problem with treeview and how the indexing of nodes works. In my program, I have a database that can contain any amount of users. Each user is separated by a carriage return (i.e. one user per line). I'm creating a treeview object that lists all users in the database. If the user clicks on a specific node, how do I refer to that node / handle it being selected, as I am dynamically making nodes from the database?

StreamReader getMembers = new StreamReader(@"[data]\db\users.db");
        List<string> mems = new List<string>();
        members.Nodes.Add("Members");
        while (!getMembers.EndOfStream)
        {
            mems.Add(getMembers.ReadLine());
        }
        foreach (string o in mems)
        {
            TreeNode n = new TreeNode(o);
            members.Nodes[0].Nodes.Add(n);
        }

Database & Program:

Answers


If you are trying to get the tree node that was selected you can achieve that by the TreeView.SelectedNode property... (http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.selectednode.aspx)

if you want to handle an event on treenode selected register the TreeView.AfterSelect event (http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.afterselect)

example:

private void TreeView1_AfterSelect(System.Object sender, 
    System.Windows.Forms.TreeViewEventArgs e)
{

    // Vary the response depending on which TreeViewAction
    // triggered the event. 
    switch((e.Action))
    {
        case TreeViewAction.ByKeyboard:
            MessageBox.Show("You like the keyboard!");
            break;
        case TreeViewAction.ByMouse:
            MessageBox.Show("You like the mouse!");
            break;
    }
}

Assuming you are using the standard forms treeview, it sounds like you want to subscribe to the event on the TreeView.AfterSelect.

// Handle the After_Select event.
private void TreeView1_AfterSelect(System.Object sender, 
    System.Windows.Forms.TreeViewEventArgs e)
{
  // If (TreeView1.SelectedNode...
}

Need Your Help

Creating c# object at runtime with dynamic fields and attributes

c#

I'm using an ORM sytem (sqlite-net) for my android application. I want to be able to create my DB mapping classes dynamically, and be able to add attributes to members, is this possible? I would ne...

Log errors in a file

erlang

How do I log all process crashes into a file instead of a tty ? I've read in the documentation that there are some standard Erlang modules that can do it (SASL, error_logger), but unfortunately hav...