Results 1 to 14 of 14

Thread: Combobox Problem!!!

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Unhappy Combobox Problem!!!

    I added a few items in the combobox dropdown list at the form load event.
    so i did this code:
    Code:
     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ComboBox1.Items.Add("Mr.")
            ComboBox1.Items.Add("Mrs.")
        End Sub
    My form looks like this:
    Attachment 72421
    At the runtime I dont want to allow the user to write something in the combobox as a text.The user can only select an item from the dropdown list of the combobox.
    How to do this?
    Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.

  2. #2
    Lively Member
    Join Date
    Aug 2007
    Posts
    80

    Re: Combobox Problem!!!

    u can try this

    stops them from pressing keys try this and see how it goes 4 u

    Code:
    Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
    
    e.SuppressKeyPress = True
    
    End Sub

  3. #3
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: Combobox Problem!!!

    You can change the style of the combobox so that it can't be edited:

    Code:
     ' This can NOT be edited
    ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    
    'This CAN be Edited
    ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
    I also like to go ahead and show the first combobox element instead of leaving it blank. After I populate the box, I do this:

    Code:
    ComboBox1.Text = ComboBox1.Items(0)

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Unhappy Re: Combobox Problem!!!

    Hi dkahn
    the below code did well:
    Code:
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ComboBox1.Items.Add("Mr.")
            ComboBox1.Items.Add("Mrs.")
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
            ComboBox1.Text = ComboBox1.Items(0)
        End Sub
    Now inspite of showing the 1st element of the combobox with the below code:
    Code:
    ComboBox1.Text = ComboBox1.Items(0)
    can i leave the combobox like this so as to indicate the user that there is some items in the dropdownlist of the combo:
    Attachment 72422
    Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.

  5. #5
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: Combobox Problem!!!

    If you mean highlighted, but blank, you could do this:

    Code:
    ComboBox1.BackColor = Color.Blue
    I don't see a property that highlights the empty box, but there may be one.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Combobox Problem!!!

    well i tried this earlier....if i do this then the dropdown list of the combobox also gets colored blue.....
    Is nt it possible to colour only the text box blue until an input is provided from the dropdown list?

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Combobox Problem!!!

    Toggle the backcolor between white and blue.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Combobox Problem!!!

    How to toggle the backcolor between white and blue?

  9. #9

  10. #10
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: Combobox Problem!!!

    It would make more sense from the user standpoint to show a selection in the combo box. Even if it is < Select One > showing up. In my opinion,then you can continue on based on their selection.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Combobox Problem!!!

    I think its a better idea to have a <Select One> Coach....
    Here lies my code that i tried....
    Code:
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    ComboBox1.Items.Add("Mr.")
            ComboBox1.Items.Add("Mrs.")
            ComboBox1.Items.Add("Miss.")
            ComboBox1.Items.Add("Ms.")
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        End Sub
    End Class
    How to modify the code so that when the form loads the <Select One> option appears ?

  12. #12

  13. #13
    Hyperactive Member
    Join Date
    May 2008
    Location
    >> ( ҉ )
    Posts
    413

    Re: Combobox Problem!!!

    If i'm sure you are adding items inside it.

    Easier?

  14. #14
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Combobox Problem!!!

    All you need is to set the focus to the ComboBox control on the Form_Shown event handler. Alternatively you could set the Tab Order of the form where the ComboBox is the first in the list.
    vb Code:
    1. Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    2.     Me.ComboBox1.Focus()
    3. End Sub

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