[Bug]: Colors ComboBox problem
See original GitHub issueI want to create a Custom ComboBox
containing a list of colors.
The DrawItemEventHandler
event is only raised when the DrawMode
property is set to DrawMode.OwnerDrawFixed
or DrawMode.OwnerDrawVariable
.
I set this.DrawMode = DrawMode.OwnerDrawFixed;
but CustomComboBox_DrawItem
never executed, is this a bug of KryptonComboBox
?
using Krypton.Toolkit;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Reflection;
using System.Windows.Forms;
namespace ColorComboBox
{
public class CustomComboBox : KryptonComboBox
{
#region Fields
private IPalette? _palette;
private List<Color> _colorList = new List<Color>();
#endregion
#region Constructor
public CustomComboBox()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
_colorList = GetColorList();
this.DataSource = _colorList;
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DrawItem += CustomComboBox_DrawItem;
}
#endregion
#region AvoidFlicker
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return createParams;
}
}
#endregion
#region Color
public List<Color> GetColorList()
{
List<Color> colorList = new List<Color>();
foreach (PropertyInfo property in typeof(Color).GetProperties())
{
if (property.PropertyType == typeof(Color))
{
object? objColor = property.GetValue(null);
if (objColor != null)
{
colorList.Add((Color)objColor);
}
}
}
return colorList;
}
#endregion
#region Render
private void CustomComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index >= 0)
{
Color color = (Color)_colorList[e.Index];
Rectangle r1 = new Rectangle(e.Bounds.Left + 1, e.Bounds.Top + 1, 2 * (e.Bounds.Height - 2), e.Bounds.Height - 2);
Rectangle r2 = Rectangle.FromLTRB(r1.Right + 2, e.Bounds.Top, e.Bounds.Right, e.Bounds.Bottom);
string txt = this.GetItemText(this.Items[e.Index]);
using (var b = new SolidBrush(color))
{
e.Graphics.FillRectangle(b, r1);
e.Graphics.DrawRectangle(Pens.Black, r1);
TextRenderer.DrawText(e.Graphics, txt, this.Font, r2, this.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}
}
}
#endregion
}
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Possible bug with combo box back color?
I have a form with the back color set to a dark blue. When you open the form the text boxes and combo...
Read more >ComboBox text color issue #4047
Describe the bug Wrong default text color displaying in Dark mode in editable combobox. It's displaying TextFillColorSecondaryBrush ...
Read more >Style Bug? Can not set Background Color within ...
The grey combobox colour is hard coded in the template for windows 10. The grey bit is not the background and it's not...
Read more >Color of field switches unexpectedly-when focus changes ...
Hello, ~My problem: color of field switches unexpectedly-when focus changes to another field. My environment stats are: Win7Pro; 64-bit; ...
Read more >[macOS] Comboboxes: inconsistent colors of the popup
In the first (editable) combobox the popup ignores the set background color, and with the selected colors is mediocre readable. In the second...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I found a bug in
KryptonComboBox.cs
.A. Look
OnComboBoxDrawItem
method inKryptonComboBox.cs
Add
OnDrawItem(e);
in last line ofOnComboBoxDrawItem
Add
EnableColorComboBox
property inKryptonComboBox.cs
Add
follows code
inWndProc
method inKryptonComboBox.cs
B. Look
Constructor
ofCustomComboBox.cs
Add
EnableColorComboBox = true;
inCustomComboBox
method inCustomComboBox.cs
Add
EnableColorComboBox = true;
inCustomComboBox_DrawItem
method inCustomComboBox.cs
Result is