Results 1 to 5 of 5

Thread: Combobox initial value

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Buckingham, England
    Posts
    197

    Combobox initial value

    I am using a form with a combobox which is populated from an SQL database table. This works OK but is it possible to leave the box blank on the form until a value is selected.By default the box has the content of the first record.

  2. #2
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Combobox initial value

    yes it is possible , provided your combobox dropdownstyle property is set to dropdown or simple
    if it is set to dropdownlist it is not possible

    vb Code:
    1. With Me.ComboBox1
    2.             .DropDownStyle = ComboBoxStyle.DropDown
    3.             .Text = ""
    4.         End With
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Buckingham, England
    Posts
    197

    Re: Combobox initial value

    'CRAP'
    Not the answer I wanted to see but it happens.
    At least I now know how to do it but I also wanted to prevent the end user from making any changes so I had a dropdownlist. Thanks for your reply.

  4. #4
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Combobox initial value

    combobox dropdownlist property is not the ultimate to restrict the users from entering their own choice
    use this function in combobox lostfocus event, which will tell whether the user has chosen the corrent value or else entered his own text

    vb Code:
    1. Private Function LocokComboForEdit() As Boolean
    2.  
    3.         'locks the combobox for edit
    4.  
    5.         If Me.ComboBox1.Text = "" Then Exit Function
    6.         Dim Sel_Item_index As Integer = Me.ComboBox1.SelectedIndex
    7.  
    8.         If Sel_Item_index < 0 Then
    9.             MessageBox.Show("Invalid entry ")
    10.         Else
    11.             MessageBox.Show("DCorrect entry")
    12.         End If
    13.  
    14.     End Function

    use the return values of this function as per your connivance.
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  5. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    28

    Re: Combobox initial value

    Hi.

    You can also try this other method while using the dropdownstyle as dropdownlist.

    1. At the form load, query your database for the values you want to fill the combobox with, and fill it through a loop
    2. Before you fill the combobox with the values, add a blank one i.e. ( combobox1.items.add("") )
    3. After a user selects a value from the combobox, delete the first one, which will be the blank one, something like this :

    VB Code:
    1. private sub combobox1_selectedindexchanged()
    2. if combobox1.items(0) = "" then
    3. combobox1.items.removeat(0)
    4. end if
    5. end sub

    That way the combobox will have a blank value untill the user selects one, while restricting the user from inputting his own values.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width