Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 -

C#
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();
}
Posted
Updated 23-Dec-13 10:20am
v3
Comments
Nelek 23-Dec-13 6:55am    
If you tried it, where is the code or the description of What have you tried?[^]
You just wrote a serie of requirements and told "plz give me solution"
Gauri Bharde 23-Dec-13 8:01am    
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();
}
Gaurav Makwana 23-Dec-13 7:11am    
you need to give your effort then we will together try to solve
Gauri Bharde 23-Dec-13 8:02am    
this is my code plz check
private void btnAddNew_Click(object sender, EventArgs e)
{
if (radioButtonNewParent.Checked == true)
{

int index;
TreeNode Node=treeViewDept.Nodes.Add(textBoxNewParent.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();
}
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();
}
}

1 solution

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++;
                }
            }
        }
    }
}
 
Share this answer
 
Comments
Gauri Bharde 23-Dec-13 23:33pm    
thanks i will try it immediately.
Gauri Bharde 24-Dec-13 4:20am    
sir i have tried above code.Create New parent code are Sucessfully done.
but In existing parent node ,there is problem to assign treeview.selectednode=currentnode.
actually my Currentnode = new TreeNode(comboBox1.Text);
code as follows:

else // use an existing Node as Parent
{

currentNode = new TreeNode(comboBox1.Text);
treeViewDept.SelectedNode = currentNode;

if (currentNode == null)
{
if (treeViewDept.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(textBoxSubDept.Text.ToString() + ": added Child: " + addedNodeID.ToString());
currentNode.Nodes.Add(newChildNode);
newChildNode.EnsureVisible();
treeViewDept.SelectedNode = newChildNode;
addedNodeID++;
}
}

but still child node not added into existing parent node.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900