Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd ComboBox behavior on resize

I have an issue where a ComboBox control will change it's Text value when it is resized. Here is some sample code that I worked up:


Option Explicit On  
Option Strict On

Public Class FMain  
    Private Sub FMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
        uxComboBox.DropDownStyle = ComboBoxStyle.DropDown  
        uxComboBox.AutoCompleteSource = AutoCompleteSource.ListItems  
        uxComboBox.AutoCompleteMode = AutoCompleteMode.Suggest  

        ComboTest()  
    End Sub  

    Private Sub ComboTest()  
        Dim value As String = "6"  

        uxComboBox.Text = String.Empty  
        uxComboBox.Items.Clear()  

        uxComboBox.Items.AddRange(New String() {"4 9/16", "6 9/16", "7 9/16", "8 9/16"})  

        Dim index As Integer = uxComboBox.FindStringExact(value)  
        If uxComboBox.SelectedIndex  index Then  
            uxComboBox.SelectedIndex = index  
        End If  

        If uxComboBox.SelectedIndex = -1 AndAlso _
           Not String.Equals(uxComboBox.Text, value, StringComparison.OrdinalIgnoreCase) Then  
            uxComboBox.Text = value  
        End If  

        ' unselect the text in the combobox  
        '  
        uxComboBox.Select(0, 0)  
    End Sub  
End Class  

Note that this form (FMain) has a single combobox on it (uxComboBox) that is docked to the top. When I run the code I see that the combobox has a value of "6" which is what I would expect. When I then resize the form, the combobox gets a value of "6 9/16" which is what I would NOT expect.

Does anyone know why this happens? Any suggested workarounds?

Thanks!

Stephen

like image 834
StephenM Avatar asked Jan 28 '10 00:01

StephenM


People also ask

How can ComboBox give the characteristics of ComboBox?

Let's create a combo box by dragging a ComboBox control from the Toolbox and dropping it on the form. You can populate the list box items either from the properties window or at runtime. To add items to a ComboBox, select the ComboBox control and go to the properties window for the properties of this control.

What is ComboBox with example?

A combo box is a text box with a list box attached. This type of control enables users to select a predefined value in a list or type their own value in the text box portion of the control. The list is hidden until the user clicks the arrow next to the box.

What is ComboBox control?

The Windows Forms ComboBox control is used to display data in a drop-down combo box. By default, the ComboBox control appears in two parts: the top part is a text box that allows the user to type a list item. The second part is a list box that displays a list of items from which the user can select one.


1 Answers

Yes, this is a known bug in the native Windows implementation of ComboBox. There's another aspect to this bug. Put a button on your form and give it TabIndex = 0, change the CB's TabIndex to 1. Run it, the button will have the focus. Resize. Note that the ComboBox's text changes as before but now also gets highlighted, as though it has the focus. Even though it hasn't.

I think this bug has been around since Vista, it didn't get fixed in Win7. There's no known workaround for it.

like image 81
Hans Passant Avatar answered Oct 15 '22 05:10

Hans Passant