如何选择一个节点? [英] how to select a node?

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

问题描述

您好,我正在一个项目上,我正在使用Treeview工具在一侧组织一些功能,在另一侧组织一个RichTextBox,以提供对每个功能的一些描述.

到目前为止,我知道如何使用属性菜单创建节点,但是在对其执行事件时遇到了一些麻烦.例如,我想要这样,以便当某人双击某个节点时将触发一个if命令,该命令将在选择一个节点时在Richtextbox上显示文本,以提供对该特定节点的描述.

我正在使用Visual C#,并且我的应用程序是窗口形式

谢谢,

Hi, I am working on a project and I am using the treeview tool to organize some functions on one side and a richtextbox on the other providing some descriptions of each one.

So far I know how to create a node using the properties menu but I am having some trouble doing events on them. For example I want it so that when someone double clicks on a node it will fire an if command that when a node is selected it will display a text on the richtextbox providing the description for that particular node.

I''m using Visual C# and my application is a window form

Thanks,

推荐答案

您正在询问节点的选择,但是通过描述行为,您似乎需要处理节点本身的选择.

首先,在处理事件时将一些相关数据放入每个节点中.
You are asking about selection of the node, but by description of the behavior it looks like you need to handle selection of the node itself.

First of all, put some relevant data in each node to use while processing event.
internal struct TreeNodeData {
    internal TreeNodeData(string name, string description) { fName = name; fDescription = description; }
    internal string Name { return fName; }
    internal string Description { return fDescription; }
    string fName, fDescription;
}



将这种类型的项目添加到树视图中,添加更多相关数据(如您所说,使用命令).我建议您添加一个委托实例,该实例将运行执行命令.

您应该将此结构的实例分配给要处理的每个节点的属性Tag.

现在,我认为您无需单击即可显示描述.描述应在选择时显示.像这样设置:



Add the items of this type to your tree view, add more relevant data (command, as you say). I would suggest you add a delegate instance which will run execute the command.

You should assign an instance of this structure to the property Tag of each node you want to process.

Now, I don''t think you need a click to show description. The description should be shown on selection. Set it up like this:

myTreeView.AfterSelect += (sender, eventArgs) => {
    if (eventArgs.Node.Tag == null) return;
    TreeNodeData nodeData = (TreeNodeData)eventArgs.Node.Tag; //this is selected node because "After Select"
    ProcessTreeViewNodeSelection(nodeData); //all clear, not more type casts
}

myTreeView.NodeMouseDoubleClick += (sender, eventArgs) => {
    TreeViewNode selectedNode = eventArgs.Node;
    if (selectedNode.Tag == null) return;
    TreeNodeData nodeData = (TreeNodeData)selectedNode.Tag;
    RunTreeViewCommand(nodeData);
}

void ProcessTreeViewNodeSelection(TreeNodeData nodeData) {
    textBoxNodeDescription.Text = nodeData.Description; //assuming your text box was added earlier
    //...
}

void RunTreeViewCommand(TreeNodeData nodeData) {
    // you did not ask about this part, so this is on you
    // I would suggest you store some delegate instance in each node,
    // and to implement a command you add some handler to the invocation list of each delegate instance
    // and just invoke the delegate instance right here
}



上面显示的两个事件处理程序应在显示表单之前添加(+ =).我通常在InitializeComponent之后调用在构造函数中添加代码的处理程序.
现在清楚了吗?

祝你好运,

—SA



Both event handlers shown above should be added (+=) before the form is shown. I usually call the code adding handlers in the constructors after InitializeComponent.

Is it clear now?

Good luck,

—SA


1.添加树视图和文本框.

Form1.Designer.cs中的示例代码:

1.Add a treeview and a textbox.

Sample Code from the Form1.Designer.cs:

private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.TextBox textBox1;



2.将事件处理程序添加到树视图.该事件称为nodemouseclick.查看事件列表,您会找到它.

来自Form1.Designer.cs的有关事件处理程序的示例代码:



2.Add a event handler to the treeview. The event is called nodemouseclick. See the events list and you will find it.

Sample code about eventhandler from Form1.Designer.cs:

this.treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);



3.现在将代码放入Form1.cs:



3.Now the code to put in Form1.cs:

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    textBox1.Text = e.Node.Text;
}


这篇关于如何选择一个节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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