vba 识别并填充列表框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8689812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identify and populate a listbox
提问by jroeleveld
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'?
这对我来说是个谜:填充列表框的语法是什么?但首先:您如何识别列表框?在许多论坛中,我读到:ListBox1.Additem
...但他们怎么知道它是“ListBox1”?
回答by Cody Gray
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 typeof control, suffixed with an integer identifier.
这是将ListBox
控件添加到表单时的默认名称。VB 和 VBA 自动使用控件类型的名称命名新的或未命名的控件,并以整数标识符为后缀。
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.
这与您的控件名称完全无关。示例代码的重点是演示一个概念。您应该ListBox1
用您的控件命名的任何内容替换。
And you should definitelyname your controls something other than the default, because as you observe here, it's not very descriptive.
并且您绝对应该将您的控件命名为默认值以外的其他名称,因为正如您在此处观察到的那样,它不是很具有描述性。
It used to be recommendedby 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.
它曾经被建议由大家在与显示控件的类型,其次是你的常规描述性名称的3字母前缀某种类型的准匈牙利命名法来命名的控制。在过去的几年里,任何看起来像匈牙利符号的东西都受到了强烈反对,但我认为它在命名 GUI 控件方面仍然非常有用,所以我仍然使用它。对于ListBox
控件,我可能会称其为:lstCustomers
或lstItemsForSale
。但这完全是可选的:同样,您选择为控件命名的内容与代码的工作方式或应用程序的行为方式无关。
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:
因此,要在 VB 6 和/或 VBA 中填充列表框,您需要使用以下代码,其中myListBox
是您的ListBox
控件名称:
' 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
回答by chris neilsen
ListBox1.AddItem
is for loading a single column ListBox (CodyGrey's answer covers that).
ListBox1.AddItem
用于加载单列 ListBox (CodyGrey 的回答涵盖了这一点)。
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
如果您使用的是多列 ListBox(属性.ColumnCount
> 1),则需要将 Array 加载到.List
. 以下代码段将 3 行加载到 2 列 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)
访问列表框:(这会因 Word 的不同版本而异,这是 2003 年的)
To access the ListBox properties:
要访问 ListBox 属性:
- Display the "Control Toolbox" toolbar
- Go To Design Mode
- Click the control, and select Properties on the "Control Toolbox"
- The Name property should now be visible and editable
- Other properties, such as ColumnCount etc can also be set
- 显示“控件工具箱”工具栏
- 进入设计模式
- 单击控件,然后在“控件工具箱”上选择“属性”
- Name 属性现在应该可见且可编辑
- 还可以设置其他属性,例如 ColumnCount 等
回答by Bill
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.
在 Access 中使用 VBA 时,一次填充整个列表框的好方法是当 ListBox 是 ValueList 时使用 RowSource 属性。添加许多条目时,AddItem 方法有点慢。
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 ;