更改在ListBox WindowsForm C#中选择的颜色行项目

时间:2018-10-03 10:23:12

标签: c# listbox

我尝试通过以下代码(c#,windowsform,VS2017):

dynamic model;
private void ListBox_DrawItem(object sender, DrawItemEventArgs e)
{
    try
    {
        if (e.Index < 0) return;
        if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            e = new DrawItemEventArgs(e.Graphics,
                                      e.Font,
                                      e.Bounds,
                                      e.Index,
                                      e.State ^ DrawItemState.Selected,
                                      e.ForeColor,
                                      Color.LimeGreen);

        e.DrawBackground();

        e.Graphics.DrawString(listBox.Items.OfType<Game>().Select(table => table.name).ElementAt(e.Index), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
        e.DrawFocusRectangle();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "MainForm.ListBox_DrawItem()", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

要将“列表框”中所选项目的行的颜色(默认为蓝色)更改为另一种颜色(此处为绿色)

这是渲染: LisBox: change color item selected

但是我想用一个开关在4个不同的对象之间进行选择,但没有其他效果:

        dynamic model;
    private void ListBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        try
        {
            if (e.Index < 0) return;
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                e = new DrawItemEventArgs(e.Graphics,
                                          e.Font,
                                          e.Bounds,
                                          e.Index,
                                          e.State ^ DrawItemState.Selected,
                                          e.ForeColor,
                                          Color.LimeGreen);

            e.DrawBackground();

            switch (selectedModel)
            {
                case "Game":
                    model = listBox.Items.Cast<Game>().Select(table => table.name).ElementAt(e.Index);
                    break;
                case "Gender":
                    model = listBox.Items.Cast<Gender>().Select(table => table.name).ElementAt(e.Index);
                    break;
            }

            e.Graphics.DrawString(model, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
            e.DrawFocusRectangle();
        }

我尝试使用没有成功: OfType <>和Cast <>

model = listBox.Items.Cast<Game>().Select(table => table.name).ElementAt(e.Index);

model = listBox.Items.OfType<Game>().Select(table => table.name).ElementAt(e.Index);

异常:System.Core.dll中的'System.InvalidCastException'

谢谢您的帮助,如果我不太清楚,我会事先为自己辩解,尝试为我的要求简化图片。


编辑:

我回答自己,这可能帮助了多个人! 我最终找到了解决方案(但我认为不是很干净) 她在那儿

if (listBox.SelectedItem.GetType() == typeof(Game))
{
    e.Graphics.DrawString(listBox.Items.Cast<Game>().Select(table => table.name).ElementAt(e.Index), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
    e.DrawFocusRectangle();
}
if (listBox.SelectedItem.GetType() == typeof(Gender))
{
    e.Graphics.DrawString(listBox.Items.Cast<Gender>().Select(table => table.name).ElementAt(e.Index), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
    e.DrawFocusRectangle();
}

显然,该开关不起作用,但我不明白为什么?

因此,如果您有一个更好的主意,使其更紧凑,更优雅,我将为您服务! 谢谢大家

0 个答案:

没有答案