C# WinForms: I can not override OnNodeMouseDoubleClick event for a TreeView

Posted on 2012-09-15
Last Modified: 2012-09-16
I created a new object which inherits from the TreeView I created in the designer.  In the C#, I:

- hide that TreeView
- I construct a new TreeView based on the child class
- assign the value to it from the designer TreeView, to save work for myself
- I show the new TreeView and add it to the Controls list for the form

But, when I double click a collapsed node it expands the node before hitting the breakpoint in my overriden function:

        protected override void OnNodeMouseDoubleClick(TreeNodeMouseClickEventArgs e)

I want to stop the collpase expand behavior and get that under my control, so I can only expand/collapse when I choose to.

What am I doing wrong?
Question by:newbieweb
    LVL 13

    Accepted Solution

    This is default behavior of treeview but you can handle it like following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace WindowsFormsApplication2
        public class MyTreeView: TreeView
            bool isCancelExpand = false;
            protected override void OnBeforeExpand(TreeViewCancelEventArgs e)
                e.Cancel = isCancelExpand;
                isCancelExpand = false;
            protected override void OnMouseDown(MouseEventArgs e)
                isCancelExpand = e.Clicks > 1;

    Open in new window


    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now