hi can any one please send ,first i select an item to combobox it will be displayed in tree
view next if select the same item it will not add we add different then it will
added to treeview

Dani AI

Generated

Quick, practical fix and why it works (builds on suggestions from , , and )

Rather than blindly appending the ComboBox text (or tacking on a counter), give each node a stable key and check for that key before adding. If the ComboBox is data-bound use the item id (SelectedValue); otherwise normalize the display text (trim + lowercase) and use it as the key. Use the TreeView API to test existence instead of iterating nodes yourself.

Example pattern (C#) — compute a stable key, test, then add:

string key = comboBox1.SelectedValue != null
    ? comboBox1.SelectedValue.ToString()
    : comboBox1.Text.Trim().ToLower();

if (!treeView1.Nodes.ContainsKey(key))
{
    treeView1.Nodes.Add(key, comboBox1.Text);
}
  • Use a key-based check with TreeNodeCollection.ContainsKey or TreeNodeCollection.Find when you need to search children (ContainsKey, Find).
  • If the ComboBox is data-bound prefer SelectedValue as the key ().
  • Watch out for whitespace and case differences — normalize before checking. For large trees keep a separate HashSet of keys for O(1) checks instead of a repeated tree search.
  • If using a web/JavaScript tree widget, the same idea applies: compute a stable id and check the widget or a Set before inserting.

This avoids brittle counter hacks and prevents accidental duplicates even across tree levels.

Recommended Answers

All 3 Replies

for adding a selected combo item in tree view following is the line of code,

"Me.TreeView1.Nodes.Add(Me.ComboBox1.Text)"

To avoid duplicate addition you need to check that before adding the item to the tree view.

hi,
try creating a different key for each node u create.i think that will work.key should be unique in treeview nodes.

Me.TreeView1.Nodes.Add(Me.ComboBox1.Text & i)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.