【发布时间】:2016-07-20 14:18:46
【问题描述】:
当DropdownStyle 属性为DropdownList 时,我正在尝试更改ComboBox 的显示颜色。当属性从DropdownList 更改为Dropdown 时,颜色会发生变化。
如何控制下拉框的视图颜色?
谢谢
【问题讨论】:
标签: c# .net winforms windows-applications dropdownbox
当DropdownStyle 属性为DropdownList 时,我正在尝试更改ComboBox 的显示颜色。当属性从DropdownList 更改为Dropdown 时,颜色会发生变化。
如何控制下拉框的视图颜色?
谢谢
【问题讨论】:
标签: c# .net winforms windows-applications dropdownbox
几年来我一直在使用堆栈溢出,但没有订阅或贡献。这是我寻找解决方案时的第一选择,因为它通常提供解决方案,我无需缩放即可阅读。 81岁的我已经是石化了,但“灭绝也挺好玩的”。 谢谢,奥格登·纳什。
当背景阴影应用于文本时,对比度降低使我的老眼睛难以阅读。我用谷歌搜索了这个问题,提供的解决方案让我害怕。 我什至考虑过使用图形来拼凑功能,但我需要几个实例。总得有个办法吧。
用文本框覆盖组合框的文本部分,并将文本框更改为多行以使其高度与组合框匹配。添加几个事件处理程序,Bob 就是你的叔叔。
Private Sub cmbPoints_SelectedIndexChanged(sender As Object, e As EventArgs
)HandlescmbPoints.SelectedIndexChanged
' Make the selection visible in the textbox
txtPoints.Text = cmbPoints.Text
End Sub
Private Sub txtPoints_GotFocus(sender As Object, e As EventArgs
) Handles txtPoints.GotFocus
' Prevent the user changing the text.
cmbPoints.Focus()
End Sub
【讨论】:
就像上面提到的那样;您可以将 FlatStyle 属性设置为 Popup/Flat。这样背景颜色将在 DropDown 和 DropDownList 模式下使用。
但是你不会有你期望的样子。 我做了一个技巧,我创建一个面板并将其边框属性更改为 FixedSingle。将面板的颜色更改为所需的颜色,然后更改其 size 属性以匹配 ComboBox 的大小。例如到 80、22。 在您拥有 ComboBox 的位置上,放置面板。 将您的组合框放在面板上。 如果你可以微调它的位置,当你调试的时候,你会发现你的 ComboBox 看起来像是有边框的。
【讨论】:
我创建了自己的用户控件。您必须将下拉菜单设置为 Flatstyle=Flat 并更改 Backcolor=White。然后下面的代码将绘制缺少的边框。下面是代码和它的样子的图片。您可以将其复制并粘贴到您自己的命名空间中,然后随意命名。
注意:您需要添加 System.Windows.Forms; System.ComponentModel;和 System.Drawing;到你的班级。
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
public class KDCombo : ComboBox
{
public KDCombo()
{
BorderColor = Color.DimGray;
}
[Browsable(true)]
[Category("Appearance")]
[DefaultValue(typeof(Color), "DimGray")]
public Color BorderColor { get; set; }
private const int WM_PAINT = 0xF;
private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
using (var g = Graphics.FromHwnd(Handle))
{
// Uncomment this if you don't want the "highlight border".
/*
using (var p = new Pen(this.BorderColor, 1))
{
g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
}*/
using (var p = new Pen(this.BorderColor, 2))
{
g.DrawRectangle(p, 0, 0, Width , Height );
}
}
}
}
}
【讨论】: