我如何编码,只显示我选择的组合框?

时间:2015-06-12 19:39:01

标签: vb.net

textbox1.text = "OS = " combobox1.selecteditem + environment.newline + 
combobox2.selecteditem + environment.newline + combobox3.selecteditem + 
environment.newline    

使用Visual Basic 2010 我试图让显示器显示为 " OS ="我可以说我选择了" combobox3"我希望它看起来像 " OS = Windows" 而不是

" OS =

视窗"

我的代码完成,所以票价是

2 个答案:

答案 0 :(得分:0)

字符串连接应该用&而不是+。另外,删除environment.newline以消除换行符。

textbox1.Text = "OS = " & combobox1.SelectedItem & " " & 
combobox2.SelectedItem & " " & combobox3.SelectedItem 

如果我误解了这个问题并且你试图避免在没有价值的地方换行,你会做以下事情:

    Dim strResult As String = "OS = " & Environment.NewLine

    ComboBox1.SelectedIndex = 1

    If ComboBox1.SelectedItem IsNot Nothing Then
        strResult += ComboBox1.SelectedItem.ToString() & Environment.NewLine
    End If

    If ComboBox2.SelectedItem IsNot Nothing Then
        strResult += ComboBox1.SelectedItem.ToString() & Environment.NewLine
    End If

    If ComboBox3.SelectedItem IsNot Nothing Then
        strResult += ComboBox1.SelectedItem.ToString() & Environment.NewLine
    End If

    TextBox1.Text = strResult

答案 1 :(得分:0)

我会亲自使用Linq。您不必担心许多If语句并检查是否选择了某些内容,还要连接字符串,或者如果您打算添加或删除CheckBox's;它将抓住所有这些。如果您打算不包含某些内容,则需要修改查询以适应。这也经过了试验和测试。

  Dim q As List(Of String) = (From s As ComboBox In Me.Controls.OfType(Of ComboBox).Reverse() Where s.SelectedItem IsNot Nothing Select CStr(s.SelectedItem) & Environment.NewLine).ToList
  MsgBox("OS = " & String.Join("", q.ToArray)) 'Just for testing!