我需要帮助改变列表框中特定项目的颜色。更改列表框颜色如果
我的代码:
namespace WindowsFormsApplication6
{
public partial class Form2 : Form
{
List<string> lst;
public Form2()
{
InitializeComponent();
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd/MM/yyyy HH:mm";
lst = new List<string>();
}
private void BindList()
{
lst = (lst.OrderByDescending(s => s.Substring(s.LastIndexOf(" "), s.Length - s.LastIndexOf(" ")))).ToList();
listBox1.DataSource = lst;
}
private void button1_Click(object sender, EventArgs e)
{
string s = textBox1.Text + ", " + Convert.ToDateTime(this.dateTimePicker1.Value).ToString("dd/mm/yyyy HH:mm");
lst.Add(s);
BindList();
}
private void button2_Click(object sender, EventArgs e)
{
lst.Remove(listBox1.SelectedItem.ToString());
BindList();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dateTimePicker1.CustomFormat = "dd/MM/yyyy HH:mm";
}
}
}
我从DateTimePicker1添加从TextBox1的文本和时间和日期listBox1中。
如果当前时间少于1小时,我需要列表框中的项目变成红色。
我试过到目前为止:
DateTime current = System.DateTime.Now.AddHours(+1);
DateTime deadline = Convert.ToDateTime(dateTimePicker1.Value);
do
{
// missing this part
}
while (current <= deadline);
如果你能完成这个还是有不同的解决方案,这将是巨大的。
谢谢!
请参阅[ListBox.DrawItem事件](http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.drawitem(v = vs.110).aspx) – LarsTech