winform控件中的文本中有一个单词粗体

Mar*_*elo 6 .net c# formatting listbox winforms

我想要一些方法来使一个给定的单词变为粗体(列表框中每个项目的前两个字符),而不是其他任何东西,如下所示:

01
02
03市场

至于这三个,作为列表框控件中的项目,总是前两个字符,其余的不应该是粗体.

这样做有什么实际可行的方法吗?

数据:

  • Visual Studio 2008
  • .NET 3.5

请求:

    private void lstMaster_DrawItem(object sender, DrawItemEventArgs e)
    {
//TEST
        e.DrawBackground();
        Brush myBrush = Brushes.Black;
        Pen pen = new Pen(myBrush);
        e.Graphics.DrawRectangle(pen, 0, 0, 10, 10); //BREAKPOINT HERE

        e.Graphics.DrawString("aaa" + lstMaster.Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);

        e.DrawFocusRectangle();

    }
Run Code Online (Sandbox Code Playgroud)

它只是保持相同,没有矩形,没有"AAA",或方形没有断点到达...

gyu*_*isc 6

有可能,您需要使用列表框的DrawItem事件,然后您可以在那里绘制您想要的项目:

MSDN上的DrawItem事件

这是一个用不同颜色绘制每个项目的示例:

private void ListBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
  // Draw the background of the ListBox control for each item.
  e.DrawBackground();
  // Define the default color of the brush as black.
  Brush myBrush = Brushes.Black;

  // Determine the color of the brush to draw each item based 
  // on the index of the item to draw.
  switch (e.Index)
  {
      case 0:
          myBrush = Brushes.Red;
          break;
      case 1:
          myBrush = Brushes.Orange;
          break;
      case 2:
          myBrush = Brushes.Purple;
          break;
  }

  // Draw the current item text based on the current Font 
  // and the custom brush settings.
  e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), 
    e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
  // If the ListBox has focus, draw a focus rectangle around the selected item.
  e.DrawFocusRectangle();
 }
Run Code Online (Sandbox Code Playgroud)

根据文档,您还需要更改列表框的DrawMode属性,以便触发事件:

此事件由所有者绘制的ListBox使用.仅当DrawMode属性设置为DrawMode.OwnerDrawFixed或DrawMode.OwnerDrawVariable时才会引发该事件.您可以使用此事件来执行在ListBox中绘制项目所需的任务.