代码之家  ›  专栏  ›  技术社区  ›  Gowtham Ramamoorthy

当DropdownStyle为DropDownList时,如何更改组合框的背景色?

  •  13
  • Gowtham Ramamoorthy  · 技术社区  · 8 年前

    我正在尝试更改 ComboBox DropdownStyle 属性为 DropdownList 。当属性更改为 Dropdown 从…起 下拉列表 颜色改变。

    如何控制下拉框的视图颜色?

    谢谢

    4 回复  |  直到 8 年前
        1
  •  22
  •   Reza Aghaei    3 年前

    您可以设置 FlatStyle 属性到 Popup 。这样,背景色将用于 DropDown DropDownList 模式

    如果您不喜欢平面样式或需要在渲染时进行更多自定义 ComboBox ,您可以使用绘制的所有者 组合框 。例如,您可以设置 DrawMode 属性到 OwnerDrawFixed 和手柄 DrawItem 事件并根据您的逻辑绘制组合框。

    您还可能对以下帖子感兴趣,以定制ComboBox:

        2
  •  3
  •   Robin Holden    4 年前

    几年来,我一直在使用堆栈溢出,没有订阅或贡献。这是我在寻找解决方案时的第一选择,因为它通常提供了一个解决方案,而且我不用缩放就能阅读它。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
    
        3
  •  2
  •   James    4 年前

    如上所述;可以将FlatStyle属性设置为Popup/Flat。这样,背景色将同时用于DropDown和DropDownList模式。

    但这样你就不会有你期待的样子了。 我有一个技巧,创建一个面板,并将其边界属性更改为FixedSingle。根据需要将面板的颜色更改为,然后更改其大小属性以匹配组合框的大小。例如80、22。 在组合框所在的位置,放置面板。 将组合框放在面板上。 如果你能微调它的位置,当你调试时,你会发现你的ComboBox看起来像有一个边框。

        4
  •  1
  •   Kim    3 年前

    我创建了自己的Usercontrol。您必须将下拉菜单设置为Flatstyle=Flat,并更改Backcolor=White。然后,下面的代码将绘制缺失的边框。下面是代码和它的图片。您可以将其复制并粘贴到您自己的命名空间中的某个位置,并根据自己的喜好命名。

    注意:您需要添加System.Windows。形式;系统组件模型;和系统。绘画你的班级。

    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 );
                    }
                }
            }
        }
    }
    

    enter image description here