将项目动态添加到 Combobox VB.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24420795/
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
Dynamically add Items to Combobox VB.NET
提问by smr5
I have 2 combobox in windows application. I want to programmatically add Items to second combobox based on what is selected in the first combobox.
我在 Windows 应用程序中有 2 个组合框。我想根据在第一个组合框中选择的内容以编程方式将项目添加到第二个组合框。
This is what I do in the selected index change of the combobox:
这是我在组合框的选定索引更改中所做的:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
If ComboBox1.SelectedIndex = 0 Then
ComboBox2.Items.Add("MM")
ComboBox2.Items.Add("PP")
ElseIf ComboBox1.SelectedIndex = 1 Then
ComboBox2.Items.Add("SMS")
ElseIf ComboBox1.SelectedIndex = 2 Then
ComboBox2.Items.Add("MMS")
ComboBox2.Items.Add("SSSS")
End If
Catch ex As Exception
End Try
End Sub
It works fine, however, if I keep selecting different items it's keep adding the value over and over. I would like to add those values only once.
它工作正常,但是,如果我不断选择不同的项目,它会一遍又一遍地增加价值。我只想将这些值添加一次。
Also, when I add an item I would prefer to add an ID with the item description. I tried:
此外,当我添加一个项目时,我更愿意添加一个带有项目描述的 ID。我试过:
ComboBox2.Items.Add("SSSS", "1")
It seems that it's not working.
它似乎不起作用。
Any suggestions?
有什么建议?
回答by illumi
try this
尝试这个
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
If ComboBox1.SelectedIndex = 0 Then
If Not (ComboBox2.Items.Contains("MM")) And Not (ComboBox2.Items.Contains("PP")) Then
ComboBox2.Items.Add("MM")
ComboBox2.Items.Add("PP")
End If
ElseIf ComboBox1.SelectedIndex = 1 Then
If Not (ComboBox2.Items.Contains("SMS")) Then
ComboBox2.Items.Add("SMS")
End If
ElseIf ComboBox1.SelectedIndex = 2 Then
If Not (ComboBox2.Items.Contains("MMS")) And Not (ComboBox2.Items.Contains("SSSS")) Then
ComboBox2.Items.Add("MMS")
ComboBox2.Items.Add("SSSS")
End If
End If
Catch ex As Exception
End Try
回答by ilans
Regarding:
关于:
"Also, when I add an item I would prefer to add an ID with the item description"
“另外,当我添加一个项目时,我更愿意添加一个带有项目描述的 ID”
You can use the AddRange
function of the ComboBox or make a list as I show here and use it as a datasource - as @Plutonix mentioned in his comment.
您可以使用AddRange
ComboBox的功能或制作我在此处显示的列表并将其用作数据源 - 正如@Plutonix 在他的评论中提到的那样。
With it you can add an array of objects of a type that holds a value and a description, given that you override the ToString
base function (of Object
class).
有了它,您可以添加一个包含值和描述的类型的对象数组,前提是您覆盖了ToString
(Object
类的)基函数。
Here is such a class:
这是这样一个类:
Public Class CodeAndDescription
Public code As String
Public description As String
Public Overrides Function ToString() As String
Return Me.code + " - " + description
End Function
End Class
Now make a list of that upper class and add it to the combobox. Something like:
现在列出该上层阶级并将其添加到组合框。就像是:
Dim lstItems As List(Of CodeAndDescription) = GetList()
yourComboBox.Items.Clear()
yourComboBox.Items.AddRange(lstItems.ToArray)
Don't forget to Cast the retrieved object when you take it out of the combo:
当您将检索到的对象从组合中取出时,不要忘记 Cast:
Dim codeDesc As CodeAndDescription = TryCast(yourComboBox.Items(0), CodeAndDescription)
I've done this on a check list, but I think the principle is the same for a ComboBox.
我已经在检查清单上完成了这项工作,但我认为 ComboBox 的原理是相同的。