Solved

Treeview key node test

Posted on 2009-05-06
2
353 Views
Last Modified: 2012-05-06
Hello,

I  am trying to come up with a way to test is a key is already present in a treeview before I add a node. My treeview is populated from a flat file and it is possible (though unlikely) that the file could contain duplicate records. If this is the case I would like to notify the user via a MsgBox of the duplicate. Currently the duplicates are caught by a standard error handler that traps the 35602 - key is not unique in collection message. However I would like to test for a duplicate before the new node is added (and fails).

Ex:
if treeView.Nodes("key-to-check") is nothing then
 ' add the node
else
 msgbox "Error, key-to-check already exists."
end if

Currently when I try to use this method I receive a different runtime error, something relating to the node could not be found. My node keys are not something I am willing to change as they are referenced in many places.

Is there a way I can check to see if a node with a specific key already exists without having to trap an error?

Thanks in advance for any and all suggestions.
0
Comment
Question by:rite_eh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 total points
ID: 24320516
The easiest way is using the containskey function of a treeview

so assuming that a node was added using

dim primaryNode as treenode= New TreeNode(sometext)
                Me.TreeView1.Nodes.Add(primaryNode)

Then you can see if a node already exists using

if TreeView1.Nodes.ContainsKey(sometext) then

    msgbox "Error, key-to-check already exists."

else

  ' add the node

endif
0
 
LVL 1

Author Comment

by:rite_eh
ID: 24320788
This is a .net solution, but I am writing in both so good enough for me.

Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question