We help IT Professionals succeed at work.

Using a combobox in vb.net

sidwelle
sidwelle asked
on
Trying to use a ComboBox in vb.net, but I get the odd behavior the as soon as the dropdown is lowered, the box makes the first selection of the list the selected item ?

Its like it tries to match the current selection ?
Me.ComboBox1.Text = "B"
Make the above change and then pull down the list ?

thanks.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim i As Integer

        For i = 0 To 5
            Me.ComboBox1.Items.Add(Chr(65 + i) & "- " & Format(i, "00"))
        Next
        Me.ComboBox1.Text = "A"

    End Sub

Open in new window

Comment
Watch Question

Top Expert 2015

Commented:
It's the standard search function of a combobox. Type in a letter and it will select the first item that starts with that letter. Try typing in another letter. Type in an "X" and nothing happens because nothing in the list starts with "X". When you say Me.ComboBox1.Text = "A" your doing the same as typing it in. To add "A" as a choice you would say Me.ComboBox1.Items.Add("A").

Author

Commented:
Can I disable this function ?, but it seems like it does not try to auto-select until the listbox drops down.
If I type in the box after list is visible, I don't get a serach function ?

Commented:
Try intializing the index on load and it shouldn't do that.
Try this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim i As Integer

        For i = 0 To 5
            Me.ComboBox1.Items.Add(Chr(65 + i) & "- " & Format(i, "00"))
        Next
        Me.ComboBox1.selectedindex = 0
    End Sub

Commented:
Or if you want no item selected at all on load then do this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim i As Integer

        For i = 0 To 5
            Me.ComboBox1.Items.Add(Chr(65 + i) & "- " & Format(i, "00"))
        Next
        Me.ComboBox1.selectedindex = -1
    End Sub

Author

Commented:
That just forced a selection from the list.

I want the user to have to opions:
1. Make a selection from the list
2. Enter something as free-text and not have it linked to the list.

Commented:
 Me.ComboBox1.selectedindex = -1
does that.

Author

Commented:
Until you the pulldown/listbox becomes visible, then you get the same behavior as before.

Commented:
If I use the pulldown it doesn't select anything unless I select it. Are you using a regular combobox?
With the default dropdownstyle of dropdown??
Top Expert 2015

Commented:
Add this:

        Me.ComboBox1.AutoCompleteSource = AutoCompleteSource.None

Commented:
Even if i use dropdownlist it behaves the same way.
Do you have code in the combobox events that is modifying its standard behavior?
I am using your code and it works fine. It doesn't select an item. When I click on the arrow by it it dropsdown the list of items without selecting any of them in the box. I can select one item or i can type something in. I can select an item and backspace to delete it and type something in.

Commented:
The starndard  combobox has it autocompletemode and autocompletesource set to none by default.

Commented:
OK. I think I may understand what you are saying.
For what you are trying to do just set your combobox to dropdown not dropdownlist.
With dropdown you can select an item but you can type over it if you want to enter something that is not on the list. It behaves like a list + the benefit of allowing you to enter items not on the list.
Use this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim i As Integer

        For i = 0 To 5
            Me.ComboBox1.Items.Add(Chr(65 + i) & "- " & Format(i, "00"))
        Next
        Me.ComboBox1.selectedindex = -1
 Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
    End Sub

Author

Commented:
Yes it does.

The autocompletemode and autocompletesource are set to none in the properties and through code.
But it still makes a selection ?

Commented:
What other code do you have for your combobox1 anywhere in your program?

Author

Commented:
Nothing,  just for testing, I opened up a new project and it only has a form with a combobox.

There should be a simple way to turn this feature off ?

Commented:
There is no feature if you are using the standard combobox with the dropdownstyle ofr dropdown.
Are you? Is this a standard combbobox?
What version of visual basic are you using?

Author

Commented:
All standard, VS '08

Author

Commented:
13598:

Can you not reproduce the same results that I am getting ?
How does yours behave ?

Commented:
Mine behaves like you desire:
It opens up with no selection.
when I click on the down arrow it displays the selection list of items.
If I select an item it displays it but I can type over or hit backspace to wipe it clean.
Are you sure yours is not a custom control?

Author

Commented:
Its right out of the MS toolbox on the left.

If you type the first char of any selection in the list and drop the list it does not autoselect that item ?

Commented:
Yeah my apologies. I was typing and wiping out and then using the list.
Doing too many things at once.  ={
It appears that is the behavior.
I will try and find out if there is a way around it without having to create your own combobox.
Just didn't want to let you hanging thinking mine was working.
Commented:
I figured out an easy workaround for you.
In your combobox1 dropdown event enter this code:
Private Sub ComboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown
        Me.ComboBox1.Text = Me.ComboBox1.Text & " "
        Me.ComboBox1.SelectionStart = Me.ComboBox1.Text.Length - 1
        Me.ComboBox1.SelectionLength = Me.ComboBox1.Text.Length - 1
    End Sub

That will do it.

Author

Commented:
Here is my final solution, Its kind of laim that MS did not write in some logic to handle this issue.

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

        If Not Me.ComboBox1.Text.ToString.EndsWith(" ") Then
            Me.ComboBox1.Text = Me.ComboBox1.Text & " "
        End If
        Me.ComboBox1.SelectionStart = Me.ComboBox1.Text.Length - 1
        Me.ComboBox1.SelectionLength = Me.ComboBox1.Text.Length - 1

    End Sub

    Private Sub ComboBox1_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDownClosed
        ComboBox1.Text = ComboBox1.Text.ToString.TrimEnd
    End Sub

Commented:
I agree with you. It would have been easy to have a property to enable/disable autocomplete.
What I posted was an example to help you with your current data but if you want it to work with 'any' data in case your item naming convention was to change in the future to allow one blank space I would add a bunch of blank spaces instead of just 1, .i.e.. add 10 blank spaces (it doesn't really matter because the cursor is positioned back to where the user left it  when you do your selectionlength). This would guarantee that if you have an item in the future like 'A 01' the autocomplete wouldn't get triggered, i.e:  

 Me.ComboBox1.Text = Me.ComboBox1.Text & "          "
        Me.ComboBox1.SelectionStart = Me.ComboBox1.Text.Length - 10
        Me.ComboBox1.SelectionLength = Me.ComboBox1.Text.Length - 10
If you know that would never change you can leave it as is.

Author

Commented:
I just tested this in my app, and it fails.
See if you can reproduce the results: Past about 10 - 15 buttons/controls on the form after the combobox so that the combobox is near the bottom of the list in the "Document Outline View"
I am guessing that makes it near the end of the hook chain ?

Then run it again. It seems that the now the "AutoComplete" fires before the "DropDown" event.
I can put it near the top in "OutLine view", but that may not always be practical.  What if I have a very busy form or I have a lot of groupboxes/deviders ?

Commented:
Have you tested it without your (just comment it out)
 Private Sub ComboBox1_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDownClosed
        ComboBox1.Text = ComboBox1.Text.ToString.TrimEnd
    End Sub

I am not sure what you are trying to do there.

Commented:
Also your code is not quite right: You need to use the selectionstart/length only if you add a space to it.
  Private Sub ComboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown

        If Not Me.ComboBox1.Text.ToString.EndsWith(" ") Then
            Me.ComboBox1.Text = Me.ComboBox1.Text & " "
                Me.ComboBox1.SelectionStart = Me.ComboBox1.Text.Length - 1
        Me.ComboBox1.SelectionLength = Me.ComboBox1.Text.Length - 1
End If

    End Sub

Commented:
The name of your combobox in your program is ComboBox1, right?
There is no reason why having more controls would change the event handler.

Author

Commented:
That line: ComboBox1.Text = ComboBox1.Text.ToString.TrimEnd
Just puts the text back the way that I found it.
After playing with it in the OutLine view code is behaving correctly now.
Why would you only want to select the space if you add it.
If there is a space you want it selected, right ?   Not sure what selecting that space does anyway ?
 
 
 

Commented:
When you use
ComboBox1.Text = ComboBox1.Text.ToString.TrimEnd in your dropdownclosed event you are taking any spaces out. But you don't know if you put them there or if the user put them there.
Adding one (or more)  space to it and removing it right after in the dropdown event you are removing what you put there. The reason why you need to add a space (or more) is to fool the autocomplete function, i.e.:
User enters B and your list of numbers in the combobox is letter,dash,number. By adding a space to it before the autocomplete function kicks in, it makes the autocomplete function not find a match and therefore not do anything at all. It doesn't find a match because there is no value in your list containing 'B ' (B + one space).     =)

Author

Commented:
Yes, I figured that, but why select it ?

From user groups another programmer had the idea of floating a text box over the edit portion of the combo box, and just update the textbox if a selection is made from drop list.
TextBox1.Text = ComboBox1.SelectedItem

Your solution from yesterday worked, but I could not get it to work consistently.
My app has two combo boxes side by side and I wanted the auto-select feature removed from each.
One worked, and the other did not.

Either this or start to build my own control.  (Like you suggested).

Thanks


Commented:
You don't have to select it. I thought that if the user entered B for example and clicked on the dropdown arrow to see what was there and then wanted to continue typing then the cursor would be right there where he/she left off. You don't actually have to select it.  =)

Author

Commented:
I experimented with floating the textbox, it works exactly like I wanted the combobox to work by its self.

Thanks for all your help.