none
VB .net Combo Box property

    Question

  • Hi. I'm using VB .net 2003 windows form. In one form I am using Combo Box and it contains list of values.

    I have entered those values through "Collection" property. I have disable the editable property by combobox.DropDownStyle=DropDownList.

     

    I want to show the default text "Please select" in combo box in runtime. But it is not possible to set Combobox.text=please select if I choose combobox.DropDownStyle=DropDownList.

     

    So what should be the code for that?

    Wednesday, June 20, 2007 6:54 AM

Answers

  •  

    Hi anish,

     

    Like Dave have said, we can add items to the ComboBox's items property at run time. We can delete this item when drop-down portion of a ComboBox is shown. Try something like this:

    Code Snippet

    Public Class Form2

        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

            Me.ComboBox1.Items.Insert(0, "Please select ...")

            Me.ComboBox1.SelectedItem = "Please select ..."

        End Sub

     

        Private Sub ComboBox1_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown

            Me.ComboBox1.Items.Remove("Please select ...")

        End Sub

    End Class

     

    Hope this helps.

    Regards

    Friday, June 22, 2007 12:15 PM
  • Put a label on top of the combobox.  Set AutoSize = False and make it just big enough so it covers the textbox portion of the combo.  In the SelectedIndexChanged event handler set the label's Visible property to false.
    Wednesday, June 20, 2007 9:23 AM
    Moderator

All replies

  • The text property is not available for that enumeration type. You will have to add an item with the text you like.

    In your programs Load event or something add the following code:
    this.combobox.Items.Insert(0, "Please select an item...");
    Wednesday, June 20, 2007 8:34 AM
  • Put a label on top of the combobox.  Set AutoSize = False and make it just big enough so it covers the textbox portion of the combo.  In the SelectedIndexChanged event handler set the label's Visible property to false.
    Wednesday, June 20, 2007 9:23 AM
    Moderator
  •  

    Hi anish,

     

    Like Dave have said, we can add items to the ComboBox's items property at run time. We can delete this item when drop-down portion of a ComboBox is shown. Try something like this:

    Code Snippet

    Public Class Form2

        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

            Me.ComboBox1.Items.Insert(0, "Please select ...")

            Me.ComboBox1.SelectedItem = "Please select ..."

        End Sub

     

        Private Sub ComboBox1_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown

            Me.ComboBox1.Items.Remove("Please select ...")

        End Sub

    End Class

     

    Hope this helps.

    Regards

    Friday, June 22, 2007 12:15 PM
  • Hi Anish Just set

    "please select" add in first in Combo items

    formload

    Combox1.SelectedIndex=0
    Monday, November 30, 2009 8:13 AM
  • Monday, February 01, 2010 7:02 PM
  • Things have changed over the years, with .Net 4.0 you can do this

     

    ClientComboBox.Text = Nothing
    ClientComboBox.Text = "Please Select a Client"

     

    this will enable you to set the Text property, and it has nothing to do with the items in the combo

    so the selected item will still be nothing and you can do your validation the way you want :D

     

    Wednesday, September 14, 2011 8:54 AM