Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How to write a HatchStyle Dropdown

3.61/5 (10 votes)
4 Sep 20051 min read 1   925  
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