开发者

Can i grab Click and DoubleClick in a node treeview C#?

I'm trying to define different handlers for click and double click events in a node in a TreeView, but the click event is always fired and the double ignored. Can I have both working?

try 1

    private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        MessageBox.Show(e.Node.Text.ToUpper());
    }
    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        MessageBox.Show(e.Node.Text);
    }

try2

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        switch (e.Clicks)
        {
            case 开发者_如何学运维1:
                MessageBox.Show(e.Node.Text); break;
            case 2:
                MessageBox.Show(e.Node.Text.ToUpper()); break;
            default:
                break;
        }
    }


you can try this method... but this is not a proper way

need a timer control, say timer1, set interval 500.

timer1.Interval = 500;

declare a variable to check mouse click

static int mClick = 0;

in the treeview mouse down

private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            mClick++;
            timer1.Enabled = true;
        }

in the timer tick

private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            if (mClick == 1)
            {
                mClick = 0;
                MessageBox.Show("single click");

            }
            if (mClick == 2)
            {
                mClick = 0;
                MessageBox.Show("double click");
            }

        }

this is working properly in my side.

rest is up to you...


As I suspected, the problem is that you're showing a MessageBox on the Click event. When that message box gets displayed, it "hogs" all input (message boxes are modal dialogs) and the user's second click on the TreeView doesn't get recognized.

Hans Passant's answer already provides a detailed explanation of how Windows determines if a click event is a Click or DoubleClick, so I won't rehash that here.

However, the solution is simple: Remove the MessageBox.Show statements from your event handlers and everything will work as expected. You'll need to find some other way to alert the user to the node that was selected (if that's even necessary), but that will turn out to be a blessing in disguise. A message box popping up every time a node is clicked on is not exactly a friendly UI. For your particular scenario (at least as best I can tell), there's no need for more complicated techniques like a timer.

This also presents an important larger lesson in debugging code involving Windows user interface elements. Any time you throw a MessageBox into the mix, you run the risk of breaking the delicate sequence of events. The best drop-in replacement is probably a call to Debug.WriteLine instead.


This is by design, you always get the Click event first when the user double-clicks. Knowing that she intended to double-click requires a time machine. You can create one, start a Timer in the Click event and set its Interval to SystemInformation.DoubleClickTime + 45.

When you get the DoubleClick event set the timer's Enable property to false and do the double-click action. When you get the Tick event, set the timer's Enable property to false and do the single-click action. It doesn't make for a great user interface experience.

You didn't get the DoubleClick event because of the message box, it jerks the focus away.


You can check number of clicks in Click event handler. It should be in EventArgs instance.


If you remove the click handler from the code above then the double click handler should fire. Because the click handler shows a dialog, there is no way for the second click of the double click to reach the double click handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜