Form1のlistBoxのテキストに色を付けるために使用しているこのクラスがあります。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace GatherLinks
{
class ColorText
{
public static void Texts(RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
public static void ColorListBox(List<string> data, DrawItemEventArgs e)
{
int keywords = 0;
string keyword = null;
string url = data[e.Index].Substring(0, 5);
if (data[e.Index].Contains("Local KeyWord:"))
{
keywords = data[e.Index].IndexOf("Local KeyWord:");
keyword = data[e.Index].Substring(keywords, 14);
}
else
{
keywords = data[e.Index].IndexOf("Localy KeyWord:");
keyword = data[e.Index].Substring(keywords, 15);
}
using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular))
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
e.Graphics.FillRectangle(Brushes.LightBlue, e.Bounds);
else
{
using (SolidBrush sb = new SolidBrush(SystemColors.Window))
e.Graphics.FillRectangle(sb, e.Bounds);
}
SizeF size = e.Graphics.MeasureString(url, f);
using (SolidBrush sb = new SolidBrush(Color.Red))
{
e.Graphics.DrawString(url, f, sb, new PointF(e.Bounds.X, e.Bounds.Y));
string toMeasure = data[e.Index].Substring(0, keywords - 1);
float startPos = e.Graphics.MeasureString(toMeasure, f).Width;
e.Graphics.DrawString(keyword, f, sb, new PointF(e.Bounds.X + (int)startPos, e.Bounds.Y));
}
// string token = data[e.Index].Substring(url.Length, data[e.Index].LastIndexOf(" --- ") - (url.Length));
// e.Graphics.DrawString(token, f, Brushes.Black, new PointF(e.Bounds.X + size.Width, e.Bounds.Y));
// Get the string to print in black.
int first = url.Length;
int last = data[e.Index].LastIndexOf(" --- ") + 5; //(5 = length of " --- ").
int length = last - first;
string token = data[e.Index].Substring(first, length);
// Get the place to draw it.
size = e.Graphics.MeasureString(url, f);
float positionX = size.Width;
// Draw the string.
e.Graphics.DrawString(token, f, Brushes.Black, new PointF(positionX, e.Bounds.Y));
//size = e.Graphics.MeasureString(url + token, f);
//size = e.Graphics.MeasureString("Url:" + token, f);
//e.Graphics.DrawString(data[e.Index].Substring(data[e.Index].IndexOf(token) + token.Length, data[e.Index].LastIndexOf(": ") - token.Length - data[e.Index].IndexOf(token) + 1), f, Brushes.Black, new PointF(size.Width, e.Bounds.Y));
token = data[e.Index].Substring(data[e.Index].LastIndexOf(": ") + 2);
size = e.Graphics.MeasureString(data[e.Index].Substring(0, data[e.Index].LastIndexOf(token)), f);
using (SolidBrush sb = new SolidBrush(Color.Green))
e.Graphics.DrawString(token, f, sb, new PointF(e.Bounds.X + size.Width + 4, e.Bounds.Y));
e.DrawFocusRectangle();
}
}
}
}
問題は、関数ColorListBoxで特定の文字列/アイテムを検索していることです: "Local KeyWord"
しかし、Form1では、listBoxには何でも含まれている可能性があります。つまり、この関数がlistBox内の任意の文字列で機能するように適合させたい任意の文字列が含まれている可能性があります。
たとえば、Form1では次のようにしました。
data.Add("System Fan Speed - ");
listBox1.DataSource = data;
私がやりたいのは、右側のテキスト、たとえばGpu Temperatureが赤になり、文字列「-」が黒になり、数字自体が緑になります。この関数で、listBoxアイテムで選択した各パーツの色を設定できるようにしたいと思います。
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1)
{
}
else
{
string url = data[e.Index].Substring(0, 5);
using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular))
{
ColorText.ColorListBox(data, e);
}
}
}