我有一个列表框的这个用户控件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Lightnings_Extractor
{
public partial class ListBoxControl : UserControl
{
private List<int> m_itemIndexes = new List<int>();
public ListBoxControl()
{
InitializeComponent();
this.listBox1.SelectedIndex = 0;
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
int index = listBox1.IndexFromPoint(e.X, e.Y);
listBox1.SelectedIndex = index;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
if (m_itemIndexes.Contains(index))
return;
m_itemIndexes.Add(index);
DrawItem(index);
}
else if (e.Button == MouseButtons.Left)
{
if (!m_itemIndexes.Contains(index))
return;
m_itemIndexes.Remove(index);
DrawItem(index);
}
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
bool coloring = m_itemIndexes.Contains(e.Index);
bool selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
if (coloring)
{
using (var brush = new SolidBrush(Color.Red))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
}
else
{
e.DrawBackground();
}
string item = listBox1.Items[e.Index].ToString();
e.Graphics.DrawString(item, e.Font, selected || coloring ? Brushes.White : Brushes.Black, e.Bounds, StringFormat.GenericDefault);
if (selected)
e.DrawFocusRectangle();
}
private void DrawItem(int index)
{
Rectangle rectItem = listBox1.GetItemRectangle(index);
listBox1.Invalidate(rectItem);
}
}
}
例如这一行:
using (var brush = new SolidBrush(Color.Red))
现在它设置为红色。但我希望用户能够在他的代码或其他形式或类中的任何位置将其更改为任何颜色。而且不仅在这个 UserControl 代码中。
我怎样才能添加这样的属性?