ListBox の項目ごとに異なる TextFormat/Style を表示する方法はありますか
例:
Data1: value
Data2: value world
Data3: value hello
私はこれを試しましたが、次の文字列を太字にすることはできません
private void lbDetails_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Rectangle rec = e.Bounds;
string[] temp = lbDetails.Items[e.Index].ToString().Split(':');
e.Graphics.DrawString(temp[0],
new Font("Arial",8, FontStyle.Regular),
Brushes.Black,
rec,
StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
解決
private void lbDetails_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
e.DrawBackground();
string[] temp = lbDetails.Items[e.Index].ToString().Split(':');
SizeF prevStr = e.Graphics.MeasureString(temp[0] + ": ", new Font("Arial", 8, FontStyle.Regular));
SizeF nextStr = e.Graphics.MeasureString(temp[1], new Font("Arial", 8, FontStyle.Bold));
RectangleF firstRec = new RectangleF(e.Bounds.X, e.Bounds.Y, prevStr.Width, prevStr.Height);
RectangleF secondRec = new RectangleF(prevStr.Width, e.Bounds.Y, nextStr.Width, nextStr.Height);
e.Graphics.DrawString(temp[0] + ": ",
new Font("Arial", 8, FontStyle.Regular),
Brushes.Black,
firstRec,
StringFormat.GenericDefault);
e.Graphics.DrawString(temp[1],
new Font("Arial", 8, FontStyle.Bold),
Brushes.Black,
secondRec,
StringFormat.GenericDefault);
//e.Graphics.DrawRectangle(Pens.Red, firstRec.X, firstRec.Y, firstRec.Width, firstRec.Height);
e.DrawFocusRectangle();
}
catch (Exception ex)
{
}
}