views:

233

answers:

2

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

A: 

When the form loads, ComboTest gets executed, and you see a '6', however when you resize it does not show the new data, sounds like you need to refresh the combo box, regardless of the resize or not.

Try uxComboBox.Refresh() immediately after the line uxComboBox.Items.AddRange.

And after the line 'ComboTest', set the selected index to 0 uxComboBox.Index = 0 also.

Hope this helps, Best regards, Tom.

tommieb75
Setting the selected index to 0 just selects the first item in the list which isn't what I want. It needs to retain the value of "6" that I set.I tried uxComboBox.Refresh() as you suggested but no luck. The behavior is the same when I resize the form. I even tried it in the form's Resize event but, again, no change in the behavior.
StephenM
@StephenM: Can you show the form's designer code? It seems to me that because the combo is docked to the top and resizing, the combo box does not get refreshed. Perhaps add an event handler for the form's resize and in the event handler call uxComboBox.Refresh
tommieb75
Already tried that but again, no luck.
StephenM
A: 

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.

Hans Passant
Thanks for the information. We've just finished migrating from VB6 to .NET and this issue doesn't occur in the VB6 application. Any idea why?
StephenM
It is probably a theming bug. Most of these UI bugs are. VB6 doesn't enable visual styles. And uses lots of windowless controls.
Hans Passant
Good to know. Thank you!
StephenM
I worked around this issue by storing the text value before our custom control resizes it via SetBounds() and then restoring the value if the text changed. It still flashes "6 9/16" momentarily when resizing but the end result is that it retains the current value. It feels like a clumsy fix but whatever makes it work, right?
StephenM