我要求,它的窗口形式应用程序 . 我是C#的新手,需要你的帮助来实现SourceListbox和DestListbox以及四个按钮 . 如果在复制选定按钮的帮助下将SourceListbox项颜色添加到另一个DestListBox中,如何将SourceListbox项颜色更改为绿色 . 再次如果我从DestListbox中删除项目,该项目应该在SourceListbox中转到黑色 . 请有人帮帮我 .
我无法插入图片以便于理解
请参阅下面的链接http://r4r.co.in/c1/01/tutorial/csharp/ListBox.shtml
完全像上面的例子 . 将项目添加到DestListBox后,源和目标列表框中匹配的项目应该在两个列表框中都是绿色文本 .
下面的代码给第一个项目添加绿色后,但选择的下一个项目只是绿色,这是我不想要的 .
private void button1_Click(object sender,EventArgs e){
foreach (string str in listBox1.SelectedItems)
{
listBox2.Items.Add(str);
SourceListbox.DrawMode = DrawMode.OwnerDrawVariable;// OwnerDrawFixed;
SourceListbox.DrawItem += s_lstbxChannel_DrawItem;
}
}
void s_lstbxChannel_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
bool isItemSelected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
int itemIndex = e.Index;
if (itemIndex >= 0 && itemIndex < listBox1.Items.Count)
{
Graphics g = e.Graphics; // Background Color
SolidBrush backgroundColorBrush = new SolidBrush((isItemSelected) ? Color.White : Color.White);
g.FillRectangle(backgroundColorBrush, e.Bounds); // Set text color
string itemText = listBox1.Items[itemIndex].ToString();
SolidBrush itemTextColorBrush = (isItemSelected) ? new SolidBrush(Color.Green) : new SolidBrush(Color.Black);
g.DrawString(itemText, e.Font, itemTextColorBrush, listBox1.GetItemRectangle(itemIndex).Location);// Clean up
backgroundColorBrush.Dispose();
itemTextColorBrush.Dispose();
}
e.DrawFocusRectangle();
}
1 回答
看看Background color of a ListBox item (winforms)
在那里,您可以看到如何更改ListBoxItems的颜色 . 有了这个,你可以做一些事情:
从SourceListbox复制到DestListbox的每个项目都变为绿色