(C#) Vertically align text of CheckedListBox items

414 views Asked by At

I have a class inheriting from CheckedListBox in Windows Forms and it contains two items. The problem is the following: When I try to increase the ItemHeight, the height will be changed, but the text is in the top left corner of the bounding box.

Image

I want it to be vertically aligned in the center of that bounding box. I can't seem to find any property that would do so. I tried to use the DrawItem event like this

public MyCheckedList()
{
    this.DrawMode = DrawMode.OwnerDrawVariable;
    this.DrawItem += new 
    System.Windows.Forms.DrawItemEventHandler(this.OnDrawItem);
}

public override int ItemHeight
{
    get; set;
}

public override DrawMode DrawMode
{
    get; set;
}

public void OnDrawItem(object sender, DrawItemEventArgs e)
{
    CheckedListBox list = (CheckedListBox)sender;
    if (e.Index > -1)
    {
        object item = list.Items[e.Index];
        e.DrawBackground();
        e.DrawFocusRectangle();
        TextRenderer.DrawText(e.Graphics, item.ToString(), list.Font, e.Bounds, list.ForeColor);
    }
}

Well, the program doesn't even enter the OnDrawItem() method. It's also strange that I have to override ItemHeight and DrawMode to make them work properly. Is there maybe another way?

0

There are 0 answers