列表框项目背景颜色 [英] Listbox Item Background color

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

问题描述



我正在编写一个FTP程序,该程序在列表框中显示要传输的文件的列表.成功传输文件后,我想将背景色更改为绿色,如果失败,则将其更改为红色.我没有看到如何执行此操作.有谁知道如何更改单个项目的背景颜色?

谢谢您,

Hi,

I''m writing an FTP program that displays the list of files to be transferred in a listbox. When the file is transferred successfully, I would like to change the background color to green and if it fails, change it to red. I am not seeing how to do this. Anyone know how to change the background color of a single item?

Thank you,

推荐答案

我写了一篇文章,该文章自定义了ListBox中的图形.

Anagrams-C#中的文字游戏 [
为此,我将列表框的DrawMode属性设置为OwnerDrawFixed,然后覆盖了DrawItem行为.这使我可以绘制某些外观不同的项目.对于没有被玩家发现的单词,我选择了深灰色文本,对于用户发现的单词,我使用了红色粗斜体文本.

如果下载本文的代码,您将清楚知道需要执行的操作.
I wrote an article that customizes the drawing in a ListBox.

Anagrams - A Word Game in C#[^]

The following is an excerpt of the appropriate section of the article:

The Word List

I wanted a way to show the user what he''d typed, so I decided on a list box. As the program validates and accepts submitted words, they are added to the list box (sorted alphabetically). Then, when the user clicked Solve or the round expired naturally, I wanted to show all of the words that were possible, yet show the words that the user had submitted in such a way as to highlight those words.

To accomplish this, I set the list box''s DrawMode property to be OwnerDrawFixed, and then overrode the DrawItem behavior. This allowed me to draw certain items with different appearances. I settled on dark gray text for words that were not discovered by the player, and bold red italic text for words the user found.


If you download the code for the article, you''ll get a clear idea of what you''ll need to do.


此代码根据索引为项目的背景着色-建议在该索引处处理项目的属性.

(添加了ListBox的表格[FormMain]称为listBox)
This code colors the item''s background based on index - ammend to work on a property of the item at that index.

(Form [FormMain] with a ListBox added called listBox)
using System.Drawing;
using System.Windows.Forms;

namespace CP.QA.WinForms
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            listBox.DrawMode = DrawMode.OwnerDrawFixed;
            listBox.DrawItem += new DrawItemEventHandler(listBox_DrawItem);
            listBox.Items.AddRange(new object[] { "A", "B", "C" });
        }

        void listBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index > -1)
            {
                if (e.Index == 0)
                    e.Graphics.FillRectangle(Brushes.Green, e.Bounds);
                else if (e.Index == 1)
                    e.Graphics.FillRectangle(Brushes.Red, e.Bounds);
                else
                    e.DrawBackground();
                using (Brush textBrush = new SolidBrush(e.ForeColor))
                {
                    e.Graphics.DrawString(listBox.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds.Location);
                }
            }
        }
    }
}


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

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