question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Colors ComboBox problem

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mbsysde99commented, Nov 4, 2021

I found a bug in KryptonComboBox.cs.

A. Look OnComboBoxDrawItem method in KryptonComboBox.cs

  1. Add OnDrawItem(e); in last line of OnComboBoxDrawItem

     private void OnComboBoxDrawItem(object sender, DrawItemEventArgs e)
     {
         // others code not displayed for easy reading
         // ...
         OnDrawItem(e); // add this to trigger CustomComboBox_DrawItem (BUG IN HERE, MISSING THIS IN LAST OF CODE )
     }
    
  2. Add EnableColorComboBox property in KryptonComboBox.cs

    public static bool EnableColorComboBox { get; set; } = false;
    
  3. Add follows code in WndProc method in KryptonComboBox.cs

     protected override void WndProc(ref Message m)
     {
         // others code not displayed for easy reading
         // ...
     	switch (m.Msg)
     	{				
     		case PI.WM_.PAINT:
     		{
     			// Draw using a solid brush
     			Rectangle rectangle = new(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
     			rectangle = CommonHelper.ApplyPadding(VisualOrientation.Top, rectangle,	states.Content.GetContentPadding(state));
    
     			try
     			{						
     				if (EnableColorComboBox)
     				{
     					if (SelectedIndex >= 0)
     					{
     						var color = (Color)this.Items[SelectedIndex];
     						var rect1 = new Rectangle((int)(rectangle.Left + 1), (int)(rectangle.Top + 1), (int)(2 * (rectangle.Height - 2)), (int)(rectangle.Height - 3));
     						var rect2 = Rectangle.FromLTRB(rect1.Right + 2, (int)rectangle.Top, (int)rectangle.Right, (int)rectangle.Bottom);
     						var text = this.GetItemText(this.Items[SelectedIndex]);
     						using (var b = new SolidBrush(color))
     						{
     							g.FillRectangle(b, rect1);
     							g.DrawRectangle(Pens.LightSteelBlue, rect1);
    
     							TextRenderer.DrawText(g, text, this.Font, rect2, this.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
     						}
     					}
     				}
     			        else {
     					using SolidBrush foreBrush = new(states.Content.GetContentShortTextColor1(state));
     					g.DrawString(Text, states.Content.GetContentShortTextFont(state), foreBrush, rectangle, stringFormat);	
     				}
     			}
     			catch (ArgumentException)
     			{
     				using SolidBrush foreBrush = new(ForeColor);
     				g.DrawString(Text, Font, foreBrush, rectangle, stringFormat);
     			}
     		}
    
     		// Remove clipping settings
     		PI.SelectClipRgn(hdc, IntPtr.Zero);
    
     		// Draw the drop down button
     		DrawDropButton(g, dropRect);
     	}
     }
    

B. Look Constructor of CustomComboBox.cs

  1. Add EnableColorComboBox = true; in CustomComboBox method in CustomComboBox.cs

      #region Constructor
      public CustomComboBox()
      {
     	// others code not displayed for easy reading
     	// ...
     	
     	_colorList = GetColorList();		
     	
     	EnableColorComboBox = true;	 // add this to enable color mode	 	
     	 
     	this.DataSource = _colorList;
     	this.DropDownStyle = ComboBoxStyle.DropDownList;
     	this.DrawMode = DrawMode.OwnerDrawFixed;
     	this.DrawItem += CustomComboBox_DrawItem;
      }
      #endregion
    
  2. Add EnableColorComboBox = true; in CustomComboBox_DrawItem method in CustomComboBox.cs

     #region Render
     private void CustomComboBox_DrawItem(object sender, DrawItemEventArgs e)
     {
     	if (EnableColorComboBox)
     	{				
     		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
    

Result is image

image

1reaction
mbsysde99commented, Nov 2, 2021

@mbsysde99 Is it possible to use this code in the extended toolkit as an ‘officially’ supported control? Y es, of course

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found