【问题标题】:How to change the BackColor of a ComboBox when DropdownStyle is DropDownList?当 DropdownStyle 为 DropDownList 时,如何更改 ComboBox 的 BackColor?
【发布时间】:2016-07-20 14:18:46
【问题描述】:

DropdownStyle 属性为DropdownList 时,我正在尝试更改ComboBox 的显示颜色。当属性从DropdownList 更改为Dropdown 时,颜色会发生变化。

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

谢谢

【问题讨论】:

    标签: c# .net winforms windows-applications dropdownbox


    【解决方案1】:

    您可以将FlatStyle 属性设置为Popup。这样,DropDownDropDownList 模式下都将使用背景颜色。

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

    【讨论】:

      【解决方案2】:

      几年来我一直在使用堆栈溢出,但没有订阅或贡献。这是我寻找解决方案时的第一选择,因为它通常提供解决方案,我无需缩放即可阅读。 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】:

        就像上面提到的那样;您可以将 FlatStyle 属性设置为 Popup/Flat。这样背景颜色将在 DropDown 和 DropDownList 模式下使用。

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

        【讨论】:

        • 如果你有兴趣改变ComboBox的BorderColor,看看this post
        【解决方案4】:

        我创建了自己的用户控件。您必须将下拉菜单设置为 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 );
                        }
                    }
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2017-06-29
          • 1970-01-01
          • 2011-08-21
          • 2019-11-07
          • 2010-11-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-07
          相关资源
          最近更新 更多