Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem displaying dictionary keys 1

Status
Not open for further replies.

OMoore

Technical User
Oct 23, 2003
154
EU
Hi all,

I've created a dictionary and need to get the key values displayed on my form at runtime. The dictionary was created as follows:

Private Sub Form_Load()
Dim def ' Create the variable.
Set def = CreateObject("Scripting.Dictionary")
def.Add "a", "Athens" ' Add keys and items.
def.Add "b", "Belgrade"
def.Add "c", "Cairo"
End Sub

I'd like to get the keys (a,b,and c) to appear in a listbox on my form. I've searched far and wide for examples but have not had much luck.

Any ideas?

Owen
 
Code:
  Dim dicX As Scripting.Dictionary
  Dim varK As Variant
  Dim varI As Variant
  Dim lngX As Long
  Dim itmX As ListItem
  
  Set dicX = New Scripting.Dictionary
  dicX.Add "A", "Athens"
  dicX.Add "B", "Belgrade"
  dicX.Add "C", "Cairo"
  
  varK = dicX.Keys
  varI = dicX.Items
  
  For lngX = 0 To dicX.Count - 1
    Set itmX = ListView1.ListItems.Add
    With itmX
      .Text = varI(lngX)
      .SubItems(1) = varK(lngX)
    End With
  Next lngX

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Thanks Andy.

I'm getting a compile error for "itmX as ListItem" ..
User-defined type not defined

Owen
 
I'm a newbie so sorry for the silly questions or comments!

I have the scripting runtime library referenced. I have a list box created on my form. How do I associate the listbox with the code above? Will doing this remove the error message?

thanks for your help.
Owen
 
Now you see why you should keep all of one thread together! You need a ListView control, not a ListBox. See my reply in thread222-966659

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Oops! My bad! I read "ListView" not "ListBox". Sorry!

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top