TreeView重新抓取重点Ctrl + Click [英] TreeView re-grabs focus on Ctrl+Click

查看:124
本文介绍了TreeView重新抓取重点Ctrl + Click的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms TreeView控件,我想根据当前选择的节点打开另一个窗体。我希望在Ctrl +单击节点时打开其他表单。

I have a WinForms TreeView control that I would like to use to open another form based on which node is currently selected. I want to open that other form when I Ctrl+Click on the node.

目前,如果我在DoubleClick处理程序中打开另一个表单, (并双击节点,很明显);但是,如果我使用Click(或MouseClick)处理程序,并在按下Control键时打开其他窗体,则会正确打开其他窗体,但将焦点返回到原始窗体。

Currently, it works the way I would like if I open the other form in a DoubleClick handler (and double-click on the node, obviously); however, if I use a Click (or MouseClick) handler and open the other form when the Control key is pressed, it opens the other form correctly but returns focus to the original form.

如何在打开其他表单后保持焦点不返回原始形式(我仍然希望保持打开)?为什么Click和DoubleClick处理程序之间存在不同的行为?

How do I keep focus from returning to the original form (I do still want to keep it open) after opening the other form? Why is there different behavior between the Click and DoubleClick handlers?

推荐答案

TreeView在事件返回后抢回焦点。很烦人。你可以使用一个技巧:使用Control.BeginInvoke延迟事件的动作:

TreeView steals the focus back after the event returns. Very annoying. You can use a trick: delay the action of the event with Control.BeginInvoke:

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
  this.BeginInvoke(new TreeNodeMouseClickEventHandler(delayedClick), sender, e);
}
private void delayedClick(object sender, TreeNodeMouseClickEventArgs e) {
  // Now do your thing...
}

一旦TreeView的所有事件都完成运行,并且程序进入空闲状态并重新进入消息循环,delayedClick方法就会运行。

The delayedClick method runs as soon as all the events for the TreeView have finished running and your program goes idle and re-enters the message loop.

这篇关于TreeView重新抓取重点Ctrl + Click的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆