调整大小时的奇怪 ComboBox 行为 [英] Odd ComboBox behavior on resize

查看:20
本文介绍了调整大小时的奇怪 ComboBox 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,即 ComboBox 控件在调整大小时会更改它的文本值.这是我编写的一些示例代码:

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  

请注意,此表单 (FMain) 上有一个停靠在顶部的组合框 (uxComboBox).当我运行代码时,我看到组合框的值为6",这正是我所期望的.当我调整表单大小时,组合框的值为6 9/16",这是我期望的.

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?

谢谢!

斯蒂芬

推荐答案

是的,这是 ComboBox 的本机 Windows 实现中的一个已知错误.这个错误还有另一个方面.在你的表单上放一个按钮并给它 TabIndex = 0,将 CB 的 TabIndex 更改为 1.运行它,按钮将获得焦点.调整大小.请注意,组合框的文本与以前一样更改,但现在也被突出显示,就好像它有焦点一样.即使没有.

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.

我认为这个错误从 Vista 开始就存在,它没有在 Win7 中得到修复.没有已知的解决方法.

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

这篇关于调整大小时的奇怪 ComboBox 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆