Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vb.net - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...