C# 如果列表框项目颜色存在于另一个列表框中,如何更改列表框项目颜色
我有一个需求,它的窗口窗体应用程序。我是C#的新手,需要您的帮助才能实现以下目标 SourceListbox和DestListbox以及四个按钮。 如何在“复制选定项”按钮的帮助下,将SourceListbox项颜色更改为绿色(如果在另一个DestListBox中添加)。 同样,若我从DestListbox中删除该项,那个么该项在SourceListbox中应该变成黑色。 谁能帮帮我吗 为了便于理解,我无法插入图片 请参阅下面的链接C# 如果列表框项目颜色存在于另一个列表框中,如何更改列表框项目颜色,c#,listbox,C#,Listbox,我有一个需求,它的窗口窗体应用程序。我是C#的新手,需要您的帮助才能实现以下目标 SourceListbox和DestListbox以及四个按钮。 如何在“复制选定项”按钮的帮助下,将SourceListbox项颜色更改为绿色(如果在另一个DestListBox中添加)。 同样,若我从DestListbox中删除该项,那个么该项在SourceListbox中应该变成黑色。 谁能帮帮我吗 为了便于理解,我无法插入图片 请参阅下面的链接 就像上面的例子一样。将项目添加到DestListBox后,在
就像上面的例子一样。将项目添加到DestListBox后,在源和Dest列表框中匹配的任何项目都应在两个列表框中以绿色文本显示 下面的代码给出了绿色后添加的第一项, 但接下来的选择项目只会变成绿色,这是我不想要的 私有无效按钮1\u单击(对象发送者,事件参数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();
}
foreach(listBox1.SelectedItems中的字符串str)
{
列表框2.Items.Add(str);
SourceListbox.DrawMode=DrawMode.OwnerDrawVariable;//OwnerDrawFixed;
SourceListbox.DrawItem+=SlstbxChannel_DrawItem;
}
}
void s_lstbxChannel_DrawItem(对象发送方,DrawItemEventArgs e)
{
e、 牵引杆接地();
bool isItemSelected=((e.State&DrawItemState.Selected)=DrawItemState.Selected);
int itemIndex=e.索引;
如果(itemIndex>=0&&itemIndex
看一看
在这里,您可以看到如何更改ListBoxItems的颜色。有了它,你可以做如下事情:
public Form1()
{
InitializeComponent();
SourceListbox.DrawMode = DrawMode.OwnerDrawFixed;
SourceListbox.DrawItem += SourceListbox_DrawItem;
}
//global brushes with ordinary/selected colors
private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.White);
private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black);
private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
private SolidBrush reportsBackgroundBrush1 = new SolidBrush(Color.White);
private SolidBrush reportsBackgroundBrush2 = new SolidBrush(Color.Green);
//custom method to draw the items, don't forget to set DrawMode of the ListBox to OwnerDrawFixed
private void SourceListbox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
int index = e.Index;
if (index >= 0 && index < SourceListbox.Items.Count)
{
string text = SourceListbox.Items[index].ToString();
Graphics g = e.Graphics;
//background:
SolidBrush backgroundBrush;
if (selected)
backgroundBrush = reportsBackgroundBrushSelected;
else if (DestListbox.Items.Contains(SourceListbox.Items[index]))
backgroundBrush = reportsBackgroundBrush2;
else
backgroundBrush = reportsBackgroundBrush1;
g.FillRectangle(backgroundBrush, e.Bounds);
//text:*/
SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush;
g.DrawString(text, e.Font, foregroundBrush, SourceListbox.GetItemRectangle(index).Location);
}
e.DrawFocusRectangle();
}
private void buttonCopySelected_Click(object sender, EventArgs e)
{
foreach (int idx in SourceListbox.SelectedIndices)
{
DestListbox.Items.Add(SourceListbox.Items[idx]);
}
}
public Form1()
{
初始化组件();
SourceListbox.DrawMode=DrawMode.OwnerDrawFixed;
SourceListbox.DrawItem+=SourceListbox\u DrawItem;
}
//具有普通/选定颜色的全局笔刷
private SolidBrush reportsForegroundBrushSelected=新的SolidBrush(颜色为白色);
private SolidBrush reportsforgroundbrush=新的SolidBrush(Color.Black);
私有SolidBrush reportsBackgroundBrushSelected=新的SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
私有SolidBrush reportsBackgroundBrush1=新的SolidBrush(颜色为白色);
私有SolidBrush reportsBackgroundBrush2=新的SolidBrush(颜色为绿色);
//要绘制项目,请不要忘记将ListBox的DrawMode设置为OwnerDrawFixed
私有void SourceListbox\u DrawItem(对象发送方,DrawItemEventArgs e)
{
e、 牵引杆接地();
bool selected=((e.State&DrawItemState.selected)=DrawItemState.selected);
int-index=e.index;
if(index>=0&&index
从SourceListbox复制到DestListbox的每个项目都将变为绿色如果向图像添加直接链接,其他用户可以将其编辑到您的问题中。你现在的问题很不清楚。。什么是SourceListbox和DestListbox?您使用的是winforms/wpf/asp.net吗?我在上面尝试过,但没有满足我的要求。只是想更好地解释我的问题,pl看看它,你不想背景颜色设置为绿色,而是文本颜色?您希望它同时出现在两个列表框中,而不是仅出现在SourceListbox中?删除SourceListbox中的行之后,复制它之前应该是什么样子?