Click here to Skip to main content
15,887,776 members
Articles / Programming Languages / C#
Article

How to write a HatchStyle Dropdown

Rate me:
Please Sign up or sign in to vote.
3.61/5 (10 votes)
4 Sep 20051 min read 87K   921   33   8
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.

C#
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:

C#
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.

C#
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


Written By
Architect
India India
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.

Comments and Discussions

 
GeneralA cheat sheet and downloadable control Pin
Drew Noakes15-Sep-05 3:51
Drew Noakes15-Sep-05 3:51 
GeneralWrong Code !!! >> in fwsouthern's answer Pin
Mc Gwyn29-Aug-05 22:51
Mc Gwyn29-Aug-05 22:51 
GeneralNot my article ..... Pin
fwsouthern1-Sep-05 16:55
fwsouthern1-Sep-05 16:55 
GeneralRe: Wrong Code !!! >> in fwsouthern's answer Pin
Saravanan Muthiah2-Sep-05 2:06
Saravanan Muthiah2-Sep-05 2:06 
GeneralHmmm, did you forget about "Form1_Resize"? Pin
fwsouthern2-Sep-05 16:13
fwsouthern2-Sep-05 16:13 
GeneralRe: Hmmm, did you forget about "Form1_Resize"? Pin
Saravanan Muthiah4-Sep-05 21:18
Saravanan Muthiah4-Sep-05 21:18 
Generala better way to use Pin
Mc Gwyn29-Aug-05 22:20
Mc Gwyn29-Aug-05 22:20 
QuestionHow about showing some useful purpose? Pin
fwsouthern29-Aug-05 9:37
fwsouthern29-Aug-05 9:37 

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

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