使用光标键选择Treeview节点 [英] Treeview node selection using cursor keys

查看:86
本文介绍了使用光标键选择Treeview节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在建立一个包含树视图控件和面板的小应用程序嘎然而止。我希望有人可以帮助我!



基本上我有一个treeview控件,我正在从数据库中添加检查器的名称。



我正在使用NodeMouseClick事件来运行一些代码,当点击鼠标按钮时,它会更新树视图右侧面板中的数据,一切正常。



我的问题是通过向上和向下键选择节点,节点突出显示,没问题,但我需要做的是调用NodeMouseClick事件来运行代码来更新面板中的数据,但我不知道如何做到这一点,因为我在事件中使用e.Node来确定在鼠标单击时选择了哪个节点。



我明白我可能需要从KeyPress事件传递正确的参数,但我不知道该怎么做



有人可以帮忙吗?



谢谢



这是我在NodeMouseClick事件中运行的代码< br $>


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

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();
    }
}







我想我需要使用KeyPress事件






I guess I need to use the KeyPress event

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
    }

}

推荐答案

假设这是WinForms,您正在使用Microsoft TreeView:



这很简单:
Assuming this is WinForms, and you are using the Microsoft TreeView:

This is very easy:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    currentNode = e.Node;
    
    // if not a root node
    if (currentNode .Parent != null)
    {
        // do your thing
    }
}

MS TreeView的默认行为当使用箭头键时, 更改当前选择的TreeNode。不仅支持向上/向下键,而且左/右键做正确的事来扩展或折叠当前具有子节点的节点......当然,没有更改SelectedNode。



当然,使用这种技术不会区分TreeView中的向上/向下移动:也许你需要做什么?

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 ?


第一步:

将鼠标事件中的所有代码转换为以Node为参数的函数。

从NodeMouseClick调用。



现在找到事件(如果你正在做网络,则选择NodeNodeChanged,如果你在WinForms中,则选择AfterSelect),这将触发箭头选择(实际上,它也会触发点击,所以你可能最终得到一个事件无论如何:))



调用你的函数接收来自该事件的节点参数,你就完成了。





如果这有帮助,请花些时间接受解决方案,以便其他人可以找到它。谢谢。
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.


这篇关于使用光标键选择Treeview节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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