TreeView.Clear() method not clearing keys?

Post

Posted
Rating:
#1 (In Topic #1328)
Regular
chrisRoald is in the usergroup ‘Regular’
Hi,
My main form's only TreeView displays added icon entries fine, but when I come to clear it for a fresh set of icon additions, I get an error "key already used"  :o when I code:-

Code (gambas)

  1. treeView1.add(key, label, picture)
  2. treeView1.add(key2, label2, picture2)        '.add method in a loop
  3. ... ... ...
  4. treeView1.clear()
  5. ... ... ...
  6. treeView1.add(key3, label3, picture3)
where key - which should be cleared - and key3 have the same value.  

Am I doing something wrong?  Can I clear the keys without having to loop through all the entries (potentially hundreds), deleting them?!

Thanks,
C.
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
Your snippet of code does not make much sense or explain what is going on with your variables key, key2 and key3. (what is their value?)

Nothing will happen to your key key2 key3 variables they will be whatever you set them to.

this code..

Code (gambas)

  1. TreeView1.Add("Key1", "Key1")
  2. TreeView1.Clear
  3. TreeView1.Add("Key1", "Key1")
  4.  
gives no errors

TreeView.Clear DOES clear the keys

The keys are stored in TreeView1.Keys
 the Key you provide in TreeView1.Add(Key, Text, Pic) gives the TreeView item an identifier
Eg.

Code (gambas)

  1.  
  2. Dim Key2 As String = "Unique2"
  3. TreeView1.Add(Key2, Key2)
  4.  
  5. ' now the Key name can be used to access the item
  6. TreeView1[Key2].Selected = True
  7.  
  8.  

You understand by "Key" it means a unique name ,nothing to do with keyboard yes?

the code…
TreeView1.Add(Key2, "Key2")
means Key2 must be a string variable holding a key name like Dim Key2 As String = "Keyname2"

post more complete code so we can see the errors better and not try to guess :)
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
Just a note:

To make a variable (that's not an object) have its value changed in a function parameter the ByRef keyword must be used on both sides of the command.
eg..

Code (gambas)

  1.  
  2. Public Sub MyFunction(ByRef Value As Integer)
  3.  
  4.  If Value < 100 Then Value += 100
  5.  
  6.  
  7. Public Sub Main()
  8.  
  9.   Dim iVal As Integer = 10
  10.   MyFunction(ByRef iVal)
  11.   Print iVal
  12.  
  13.  
110

if you do not see the ByRef then a non object datatype will never be affected.

object datatypes and arrays work different as they are already pointers.

for example the following works fine without ByRef

Code (gambas)

  1.  
  2. Public Sub MyFunction(Value As Integer[])
  3.  
  4.   For c as Integer = 0 To Value.Max
  5.     If Value[c] < 100 Then Value[c] += 100
  6.   Next
  7.  
  8.  
  9. Public Sub Main()
  10.  
  11.   Dim aVal As Integer[] = [10, 20, 30]  ' an array is an object pointer
  12.   MyFunction(aVal)
  13.  
  14.   For Each iVal As Integer In aVal  
  15.     Print iVal
  16.   Next
  17.  
110
120
130

Hope that makes sense ,, and helps :)
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
chrisRoald is in the usergroup ‘Regular’
Thanks for the reassuring info Bruce,
…This 'error' case was my inattentive debugging, and garbage-In > garbage-Out data (a badly constructed .json input file :roll: )
so I was adding clashing keys AFTER my treeView.clear !

The byRef usage is new to me, so an additional way of changing values beyond a function's Return expression  :)

Thanks again.
C.
Online now: No Back to the top
1 guest and 0 members have just viewed this.