如何使用c#和winforms更改列表框项目内的项目fontstyle

如何使用c#和winforms更改列表框项目内的项目fontstyle,c#,winforms,listbox,C#,Winforms,Listbox,我有一个包含项目的列表框 say Listboxitem1 listboxitem2 listboxitem3... 是否可以更改listboxitem1的项目样式 我已经看到了更改listbox所选项目背景颜色的代码,但我无法找到任何更改listboxitem中项目样式的解决方案 以下代码用于更改列表框中选定项目的背景颜色。但我无法找到任何用于更改项目样式(如字体)的属性 i

我有一个包含项目的列表框

                 say Listboxitem1
                     listboxitem2
                     listboxitem3...
是否可以更改listboxitem1的项目样式

我已经看到了更改listbox所选项目背景颜色的代码,但我无法找到任何更改listboxitem中项目样式的解决方案

以下代码用于更改列表框中选定项目的背景颜色。但我无法找到任何用于更改项目样式(如字体)的属性

if(e.Index<0)返回;
//如果选择了项目状态,则更改背景色
if((e.State&DrawItemState.Selected)=DrawItemState.Selected)
e=新的DrawItemEventArgs(如图形、,
e、 字体,
e、 界限,
e、 索引,
e、 状态^DrawItemState。已选择,
e、 前景色,
颜色(红色);//选择颜色
//为每个项目绘制ListBox控件的背景。
e、 牵引杆接地();
//绘制当前项文本
e、 Graphics.DrawString(listbox1.Items[e.Index].ToString(),e.Font,Brush.Black,e.Bounds,StringFormat.GenericDefault);
//如果列表框具有焦点,请围绕选定项绘制一个焦点矩形。
e、 DrawFocusRectangle();
有谁知道这件事吗


非常感谢。

您在自己的代码中看到e.Font了吗?只需在其中使用另一种字体;)

我试过用“fontstyle.bold”来代替e.font,但它不起作用…通过使用此选项,您可以仅更改选定项目的字体样式而不是所有项目…是否有其他选项…看-我看不到此代码的位置,但我猜它在项目的用户绘图中,对吗?如果是的话,你可以照我说的做。只需在e.Graphics.DrawString代码中使用“字体”而不是Fontstyle来代替e.Font即可。您必须提供类似“new Font(e.Font,FontStyle.Bold)”的内容,您的意思是,我需要在这一行进行更改…e.Graphics.DrawString(listbox1.Items[e.Index].ToString(),e.Font,Brush.Black,e.Bounds,StringFormat.GenericDefault);是,另一个将仅更改所选项目的字体(它仅替换当前eventargs参数)
      if (e.Index < 0) return; 
    // if the item state is selected then change the back color  
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
        e = new DrawItemEventArgs(e.Graphics, 
                                  e.Font, 
                                  e.Bounds, 
                                  e.Index, 
                                  e.State ^ DrawItemState.Selected, 
                                  e.ForeColor, 
                                  Color.Red); // Choose the color 

    // Draw the background of the ListBox control for each item. 
    e.DrawBackground(); 
    // Draw the current item text 
    e.Graphics.DrawString(listbox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault); 
    // If the ListBox has focus, draw a focus rectangle around the selected item. 
    e.DrawFocusRectangle();