Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have come to a screeching halt with a small app I'm building which contains a treeview control and a panel. I was hoping somebody may be able to help me!

Basically I have a treeview control to which I'm adding names of inspectors from a database.

I am using the NodeMouseClick event to run some code which updates data in a panel on the right hand side of the treeview when the mouse button is clicked, all works fine.

My problem is node selection by means of the up and down keys, the nodes highlight, no problem, but what I need to do is call the NodeMouseClick event to run the code to update the data in the panel but I don't know how to do this because I'm using e.Node in the event to determine which node is selected on the mouse click.

I understand that I probably need to pass the correct arguments in from a KeyPress event but I'm not sure how to do it

Can anybody help?

Thank you

Here's the code I'm running in the NodeMouseClick event

C#
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    // Each time a new inspector is selected from the treeview list we
    // need to refresh the inspector view graph to display data for
    // the newly selected item

    if (e.Node.Parent != null)
    {
        TreeviewInspectorSelection = e.Node.Text;
        DisposeInspectorViewPanel();
        treeView1.Nodes[0].Nodes.Clear();
        CreateInspectorViewPanel();
    }
}



I guess I need to use the KeyPress event

C#
private void treeView1_KeyPress(object sender, KeyPressEventArgs e)
{
    // When the cursor UP & DOWN keys are pressed we
    // need to identify which node is now selected so that
    // the nodemouseclick event can be called for updating
    // of the inspector view

    if (e.KeyChar == Convert.ToChar(Keys.Down))
    {
        //Code
    }

    else if (e.KeyChar == Convert.ToChar(Keys.Up))
    {
        // Code
    }

}
Posted
Comments
Sergey Alexandrovich Kryukov 16-Sep-14 10:23am    
There is no such concept as "call an event". You can invoke an event, but only in the class where the event is declared. That said, you cannot invoke the event you mentioned. The key is: this is never needed. The lack of such invocation is the important fool-proof feature of CLI.
—SA

Assuming this is WinForms, and you are using the Microsoft TreeView:

This is very easy:
C#
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    currentNode = e.Node;
    
    // if not a root node
    if (currentNode .Parent != null)
    {
        // do your thing
    }
}
The default behavior of the MS TreeView is to change the currently selected TreeNode when the Arrow keys are used. Not only are the Up/Down keys supported, but the Left/Right keys "do the right thing" to expand, or collapse, a current Node with Child Nodes ... without, of course, changing the SelectedNode.

Of course, using this technique does not discriminate between an Up/Down move in the TreeView: perhaps that something you need to do ?
 
Share this answer
 
Comments
Member 11048941 16-Sep-14 10:05am    
Excellent! To be honest I'd looked at and tried the AfterSelect event but it was creating me some problems with some dynamic node selection I had coded, but your answer enticed me to revert to this event and modify the rest of my code to stop the undesirable behaviour I was experiencing.

Anyway it works like a charm - thank you!
BillWoodruff 16-Sep-14 10:30am    
Glad the code was useful ! cheers, Bill
Sergey Alexandrovich Kryukov 16-Sep-14 10:31am    
Correct, but there is on more general thing to it, OP's understanding of events. Please see my comment to the question.

I voted 4. It's not 5 due to a completely different reason. Formally speaking, the name treeView1_AfterSelect only suggests that this method is added as a handler using += operator. It's always good to show this '+='. Also, it's not good to encourage such names which violate (good) Microsoft naming conventions. The conventions are broken because this is auto-generated name (automatic generation cannot do any better). The point is to never use auto-generated names, they are meant to be renamed using refactorization engine.

(I even strongly recommend to avoid adding any handlers in designer and use anonymous methods, but this point can a matter of discussion.)

—SA
Sinisa Hajnal 17-Sep-14 2:12am    
Naming is not solutions problem, but of the original code. My vote of 5.
Sergey Alexandrovich Kryukov 17-Sep-14 3:11am    
I know and don't mind. I just say that encouraging bad coding practices in an answer is not good. Yes, it does explain how to resolve the problem.
—SA
Fist step:
Take all the code from mouse event into a function that takes Node as parameter.
Call if from NodeMouseClick.

Now find the event (SelectedNodeChanged if you're doing web, AfterSelect if you're in WinForms) which will trigger on arrow selection (actually, it would trigger on the click too so you may end up with one event anyhow :) )

Call your function which receives node parameter from that event and you're done.


If this helps, please take time to accept the solution so others may find it. Thank you.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Sep-14 10:24am    
You correctly explain what to do instead of trying to invoke such event (but don't quite explain why; please see my comment to OP's question). The problem is: this is not really needed, as Solution 2 explains.
—SA
Sinisa Hajnal 17-Sep-14 2:11am    
I didn't explain HOW, question author seems to know how to do that. I will explain better in the next answer(s). Thank you.
Member 11048941 16-Sep-14 11:49am    
I appreciate the feedback, I'm merely a beginner so coming here for help is, I'd say, part of learning how to do the job properly. Thank you all for your help it is very much appreciated.

Ivan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900