Identify and populate a listbox

喜夏-厌秋 提交于 2019-12-19 03:58:44

问题


It's a riddle for me: what is the syntax to populate a listbox? But first: how do you identify a listbox? In many forums I read: ListBox1.Additem... But how do they know it's 'ListBox1'?


回答1:


That's the default name for a ListBox control when you add it to your form. VB and VBA automatically name new or unnamed controls with the name of the type of control, suffixed with an integer identifier.

It's completely irrelevant what your control is called. The point of the sample code is to demonstrate a concept. You should replace ListBox1 with whatever your control is named.

And you should definitely name your controls something other than the default, because as you observe here, it's not very descriptive.

It used to be recommended by everyone to name controls following some type of quasi-Hungarian notation with a 3-letter prefix indicating the type of control, followed by your regular descriptive name. Over the past few years, there's been a big backlash against anything that looks like Hungarian notation, but I think it's still quite useful with regards to naming GUI controls, so I still use it. For a ListBox control, I might call it something like: lstCustomers or lstItemsForSale. But it's completely optional: again, what you choose to name your controls is irrelevant to how the code works or how the application will behave.

So, to populate a listbox in VB 6 and/or VBA, you'd use the following code, where myListBox is the name of your ListBox control:

' Add an item to the end of the list
myListBox.AddItem "Peaches"

' Insert an item at the top of the list
myListBox.AddItem "Apples", 0

' Remove the first item in the list
myListBox.RemoveItem 0



回答2:


ListBox1.AddItem is for loading a single column ListBox (CodyGrey's answer covers that).

If you are using a multi column ListBox (property .ColumnCount > 1), then you need to load an Array into .List. The following snippet loads 3 rows into a 2 column ListBox

Dim dat(0 To 2, 0 To 1) As Variant

dat(0, 0) = "A"
dat(0, 1) = "B"
dat(1, 0) = "C"
dat(1, 1) = "D"
dat(2, 0) = "E"
dat(2, 1) = "F"

ListBox1.List = dat

Accessing the List Box: (this will vary for different versions of Word, this is for 2003)

To access the ListBox properties:

  1. Display the "Control Toolbox" toolbar
  2. Go To Design Mode
  3. Click the control, and select Properties on the "Control Toolbox"
  4. The Name property should now be visible and editable
  5. Other properties, such as ColumnCount etc can also be set



回答3:


When using VBA in Access, a good way to fill in the whole listbox at once is to use the RowSource property when the ListBox is a ValueList. The AddItem method is a bit slow when adding many entries.

For example, to fill in a list of files from a directory, you could use:

Dim src As String
Dim S as String

S = Dir(FullPathToCurrentDirectory & "\*.*")

While Len(S) <> 0
        src = src & """" & S & """;"
        S = Dir
Wend
If Len(src) = 0 Then src = """"";"
Me.lstFiles.RowSource = Left(src, Len(src) - 1) ' leave off the last ;


来源:https://stackoverflow.com/questions/8689812/identify-and-populate-a-listbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!