• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

color combo

hi,

i have a custom draw combo box which displayes colors and text.
the code lookes like this:
public ColorComboBox()
            {
                  this.DrawMode = DrawMode.OwnerDrawFixed;
                  this.DrawItem += new DrawItemEventHandler( OnDrawItem );
                  this.DropDown += new System.EventHandler( OnDropDown );                                    
                  this.SelectedIndexChanged += new System.EventHandler( OnSelectedIndexChanged );            
            }
private void OnDropDown(object sender, System.EventArgs e)
            {
                  this.Text = "";
                  this.ForeColor = this.BackColor = (Color)SelectedItem;
            }

            private void OnDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
            {
                  Graphics grfx = e.Graphics;
                  Color brushColor = (Color)this.Items[ e.Index ];
                  SolidBrush brush = new SolidBrush( brushColor );
                  
                  grfx.FillRectangle(brush, e.Bounds);

                  string strFilterIndex = "filter" + e.Index.ToString();
                  grfx.DrawString(strFilterIndex, e.Font, new SolidBrush(Color.Black), e.Bounds );
            }

            private void OnSelectedIndexChanged(object sender, System.EventArgs e)
            {
                  this.Text = "";
                  this.ForeColor = this.BackColor = (Color)SelectedItem;
            }

      public Color SelectedColor
            {
                  get
                  {
                        return this.BackColor;
                  }
                  set
                  {
                        this.BackColor = value;
                  }
            }

i have 2 problems:
1. the combo style is DropDown, and for some reason when i pick a color from the combobox, the name of the color is being displayed (only when i select it).
for example: Color[Red]

2. i use DrawString to display the word "filter" on each one of the colors, i wanna to invert the text color (currently they are all black).
i familiar with the function user32.dll but i couldn't get it to work.

cheers
0
Meir Rivkin
Asked:
Meir Rivkin
  • 7
  • 5
  • 2
1 Solution
 
smeggheadCommented:
rather than using events to draw the items.. try overriding the functions..

I've done this in the past and used this method.

            protected override void OnPaint(PaintEventArgs e)
            {
                  e.Graphics.FillRectangle(Brushes.Brown,this.ClientRectangle);
            }
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                  e.Graphics.FillRectangle(Brushes.Red,this.ClientRectangle);
            }
            protected override void OnDrawItem(DrawItemEventArgs e)
            {
                  // Your code
            }
0
 
smeggheadCommented:
and obviously inherit your class from the ComboBox
0
 
Meir RivkinFull stack Software EngineerAuthor Commented:
what about invertRect?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
smeggheadCommented:
     ControlPaint.FillReversibleRectangle(UseRect,Color.Yellow);
      ControlPaint.DrawReversibleFrame(UseRect,Color.Black,FrameStyle.Dashed);

This inverts the rectangle in 'UseRect' and also ensures that the colour specifier in the 2nd parameter will be visible against the new background.

It also draws a dashed border around the rectangle, to make it appear selected.

Smg.
0
 
Meir RivkinFull stack Software EngineerAuthor Commented:
smegghead : overriding the functions doesn't change a bit.

regarding inverRect, i wanna invert just the text color so if the item painted black the text will be white etc.

cheers
0
 
smeggheadCommented:
why don't you just do a simple calculation on the RGB elements of the background colour to determine the brightness of the colour..

like

if ((R+G+B)>(128*3)) Fore=Color.Black;else Fore=Color.White;
0
 
Meir RivkinFull stack Software EngineerAuthor Commented:
cause i want to display smarter color invertion, for example:

if the background is blue[0,0,255] the text color will be yellow[255,255,0]
if the background is cyan[0,255,255] the text color will be red[255,0,0]

always compliment to 255...

cheers
0
 
smeggheadCommented:
what would you use if the color was

128,128,128 ??
0
 
Meir RivkinFull stack Software EngineerAuthor Commented:
right, thats good enough
0
 
Meir RivkinFull stack Software EngineerAuthor Commented:
what about the 1st problem, when the combo displayes the color name on the rectangle's item?
its weird cause i'm not using any other DrawString or something like that in my code.
0
 
smeggheadCommented:
if you email me your code I'll have a look at it.. my email is in my profile.
0
 
Bob LearnedCommented:
From Raymond Chen blog (Micro$oft guru):

Why highlighting by inverting colors is a bad idea
http://blogs.msdn.com/oldnewthing/archive/2003/10/31/55518.aspx

Bob
0
 
smeggheadCommented:
that was my point about 128,128,128 - 5 comments above.

I advised that he uses either black or white depending on the 'brightness' of the other color. (comment at 3:40pm)

Smg.
0
 
Bob LearnedCommented:
Sorry, I missed that comment one in all the other ones, Smegghead.  

Inverting colors:

Color InvertColor(Color colorSelected)
    {

      int r, g, b;

      r = 255 - colorSelected.R;
      g = 255 - colorSelected.G;
      b = 255 - colorSelected.B;

      Color win32Color = Color.FromArgb(0, r, g, b);

      int colorRGB = win32Color.ToArgb();

      return ColorTranslator.FromWin32(colorRGB);

    }

Bob
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now