Click here to Skip to main content
Licence 
First Posted 29 Aug 2005
Views 58,970
Bookmarked 30 times

How to write a HatchStyle Dropdown

By | 4 Sep 2005 | Article
In this article we will see how to write a HatchStyle dropdown.

Sample Image - HatchStyleComboBox1.jpg

Introduction

In this article we will see how to write a HatchStyle dropdown. Typically, Windows allows us to draw the items ourselves in the ComboBox. We can use DrawItem and MeasureItem events to provide the ability to override the automatic drawing and we use Drawmode property to draw the items ourselves by setting this property to OwnerDrawVariable. Double-buffering prevents flicker caused by the redrawing of the control. To fully enable double-buffering, you must also set the UserPaint and AllPaintingInWmPaint bits to true.

        public HSComboBox(): base()
        {
            this.DrawMode = DrawMode.OwnerDrawVariable;
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.InitializeDropDown();
        }

By doing this, Windows will send us DrawItem and MeasureItem events for each item added to the ComboBox.

        protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
        {
            // The following method should generally be called before drawing.
            // It is actually superfluous here, since the subsequent drawing
            // will completely cover the area of interest.
            e.DrawBackground();
            //The system provides the context
            //into which the owner custom-draws the required graphics.
            //The context into which to draw is e.graphics.
            //The index of the item to be painted is e.Index.
            //The painting should be done into the area described by e.Bounds.
            
            if (e.Index != -1)
            {
                Graphics g = e.Graphics;
                Rectangle r = e.Bounds;
            
                Rectangle rd = r; 
                rd.Width = rd.Left + 25; 
                Rectangle rt = r;
                r.X = rd.Right; 
                string displayText = this.Items[e.Index].ToString();

                HatchStyle hs = (HatchStyle)
                  Enum.Parse(typeof(HatchStyle),displayText, true);;
                // TODO add user selected foreground
                // and background colors here
                    
                HatchBrush b = new HatchBrush(hs, Color.Black, e.BackColor);
                g.DrawRectangle(new Pen(Color.Black, 2), rd.X + 3, 
                           rd.Y + 2, rd.Width - 4, rd.Height - 4);
                 g.FillRectangle(b, new Rectangle(rd.X + 3, rd.Y + 2, 
                                       rd.Width - 4, rd.Height - 4));

                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                //If the current item has focus.
                if((e.State & DrawItemState.Focus)==0)
                {
                    e.Graphics.FillRectangle(new 
                      SolidBrush(SystemColors.Window), r);
                    e.Graphics.DrawString(displayText, this.Font, 
                            new SolidBrush(SystemColors.WindowText), r, sf);
                }
                else
                {
                    e.Graphics.FillRectangle(new 
                      SolidBrush(SystemColors.Highlight), r);
                    e.Graphics.DrawString(displayText, this.Font, 
                      new SolidBrush(SystemColors.HighlightText), r, sf);
                }
            }
            //Draws a focus rectangle on the specified graphics 
            //surface and within the specified bounds.
            e.DrawFocusRectangle();
        }

        protected override void 
          OnMeasureItem(System.Windows.Forms.MeasureItemEventArgs e)
        {
            //Work out what the text will be
            string displayText = this.Items[e.Index].ToString();

            //Get width & height of string
            SizeF stringSize=e.Graphics.MeasureString(displayText, this.Font);

            //Account for top margin
            stringSize.Height += 5;

            // set hight to text height
            e.ItemHeight = (int)stringSize.Height;    

            // set width to text width
            e.ItemWidth = (int)stringSize.Width;
        }

Hatch style Dropdown

The HatchStyle enumeration specifies the hatch pattern used by a brush of type HatchBrush. The hatch pattern consists of a solid background color and lines drawn over the background. The iteration below inserts all the hatch patterns into the dropdown:

        protected void InitializeDropDown()
        {
            foreach (string styleName in Enum.GetNames(typeof(HatchStyle))) 
            { 
                this.Items.Add(styleName); 
            }
        }

We start by creating a Windows application. Add HatchStyle ComboBox to the form. Add the following lines to the Paint event of the form. The Invalidate method of the form invalidates a specific region of the form and causes a Paint message to be sent to the form.

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        if (hsComboBox1.SelectedItem != null)
        {
            Graphics g = e.Graphics;

            HatchStyle hs = (HatchStyle)Enum.Parse(typeof(HatchStyle), 
                             hsComboBox1.SelectedItem.ToString(), true);
            HatchBrush b = new HatchBrush(hs, Color.Black, this.BackColor);
            g.FillRectangle(b, new Rectangle (0,0,this.Width,this.Height));
        }
    }

    private void hsComboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (hsComboBox1.SelectedItem != null)
        {
            this.Invalidate();
        }
    }

That's it!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Saravanan Muthiah

Architect

India India

Member

He has been programming for 8+ years in Microsoft Technologies. He started his programming with C. Now He works for Quadwave Consulting, Bangalore. He has written several network applications like call capturing, RS232 network based applications. He is a MCP Professional and he loves Microsoft Technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA cheat sheet and downloadable control PinmemberDrew Noakes3:51 15 Sep '05  
GeneralWrong Code !!! >> in fwsouthern's answer Pinmembertim_mcgwyn22:51 29 Aug '05  
GeneralNot my article ..... Pinmemberfwsouthern16:55 1 Sep '05  
GeneralRe: Wrong Code !!! >> in fwsouthern's answer PinmemberSaravanan M2:06 2 Sep '05  
GeneralHmmm, did you forget about "Form1_Resize"? Pinmemberfwsouthern16:13 2 Sep '05  
GeneralRe: Hmmm, did you forget about "Form1_Resize"? PinmemberSaravanan M21:18 4 Sep '05  
Generala better way to use Pinmembertim_mcgwyn22:20 29 Aug '05  
QuestionHow about showing some useful purpose? Pinmemberfwsouthern9:37 29 Aug '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 5 Sep 2005
Article Copyright 2005 by Saravanan Muthiah
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid