你如何使组合框使用具有多个字符串和文本的字符串? vb.net

时间:2012-03-24 11:43:37

标签: vb.net

所以我试图让一个组合框使用一个包含其他字符串并且也有文本的字符串。 像这样:

Dim numbers() As String = {one, two, three, "four", "five"}
ComboBox1.text = numbers

我试图不止一次使用数字,所以我希望一切都在那里。我尝试使用:

Dim numbers As String = (one or two or three or "four" or "five")

但这也不起作用。有没有办法让这成为可能?我在互联网上寻求帮助,我得到的是数组列表和转换为字符串。我正在尝试制作一行包含所有内容的代码。

编辑:让我重新说一下。我正在尝试这样做:

Dim numbers As String = (one or two or three or "four" or "five")
ComboBox1.items.add(numbers)
If Combobox1.text = numbers Then
TextBox1.text = "is showing"
End if

3 个答案:

答案 0 :(得分:1)

您使用的第一个语法是创建一个数组。令您感到困惑的是您想要的最终结果,但如果您只是想加入String和Text,您可以使用 & 这是VB Concatenation运算符之一

Dim numbers As String = one & two & three & "four" & "five"

根据进一步澄清进行编辑。听起来你想要像DataBinding这样的东西。看看这段代码是否适合你。

 Dim numbers() As String = {one, two, three, "four", "Five"}
 ComboBox1.DataSource = numbers

尝试这样的事情 你可以用这种方式创建你的字符串

numbers = one & "," & two & "," & three & "," & "Four" & "," & "Five"

或者这样

Dim temp() As String = {one, two, three, "Four", "Five"}
numbers = String.Join(",", temp)

像LarsTech建议的那样填写您的ComboBox。

ComboBox1.Items.AddRange(numbers.Split(",", 5, StringSplitOptions.RemoveEmptyEntries))

并检查这样的匹配

Private Sub ComboBox1_TextChanged(sender As Object, e As System.EventArgs) Handles     ComboBox1.TextChanged
    If numbers.Contains(ComboBox1.Text) Then
        TextBox1.Text = "is showing"
    End If
End Sub

答案 1 :(得分:1)

和其他人一样,问题有点模糊:

你在找这个:

ComboBox1.Items.AddRange(New String() {"one", "two", "three", "four", "five"})

然后使用ComboBox的SelectedIndexChanged事件:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
  If ComboBox1.SelectedIndex = ListBox.NoMatches Then
    TextBox1.Text = String.Empty
  Else
    TextBox1.Text = ComboBox1.SelectedItem & " is selected."
  End If
End Sub

答案 2 :(得分:0)

如果这是你想要的......

尝试这样的事情..

public sub bla(byval x as string(), byref cb as combobox)
  cb.items.clear
  for each v as string in x 
    cb.items.add(v)
  next v
end sub

' call 
Dim numbers() As String = {"one", "two", "three", "four", "five"}
bla( combobox1, numbers)