vba 选择后如何在组合框中显示第二列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41574467/
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
How to display second column in ComboBox after selection?
提问by ASH
I can click on my ComboBox and see the values of Column1 and Column2, but after I click on off the ComboBox, the value in Column1 is always displayed and I want the value of Column2 displayed.
我可以单击我的 ComboBox 并查看 Column1 和 Column2 的值,但是在单击 ComboBox 后,始终显示 Column1 中的值,我希望显示 Column2 的值。
I tried this:
我试过这个:
With ComboBox2
.Value = "None"
.ColumnHeads = True
.ColumnCount = 2
.ColumnWidths = "50;100"
.RowSource = "SetupQuestions!A42:B48"
.BoundColumn = 2
End With
That didn't set the value as I thought it would.
这并没有像我想象的那样设定价值。
I tried this:
我试过这个:
Private Sub ComboBox2_AfterUpdate()
ComboBox2.Value = ComboBox2.Column(2)
End Sub
That didn't set the value as I thought it would.
这并没有像我想象的那样设定价值。
How can I force the ComboBox to display the value in Column2 after a selection is made?
选择后如何强制 ComboBox 显示 Column2 中的值?
回答by Jedap
The DropList of a ComboBox can show multiples columns, but after selecting a row, it can show only one column as Text. To show the second column use the property TexColumn.
ComboBox 的 DropList 可以显示多列,但是选择一行后,它只能显示一列作为文本。要显示第二列,请使用属性 TexColumn。
Me.ComboBox1.TextColumn = 2
回答by Paulo Jorge Gon?alves Pinto
Use a text field ond the right side of the combo box. Set the number of columns in the combo box to 2. Set the list width the size of both combo and text together (a+b) Set the columns width for the size of the combo and the size of the text (a;b) Since the columns property is an 0 based array of columns, set the source of the text field to "=[MyCombo].Columns(1)" to display the second field. When you drop down, the first column should be exactly under the combo box and the second column under the text box. When you update the combo, the text box should update its value accordingly. Additionally you may want to set the text box properties locked to true and enabled to false.
使用组合框右侧的文本字段。将组合框中的列数设置为 2。将列表宽度设置为组合和文本的大小 (a+b) 为组合的大小和文本的大小设置列宽 (a;b)由于 columns 属性是基于 0 的列数组,因此将文本字段的源设置为“=[MyCombo].Columns(1)”以显示第二个字段。当您下拉时,第一列应正好位于组合框下方,第二列应位于文本框下方。当您更新组合时,文本框应相应地更新其值。此外,您可能希望将文本框属性锁定为 true 并启用为 false。
回答by ASH
Argh! It's not .Value
啊!这不是 .Value
It's .Text
这是 .Text
Private Sub ComboBox2_AfterUpdate()
Me.ComboBox2.text = Me.ComboBox2.Column(1)
End Sub
回答by SJR
If you are only concerned with appearances, there is a workaround
如果您只关心外观,有一个解决方法
Private Sub ComboBox2_Click()
With ComboBox2
.Text = .List(.ListIndex, 0) & " | " & .List(.ListIndex, 1)
End With
End Sub
回答by Jim Poloski
I just came across here because I was looking to solve this too. but other people's response helped me find the answer. If you haven't gotten it yet, here is my solution.
我刚刚来到这里,因为我也想解决这个问题。但其他人的回应帮助我找到了答案。如果你还没有得到它,这是我的解决方案。
Private Sub ComboBox_AfterUpdate()
ComboboxList.Text = ComboboxList.Column(0) & " " & ComboboxList.Column(1)
End Sub
回答by Murrah
An alternative you can use without VBA.
您可以在没有 VBA 的情况下使用的替代方法。
Combo row source (adjust for your situation):
组合行源(根据您的情况进行调整):
SELECT Adults.aID, Trim([Adults].[LastName]) & ", " & Trim([Adults].[FirstName]) AS Expr1
FROM Adults WHERE ((Not (Adults.LastName)=("isNull")))
ORDER BY Adults.LastName, Adults.FirstName;
Basically, make your second column a composite single field via SQL.
基本上,通过 SQL 使您的第二列成为复合单个字段。
Bound column: 1, Column count: 2, Column widths: 0cm;4cm
绑定列:1,列数:2,列宽:0cm;4cm
You can use this technique to display whatever you want by building the string representation as a single field.
您可以使用此技术通过将字符串表示形式构建为单个字段来显示您想要的任何内容。
回答by Variatus
The reason is in your setting of ColumnWidth. Your combobox shows two columns. The second one can't be displayed because the total width of your box is insufficient. Therefore you see the first column only. Set the ColumnWidth to "0;100" and you will see the second column. Make sure that there is a working relationship between the width of the box and that of the columns to be displayed within it.
原因在于您对 ColumnWidth 的设置。您的组合框显示两列。第二个无法显示,因为您的框的总宽度不够。因此,您只能看到第一列。将 ColumnWidth 设置为“0;100”,您将看到第二列。确保框的宽度与要在其中显示的列的宽度之间存在工作关系。