Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# winform控件中文本中的一个粗体字_C#_.net_Winforms_Formatting_Listbox - Fatal编程技术网

C# winform控件中文本中的一个粗体字

C# winform控件中文本中的一个粗体字,c#,.net,winforms,formatting,listbox,C#,.net,Winforms,Formatting,Listbox,我想要某种方式将给定的单词加粗(列表框中每个项目的前两个字符),而不是其他方式,类似这样: 01汽车 02豪斯 03市场 对于这三个,作为listbox控件中的项,始终是前两个字符,其余字符不应为粗体 有没有切实可行的办法 数据: Visual Studio 2008 .NET 3.5 请求: private void lstMaster_DrawItem(object sender, DrawItemEventArgs e) { //TEST e.DrawB

我想要某种方式将给定的单词加粗(列表框中每个项目的前两个字符),而不是其他方式,类似这样:

01汽车
02豪斯
03市场

对于这三个,作为listbox控件中的项,始终是前两个字符,其余字符不应为粗体

有没有切实可行的办法

数据:

  • 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();

    }

它只是保持不变,没有矩形,没有“AAA”,或者没有到达断点的正方形…

我认为从VS 2008开始,您可以使用HTML标记,例如,对于01 car,对于某些文本,可以使用
01 car
。我必须检查这是否适用于列表框。

如果可能,您将需要使用列表框的DrawItem事件,从那里您可以按照自己的意愿绘制项目:

关于MSDN

以下是使用不同颜色绘制每个项目的示例:

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();
 }
根据文档,您还需要更改listbox的属性,以便触发事件:

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


这是否有效?(谈论资源消耗和处理使用)是否有功能损失?比如选择、复制、能够收集回字符串或其他什么?我认为这是有效的当然有可能搞砸这件事,例如,如果你不知何故触发了代码的重画,然后它将开始闪烁,或者您在这个函数中执行一些计算密集型操作,据我所知,此事件仅用于重新绘制单个项目,因此它将仅对可见项目运行。这同样是非常好和有效的,您不必从代码中处理它。我已经完成了,可以肯定的是,在事件选项卡中单击“drawicon”文本框创建了事件,然后编写了代码,但它似乎使用了一个旧的,未到达其中的断点。。我还需要做什么才能将此函数分配给列表框?请将您的代码发送给我?或者把它贴在这里?我需要查看并调试它。您需要将Listbox的DrawMode属性设置为OwnerDrawn。请核对我的答案