ComboBox 的编辑框部分被自动选中 [英] Editbox portion of ComboBox gets selected automatically

查看:14
本文介绍了ComboBox 的编辑框部分被自动选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题困扰了我几个小时.

在我的 WinForms (.NET 3.5) 应用程序中,我在运行时在 TableLayoutPanel 中创建了一些组合框(DropDownStyle = DropDown)并用字符串填充它.组合框被配置为自动调整大小(锚 = 左 | 右).

问题是,只要调整组合框的大小(即调整对话框的大小),组合框的编辑框部分就会完全被选中/突出显示.在我看来,这对我想要避免的客户造成了非常混乱的影响.

如果 ComboBox 的大小固定,则不会出现此问题.

另请注意,更改 DropDownStyle 不是一个选项 - 我需要手动输入文本的可能性.

我已经尝试过重写 OnPaint 方法,但效果不佳.我还尝试清除 ComboBox.Resize 事件中的选择,这在某种程度上起作用,但似乎是一个非常丑陋的解决方案 - 有很多闪烁,故意选择的文本被取消选择,我必须将事件处理程序添加到每个以及我对话框中的每个组合框.

有没有更好的办法来解决这个问题?

提前致谢.

问候,安迪

解决方案

这是一个老问题,但我发现它正在寻找答案并最终实现了我自己的解决方案.也可以把它贴在这里,对吗?

 foreach (var cb in Controls.OfType()){cb.Resize += (sender, e) =>{如果 (!cb.Focused)cb.SelectionLength = 0;};}

<块引用>

故意选择的文本被取消选择

这个 WinForms 错误不会影响选定的 ComboBox,因此通过忽略带有 Focus 的那些,我们可以解决丢失当前选择的问题.

<块引用>

我必须添加事件处理程序到我的每一个 ComboBox对话框.

由 foreach 循环处理.如果您不想破坏设计器或让设计器破坏它,请将其放入 InitializeComponent() 或您的 .ctor 中.

<块引用>

有很多闪烁

如果我快速调整大小,我只会看到闪烁,但我使用的是 Win7,所以在 XP 上可能会有所不同.

I have a small problem that has been annoying me for some hours.

In my WinForms (.NET 3.5) application I create some ComboBoxes (DropDownStyle = DropDown) in a TableLayoutPanel at runtime and fill it with strings. The ComboBoxes are configured to resize automatically (Anchor = Left | Right).

The problem is that whenever the ComboBoxes are resized (i.e. the dialog is resized), the editbox portion of the ComboBox gets selected/highlighted entirely. In my opinion this creates a very confusing effect for the customer which I want to avoid.

The problem doesn't appear if the ComboBox has a fixed size.

Also note that changing the DropDownStyle is not an option - I need the possibility to enter text manually.

I already tried messing around with overriding the OnPaint method, which didn't quite work. I also tried clearing the selection in the ComboBox.Resize event, which worked in a way, but seemed like a very ugly solution - there was a lot of flicker, intentionally selected text became deselected and I would have to add the event handler to each and every ComboBox on my dialog.

Is there a better solution to this problem?

Thank you in advance.

Regards, Andy

解决方案

This is an old question, but I found it searching for an answer and ended up implementing my own solution. Might as well post it here, right?

    foreach (var cb in Controls.OfType<ComboBox>())
    {
        cb.Resize += (sender, e) => {
            if (!cb.Focused)
                cb.SelectionLength = 0;
        };
    }

intentionally selected text became deselected

This WinForms bug doesn't affect selected ComboBoxes, so by ignoring the ones with Focus, we take care of the problem of losing current selections.

I would have to add the event handler to each and every ComboBox on my dialog.

Taken care of by the foreach loop. Put it in InitializeComponent() or your .ctor if you don't want to break the designer, or have the designer break this.

there was a lot of flicker

I'm only seeing flicker if I resize very fast, but I'm on Win7 so it might be different on XP.

这篇关于ComboBox 的编辑框部分被自动选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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