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

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

问题描述

我有一个小问题,已经烦了我好几个小时。

在我的WinForms(.NET 3.5)应用程序创建的一些组合框(DropDownStyle =下拉)在一个TableLayoutPanel在运行时使用字符串填充它。该组合框被配置为自动调整(锚=左|右)。

现在的问题是,每当组合框的调整(调整大小即对话),组合框的编辑框部分被选中/高亮显示完全。在我看来,这会为我想避免客户一个非常混乱的效果。

如果ComboBox有一个固定大小的问题不会出现。

另外请注意,改变DropDownStyle是不是一种选择 - 我需要能够手动输入文字

我已经试过重写OnPaint方法,这也不太工作乱搞。 我也试过清零ComboBox.Resize事件,在某种程度上其工作的选择,但似乎是一个非常丑陋的解决方案 - 有很多忽悠的,故意选定的文本成为取消,我将不得不事件处理程序添加到每个和每一个组合框在我的对话框。

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

感谢你在前进。

问候, 刘德华

解决方案

这是一个老问题,但我发现它寻找答案,并最终实现自己的解决方案。还不如在这里发帖,对吧?

 的foreach(在Controls.OfType&LT变种CB,组合框>())
    {
        cb.Resize + =(发件人,E)=> {
            如果(!cb.Focused)
                cb.SelectionLength = 0;
        };
    }
 

  

有意选定的文本成为取消

本的WinForms错误不影响选择组合框,所以忽略那些与分众,我们就失去当前选择的问题负责。

  

我会添加事件处理   对每一个组合框我   对话框。

在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.

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

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