Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.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# listview中的多色子项-可能吗?_C#_Listview - Fatal编程技术网

C# listview中的多色子项-可能吗?

C# listview中的多色子项-可能吗?,c#,listview,C#,Listview,直截了当的问题是:是否可以在子项中插入一个字符串,每个leter都有不同的颜色 我想用颜色来表示时间延迟。例子: 子项字符串“10 14 50”,值10和50为红色,14为绿色 尝试将OwnerDraw模式设置为true,并自己提供绘图例程: listView1.OwnerDraw = true; listView1.DrawColumnHeader += listView1_DrawColumnHeader; listView1.DrawSubItem += listView1_DrawSub

直截了当的问题是:是否可以在子项中插入一个字符串,每个leter都有不同的颜色

我想用颜色来表示时间延迟。例子:
子项字符串“10 14 50”,值10和50为红色,14为绿色

尝试将OwnerDraw模式设置为true,并自己提供绘图例程:

listView1.OwnerDraw = true;
listView1.DrawColumnHeader += listView1_DrawColumnHeader;
listView1.DrawSubItem += listView1_DrawSubItem;

void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) {
  e.DrawDefault = true;
}

void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) {
  if (e.ColumnIndex == 1) {
    e.Graphics.SetClip(e.Bounds);
    using (SolidBrush br = new SolidBrush(listView1.BackColor)) {
      e.Graphics.FillRectangle(br, e.Bounds);
    }
    int textLeft = e.Bounds.Left;
    string[] subItems = e.Item.SubItems[1].Text.Split(' ');
    for (int i = 0; i < subItems.Length; ++i) {
      int textWidth = TextRenderer.MeasureText(subItems[i], listView1.Font).Width;
      TextRenderer.DrawText(e.Graphics, subItems[i], listView1.Font,
          new Rectangle(textLeft, e.Bounds.Top, textWidth, e.Bounds.Height),
          i == 0 ? Color.Red : i == subItems.Length - 1 ? Color.Green : Color.Black, 
          Color.Empty,
          TextFormatFlags.VerticalCenter | TextFormatFlags.PreserveGraphicsClipping);
      textLeft += textWidth;
    }
    e.Graphics.ResetClip();
  } else {
    e.DrawDefault = true;
  }
}
listView1.OwnerDraw=true;
listView1.DrawColumnHeader+=listView1_DrawColumnHeader;
listView1.DrawSubItem+=listView1\u DrawSubItem;
void listView1_DrawColumnHeader(对象发送方,DrawListViewColumnHeaderEventArgs e){
e、 DrawDefault=true;
}
作废listView1\u DrawSubItem(对象发送者,DrawListViewSubItemEventArgs e){
如果(e.ColumnIndex==1){
e、 图形.SetClip(e.Bounds);
使用(SolidBrush br=新的SolidBrush(listView1.BackColor)){
e、 图形填充矩形(br,e.Bounds);
}
int text Left=e.Bounds.Left;
string[]subItems=e.Item.subItems[1]。Text.Split(“”);
对于(int i=0;i
结果:


不是直接的。但是有一些用户控件是现成的,比如:谢谢@DanByström,我会记住你的建议!