通过文本框添加rootnode和子节点 [英] Add rootnode and child node through textbox

查看:66
本文介绍了通过文本框添加rootnode和子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我想在树视图中一次将父节点和子节点添加到treeview runtime.each中,并且每个父节点都有一个子节点强制

我的要求:

1.当用户点击添加按钮时。



2.屏幕上的面板显示,在该面板中,有两个单选按钮,1个文本框和按钮

i.Create New Parent-这用于创建新的父节点。当点击这个文本框时,启用节点名称到该文本框

ii。现有父级 - 如果用户想要将子节点添加到现有的Parentnode中,则使用此选项。当单击此显示时,所有父级将进入组合框。

iii。 textbox-enter将子名称输入到此。



当用户点击按钮父节点以及添加到treeview中的子节点时。如果存在父节点,则为
节点添加到相应的父节点。





实际上我试过这个但是我不能一次添加父节点和子节点treebut on singlebutton.all这个相应的值保存到access database.so plz给我解决方案



从下面的评论复制的其他信息

我的代码 -



actually i want to add parent node as well as child node in treeview at a time into treeview runtime.each and every parent node have one child node compulsory
My requirement:
1. when user clicks on add button.

2. Panel display on screen,in that panel,there are two radio buttons,1 textbox and button
i.Create New Parent-this is use for to create new parent node .when click on this textbox enabled give node name into that textbox
ii. Existing parent-this is use for if user want to add child node into existing Parentnode.when click on this display all parent into combobox.
iii. textbox-enter Child name into this.

when user clicks on button parent node as well as child node added into treeview.Or if there is existing parent node,then child node added into respective parent node.


actually i tried this but i cant added parent node as well as child node at a time in treeview on singlebutton.all this respective value save into access database.so plz give me solution

additional information copied from comment below
My code -

if (radioButtonNewParent.Checked == true)
{
   int index;            
   TreeNode Node=treeViewDept.Nodes.Add(textBoxNewParent.Text);
   treeViewDept.SelectedNode = Node;                        
   Node.BackColor = System.Drawing.Color.LightBlue;
   btnSave.Enabled = true;                
   btnSave.Focus();          
}
if (radioBtnexistingparent.Checked == true)
{
   TreeNode Node = new TreeNode(comExistingParent.Text);
   treeViewDept.SelectedNode = Node;
   TreeNode child = treeViewDept.SelectedNode.Nodes.Add(textBox1.Text);
   treeViewDept.SelectedNode = child;
   Node.BackColor = System.Drawing.Color.LightBlue;
   btnSave.Enabled = true;
   btnSave.Focus();
   treeViewDept.Nodes.Add(comExistingParent.Text, textBox1.Text);
   SearchAndAdd(comExistingParent.Text, textBox1.Text);                
   TreeNode node= treeViewDept.Nodes[comExistingParent.Text].Nodes.Add(textBox1.Text);
   treeViewDept.SelectedNode = node;
   btnSave.Enabled = true;
   btnSave.Focus();
}

推荐答案

这是假日季节,我没有时间/精力去问更多问题,等待您的响应,复制,运行和分析,您的代码,所以:我将写一个快速示例,说明如何实现您描述的内容,正如我现在所理解的那样。它可能不是你想要的,但是,希望你可以根据自己独特的情况调整你看到的一些东西。



我们假设:一个Win Form项目:在主窗体上,可能是某种ContainerControl,如Panel:



1.一个TreeView:'treeView1

2。两个RadioButtons:'rbNewParent,'rbUseExistingParent

3.一个按钮:'btnAdd

4.一个TextBox:'textBox1



两个RadioButton都连线使用相同的CheckChanged EventHandler。



行为:



1.用户点击'btnAdd



2.如果选中'rbNewParent RadioButton:创建一个新的父节点和子节点,并添加到TreeView的节点集合。



3.如果选中'rbUseExistingParent RadioButton:



a。如果TreeView中没有节点或当前没有选定节点,则会显示一个消息框,要求用户创建新节点或选择新节点。然后取消操作。



b。如果存在选定的节点,则创建新的子节点,并将其添加到新的父节点,并使用System将父节点添加到当前所选节点
It's holiday season, and I don't have the time/energy to ask further questions, wait for your response, copy, run, and analyze, your code, so: I'll just write a quick example of how you might implement what you describe, as I understand it now. It may not be exactly what you want, but, hopefully, you can adapt some of what you see here to your own unique case.

Let's assume: a Win Forms Project: on the Main Form, probably in some kind of ContainerControl like a Panel:

1. a TreeView: 'treeView1
2. two RadioButtons: 'rbNewParent, and 'rbUseExistingParent
3. a Button: 'btnAdd
4. a TextBox: 'textBox1

Both RadioButtons are wired-up to use the same CheckChanged EventHandler.

Behavior:

1. the user clicks on 'btnAdd

2. if the 'rbNewParent RadioButton is checked: a new Parent Node and Child Node are created, and added to the Nodes Collection of the TreeView.

3. if the 'rbUseExistingParent RadioButton is checked:

a. if there are no Nodes, or no currently selected Node, in the TreeView: a MessageBox is shown asking the user to either create a new Node, or to select a new node. Then the operation is cancelled.

b. if there is a selected Node, a new Child Node is created, and added to new Parent Node, and the Parent Node added to the Nodes Collection of the currently selected Node
using System;
using System.Windows.Forms;

namespace Dec23_CP_AddNodesExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // which mode of Node creation to use ?
        private bool isNewParent;

        // convenience variables
        private TreeNode currentNode;
        private TreeNode newParentNode;
        private TreeNode newChildNode;

        // for giving each created Node an ID so
        // verifying outcome is easier at run-time
        private int addedNodeID = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            // set defaults
            treeView1.ExpandAll();
            rbNewParent.Checked = true;
        }

        // both RadioButtons are 'wired-up to' this EventHandler
        private void rbTargetSelect_CheckedChanged(object sender, EventArgs e)
        {
            isNewParent = rbNewParent.Checked;
        }

        // all the work is done here
        private void btnAdd_Click(object sender, EventArgs e)
        {
            currentNode = treeView1.SelectedNode;

            if (isNewParent)
            {
                newParentNode = new TreeNode(textBox1.Text + ": added Parent: " + addedNodeID.ToString());

                addedNodeID++;
                
                newChildNode = new TreeNode("added Child: " + addedNodeID.ToString());

                newParentNode.Nodes.Add(newChildNode);

                treeView1.Nodes.Add(newParentNode);

                newChildNode.EnsureVisible();
                treeView1.SelectedNode = newChildNode;
                addedNodeID++;
            }
            else // use an existing Node as Parent
            {
                if (currentNode == null)
                {
                    if (treeView1.Nodes.Count == 0)
                    {
                        MessageBox.Show("Create a new Node, and then add Nodes to it.");
                    }
                    else
                    {
                        MessageBox.Show("Choose an existing Node to add the new Node to.");
                    }

                    return;
                }
                else
                {
                    newChildNode = new TreeNode("added Child: " + addedNodeID.ToString());
                    currentNode.Nodes.Add(newChildNode);

                    newChildNode.EnsureVisible();
                    treeView1.SelectedNode = newChildNode;
                    addedNodeID++;
                }
            }
        }
    }
}


这篇关于通过文本框添加rootnode和子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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