C#中:改变列表框项的颜色 [英] C# : change listbox items color

查看:142
本文介绍了C#中:改变列表框项的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作程序在Windows窗体上我有一个列表框,我验证数据我想,而用红色增加无效数据正确的数据被添加到色绿列表框,我也想从列表框中自动当一个项目被添加和向下滚动感谢



代码:

 尝试
{
是validateData;
listBox1.Items.Add(成功验证了数据:+ validateddata);
}
赶上()
{
listBox1.Items.Add(无法验证数据:+ validateddata);
}


解决方案

假设的WinForms,这是什么我会做:



开始通过一类含有添加到列表框中的项目

 公共类MyListBoxItem {
公共MyListBoxItem(彩色C,串m){
ItemColor = C;
消息=米;
}
众彩ItemColor {搞定;组; }
公共字符串消息{搞定;组; }
}

添加使用此代码的项目到你的列表框:

  listBox1.Items.Add(新MyListBoxItem(Colors.Green,经过验证的数据成功)); 
listBox1.Items.Add(新MyListBoxItem(Colors.Red,无法验证数据));

在列表框的属性,设置DrawMode到OwnerDrawFixed,并为DrawItem事件的事件处理程序。这允许然而你愿意,你绘制每个项目



在DrawItem事件:

 私人无效listBox1_DrawItem(对象发件人,DrawItemEventArgs E)
{
MyListBoxItem项目= listBox1.Items [e.Index]作为MyListBoxItem; //获取当前项目,并投它MyListBoxItem
如果(项目!= NULL)
{
e.Graphics.DrawString(//绘制ListBox中的
相应的文本item.Message,//该消息链接到项目
listBox1.Font,//以从列表框中
新SolidBrush(item.ItemColor)字体,//设置颜色
0 // X象素坐标
e.Index * listBox1.ItemHeight // Y象素坐标乘以该ItemHeight索引列表框中
中定义)。;
}
,否则
{
//该项目是不是MyListBoxItem,做点什么
}
}

有一些限制 - 主要的一个是,你需要编写自己的点击处理程序,并重新绘制适当的项目,使他们出现选择,因为Windows不会做在的OwnerDraw模式。但是,如果这仅仅是打算成为一个日志的东西在应用程序中发生的事情,你可能不关心项目出现的选择。



要滚动到最后一项尝试

  listBox1.TopIndex = listBox1.Items.Count  -  1; 


i am working on program on windows forms I have a listbox and I am validating data I want the correct data be added to the listbox with color green while the invalid data added with red color and I also want from the listbox to auto scroll down when an item is added and thanks

code :

try
{
    validatedata;
    listBox1.Items.Add("Successfully validated the data  : "+validateddata);
}
catch()
{
    listBox1.Items.Add("Failed to validate data: " +validateddata);
}

解决方案

Assuming WinForms, this is what I would do:

Start by making a class to contain the item to add to the listbox.

public class MyListBoxItem {
    public MyListBoxItem(Color c, string m) { 
        ItemColor = c; 
        Message = m;
    }
    public Color ItemColor { get; set; }
    public string Message { get; set; }
}

Add items to your listbox using this code:

listBox1.Items.Add(new MyListBoxItem(Colors.Green, "Validated data successfully"));
listBox1.Items.Add(new MyListBoxItem(Colors.Red, "Failed to validate data"));

In the properties of the ListBox, set DrawMode to OwnerDrawFixed, and create an event handler for the DrawItem event. This allows you to draw each item however you wish.

In the DrawItem Event:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    MyListBoxItem item = listBox1.Items[e.Index] as MyListBoxItem; // Get the current item and cast it to MyListBoxItem
    if (item != null) 
    {
        e.Graphics.DrawString( // Draw the appropriate text in the ListBox
            item.Message, // The message linked to the item
            listBox1.Font, // Take the font from the listbox
            new SolidBrush(item.ItemColor), // Set the color 
            0, // X pixel coordinate
            e.Index * listBox1.ItemHeight // Y pixel coordinate.  Multiply the index by the ItemHeight defined in the listbox.
        );
    }
    else 
    {
         // The item isn't a MyListBoxItem, do something about it
    }
}

There are a few limitations - the main one being that you'd need to write your own click handler and redraw appropriate items to make them appear selected, since Windows won't do that in OwnerDraw mode. However, if this is just intended to be a log of things happening in your application, you may not care about items appearing selectable.

To scroll to the last item try

listBox1.TopIndex = listBox1.Items.Count - 1;

这篇关于C#中:改变列表框项的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆