locked
Creating a color chooser RRS feed

  • Question

  • How to create control like the one in MS Office,from which the user choose a color?Or how to fill a combobox with different colors?
    Wednesday, February 28, 2007 12:32 PM

Answers

  • you can use color dialog like follow

            private void mybutton1_Click(object sender, EventArgs e)

            {

                ColorDialog cd = new ColorDialog();

                cd.ShowDialog();

            }

     

    Thursday, March 1, 2007 6:32 AM
  • for combox you can use

    set combox drawmode to OwnerDrawFixed than add code at draw item event

            private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)

            {

                Rectangle r= e.Bounds;

                Font f=e.Font;

                string s = this.comboBox1.Items[e.Index].ToString();

                Graphics g = e.Graphics;

                g.FillRectangle(new SolidBrush(Color.FromName(s)), r);

            }

     

    Thursday, March 1, 2007 6:40 AM

All replies

  • there is a project at codeproject.com which provides a class you can use if you want a colour combo box.

    search for cComboBox

    it will give you all the named web colours. The code isnt perfect and does miss a couple of colours, due to some spelling mistakes, but if you search through the 'getcolorfromname' and 'getnamefromcolor' (or simaly named) methods you can correct them
    Wednesday, February 28, 2007 4:35 PM
  • you can use color dialog like follow

            private void mybutton1_Click(object sender, EventArgs e)

            {

                ColorDialog cd = new ColorDialog();

                cd.ShowDialog();

            }

     

    Thursday, March 1, 2007 6:32 AM
  • for combox you can use

    set combox drawmode to OwnerDrawFixed than add code at draw item event

            private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)

            {

                Rectangle r= e.Bounds;

                Font f=e.Font;

                string s = this.comboBox1.Items[e.Index].ToString();

                Graphics g = e.Graphics;

                g.FillRectangle(new SolidBrush(Color.FromName(s)), r);

            }

     

    Thursday, March 1, 2007 6:40 AM