?
Solved

Treeview key node test

Posted on 2009-05-06
2
Medium Priority
?
365 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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

752 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