putting horizontal line in a combo box or list control in C#
Answering link: (Can I put a horizontal line in a combo box or list control?)
I've create a code solution but it needs improvement. The symbol "-" in
front of the item renders a line after the item.
My inputs in the Items for the combo item collections are as follow:
-All Names
Henry (Father)
-Nancy (Mother)
Sapphire
Vincent
My combo display like so:
All Names
------------------
Henry (Father)
Nancy (Mother)
------------------
Sapphire
Vincent
while my codes are:
public Form1()
{
InitializeComponent();
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DrawItem += new DrawItemEventHandler(cmb_Type_DrawItem);
}
void cmb_Type_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
string a = comboBox1.Items[e.Index].ToString();
if (comboBox1.Items[e.Index].ToString().Substring(0, 1) == "-")
{
e.Graphics.DrawLine(Pens.Black, new Point(e.Bounds.Left,
e.Bounds.Bottom - 1),
new Point(e.Bounds.Right, e.Bounds.Bottom - 1));
a = a.Substring(1, a.Length - 1);
}
TextRenderer.DrawText(e.Graphics, a,
comboBox1.Font, e.Bounds, comboBox1.ForeColor, TextFormatFlags.Left);
e.DrawFocusRectangle();
}
The improvement I need is in the "cmb_Type_DrawItem" i wanted the
"comboBox1" to be parametrized so when i call it can be applied to any
comboBox that called it (not just comboBox1).
No comments:
Post a Comment