WinForms组合框中的C#高亮文本 [英] C# Highlighted text in WinForms combo box

查看:72
本文介绍了WinForms组合框中的C#高亮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有设置要阻止Winforms组合框中的选定项在设置后自动突出显示的功能? (通过组合框属性SelectedIndex或SelectedItem)。



发生的事情是,我们在组合框中填充了一组项目,然后为所需的项目设置了索引在组合中显示,但是这会导致文本突出显示。禁用组合框时,这意味着很难阅读文本,因为突出显示颜色为蓝色,文本颜色为白色。看起来这种行为是设计使然,但很烦人!



我在网上找到并尝试过的唯一解决方案是对组合框控件进行子类化,但这也是侵入性,这意味着我们必须替换应用程序中的所有组合框才能解决此问题。我还尝试了在父控件加载后将组合上的SelectionLength属性设置为0,并且还尝试了在组合框上调用Select(0,0),但都没有达到预期的效果。



有什么想法吗?



谢谢

解决方案

这似乎是 ComboBox DropDownStyle DropDown



我认为最好的解决方案是处理 ComboBox 调整大小事件,将 SelectionLength 属性设置为0(零)。该解决方案的详细解答如下:




Is there anyway to stop the selected item in a winforms combo box from being automatically highlighted when it is set? (either via the combobox property SelectedIndex or SelectedItem).

What happens is that we populate a combo box with a set of items and then set the index for the item we want to show in the combo but this then causes the text to be highlighted. When the combobox is disabled this means that it is very hard to read the text because the highlight colour is blue and the text colour is white. Looks like this behaviour is by design but it is very annoying!

The only solution I've found online and tried which works is to subclass the combobox control but this is too invasive and would mean that we would have to replace all combo boxes in our application to solve this issue. I've also tried setting the SelectionLength property on the combo to 0 after the parent control has loaded and have also tried calling Select(0,0) on the combobox but neither has the desired effect.

Any ideas?

Thanks

解决方案

This appears to be a bug in the native Windows implementation of ComboBox with DropDownStyle of DropDown.

I think the best solution is to handle the ComboBox's Resize event, setting the SelectionLength property to 0 (zero). That solution is detailed in answers to this question:

Editbox portion of ComboBox gets selected automatically

However, I found that even that hackish fix to work around this bug does not always work. If the ComboBox is in a TableLayoutPanel, and if that TableLayoutPanel has more than one column with a Percent Size Type, then that fix often does not work.

A picture is worth a thousand words. See the following screen shot of a form I made to demonstrate the problem.

这篇关于WinForms组合框中的C#高亮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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