Hi Frnds,
i have four comboboxs that are dependant on each other selection.Its working fine but but what i want is to show "select an item" on zero selectedindex of each combobbox when form load.How can i do that???????what i have done is like as:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt2 As New DataTable("class")
        Dim sql2 As String
        sql2 = ("SELECT Code+' : '+ Name as Name from class")
        Dim da2 As New OleDbDataAdapter
        da2 = New OleDbDataAdapter(sql2, con)
        cmbclass.DropDownStyle = ComboBoxStyle.DropDownList
        cmbclass.DataSource = dt2
        cmbclass.DisplayMember = "Name"
        cmbclass.ValueMember = "Name"

AddHandler cmbclass.SelectedIndexChanged, AddressOf cmbclass_SelectedIndexChanged

the other combobxes are populating like same code ...

Recommended Answers

All 12 Replies

Ok, here we go, I`d suggest you to add an aditional row (on top, at index ) to dataTable, like:

'create table and bind it to comboBox:
Dim table As New DataTable()
table.Columns.Add("col1", GetType(String))
table.Rows.Add("item 1")
table.Rows.Add("item 2")
comboBox1.DataSource = table.DefaultView
comboBox1.DisplayMember = "col1"

'now add an item to dataTable:
Dim dr As DataRow = table.NewRow()
dr("col1") = "- Select items -"
table.Rows.InsertAt(dr, 0)
comboBox1.SelectedIndex = 0

well thnxs, its working but i m not getting "select Items" on form load event

What do you mean exactly with "I`m not getting "select Items" on form load event"?

i mean there is nothing like "select item" text when my form loads.its not displaying anywhere on form..

Member Avatar

Can't you just set the combobox's text property to "Select an Item" after you load them.

its not working sir as my first combobox shows the first data item by default on form load event

In form load event
ComboBox1.Text = "Select item"

In form load event
ComboBox1.Text = "Select item"

its not working..after applying ur code i m still getting the first data item in respective combobox.

Ok then somewhere you have made the combobox1.selectedindex=1.
Check your code

Member Avatar

The text property isn't actually going to work when the combobox's displaystyle is set to dropdownlist. Sorry.

You can either Do what Mitja posted which added a dummy row to the top of the table you are binding the combobox to, do not bind the combobox and add "Select an Item" and then add the records to the combobox with a datareader, set the displaystyle to dropdown and set the text property, or create your own combobox.

I created a custom combobox. Glad you created this thread because I have the perfect spot for this.

Add a class to your form and name it CustomCombobox. Copy and paste the following code into the class. Rebuild the solution and the control will be in your toolbox. Drag a custom combobox to the form and set its CustomText property to "Select an Item" and set its DrawCustomText property = true. Use the control the same way you use a standard combobox. In your form load event after you have set the combobox's datasource set the combobox's selectedindex = -1. The combobox will display "Select an Item" until a selection is made.

Public Class CustomComboBox : Inherits ComboBox

    Private fpCustomText As String
    Private fpDrawCustomText As Boolean
    Private fpHasNoSelection As Boolean

    Sub New()
        fpHasNoSelection = True
    End Sub

    Property DrawCustomText() As Boolean
            Return fpDrawCustomText
        End Get
        Set(ByVal value As Boolean)
            fpDrawCustomText = value
        End Set
    End Property

    Property CustomText() As String
            Return fpCustomText
        End Get
        Set(ByVal value As String)
            fpCustomText = value
        End Set
    End Property

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        'If the wm_Paint, 15, is passed then draw the custom text.
        If m.Msg = 15 AndAlso fpDrawCustomText AndAlso fpHasNoSelection Then
            Dim Graphics As Graphics = Me.CreateGraphics
            Dim TextRectangle As New Rectangle(0, 0, Me.Width - 15, Me.Height)
            Dim StringFormat As New StringFormat

            StringFormat.LineAlignment = StringAlignment.Center
            StringFormat.Trimming = StringTrimming.EllipsisWord
            StringFormat.FormatFlags = StringFormatFlags.NoWrap
            Graphics.DrawString(fpCustomText, Me.Font, Brushes.Black, TextRectangle, StringFormat)

        End If

    End Sub

    Protected Overrides Sub OnSelectedIndexChanged(ByVal e As System.EventArgs)

        'In the wndProc sub I check for fpHasNoSelection instead me.selectedindex = -1
        'what was happening is if you clicked the combobox and quickly moved your mouse
        'into the dropdown portion it wasn't displaying the custom text.
        'Me.selectedindex wasn't -1 even though it is.  This will ensure that the text is painted
        'when the user has not made a selection.
        If Me.SelectedIndex = -1 Then
            fpHasNoSelection = True
            fpHasNoSelection = False
        End If
    End Sub

End Class
Member Avatar

One more thing. This will only work when the combobox's dropdown style = dropdownlist. It would need to be modified to work with the other styles. So you should change the if statement in the wndProc to If Me.DropDownStyle = ComboBoxStyle.DropDownList AndAlso m.Msg = 15 AndAlso fpDrawCustomText AndAlso fpHasNoSelection Then

Hey Unhnd_exception Thanks. I was not knowing that he is using dropdown list... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.