【问题标题】:how to add a text for the selected text on a combobox如何为组合框上的选定文本添加文本
【发布时间】:2015-05-07 00:59:33
【问题描述】:

我有一个带有组合框的 Windows 应用程序。 这是我的代码:

Dim TitleVList As New List(Of TitleV_List)


    con = New SqlConnection(connectionString)

    'Fill the UOM Drop Down with all System UOM
    sql = "Select ID, Full_Name FROM cts_TitleV_Leads WHERE IsActive = 1"
    If con.State = ConnectionState.Closed Then
        con.Open()
    End If

    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    cmd.CommandText = sql

    dr = cmd.ExecuteReader




    If dr.HasRows = True Then
        While dr.Read

            TitleVList.Add(New TitleV_List(dr("Full_Name"), dr("ID")))

        End While
    End If

    If con.State = ConnectionState.Open Then
        con.Close()
        dr.Close()
    End If




    ddlpropTitleVlist.DataSource = TitleVList

    ddlpropTitleVlist.DisplayMember = "Full_Name"
    ddlpropTitleVlist.ValueMember = "ID"

我想要的是,当第一次显示组合框时,它会将“选择项目”显示为选定的文本,但是当他们单击下拉箭头时,“选择项目”不会显示在列表中。 我试过了

ddlpropTitleVlist.SelectedText = "选择项目"

ddlpropTitleVlist.Text = "选择项目"

但当屏幕显示时,它不会显示“选择项目”,它会显示列表中的名字。 我确实有一个选项卡控件,并且我正在使用 TabControl_DrawItem,因为我的选项卡是水平的。我不知道这是否与这不起作用有关。

请帮忙。

谢谢 马特

【问题讨论】:

    标签: vb.net winforms tabcontrol


    【解决方案1】:

    考虑:

    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        ComboBox1.SelectedIndex = 0
        ComboBox1.SelectedIndex = -1
    End Sub
    
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedIndex = -1 Then
            ComboBox1.Text = "Select Item"
        End If
    End Sub
    

    这个想法是,在设计时,您可以在 ComboBox 项目集合中添加任何您想要的选项。在运行时,在 Form.Activated 事件(Form.Load 也应该工作)中,ComboBox 的 SelectedIndex 属性设置为 -1。在 ComboBox 的 SelectedIndexChanged 事件中,如果索引为 -1,则将 Text 属性设置为占位符文本。请注意,为了使其正常工作(至少在我的系统上),我必须先将 SelectedIndex 设置为零,然后再将其设置为 -1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多