1

我有一个需要自动滚动的客户,ListBox我需要让它不显示Selected Item. 这就是我所说的蓝条...:

选择栏

...“Task4”上方的蓝色条。

我见过这样的代码会删除它:

private void listBox1_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.Red : Color.White);
        g.FillRectangle(backgroundColorBrush, e.Bounds);

        // Set text color
        string itemText = listBox1.Items[itemIndex].ToString();

        SolidBrush itemTextColorBrush = (isItemSelected) ? new SolidBrush(Color.White) : new SolidBrush(Color.Black);
        g.DrawString(itemText, e.Font, itemTextColorBrush, listBox1.GetItemRectangle(itemIndex).Location);

        // Clean up
        backgroundColorBrush.Dispose();
        itemTextColorBrush.Dispose();
    }

    e.DrawFocusRectangle();
}

但是该代码对我不起作用,因为我在 a 中运行选择事件Timer,所以我不能做任何类似的事情e.whatever,有什么方法可以做到这一点,同时在 a 中运行它Timer

这是我的代码Timer

int ii = 0;
int i = 1;
private void timer1_Tick(object sender, EventArgs e)
{
    ii = LstBxTaskList.Items.Count;
    if (i == ii)
    {
        i = 0;
        LstBxTaskList.SelectedIndex = 0;
    }
    LstBxTaskList.SelectedIndex = i;
    i++;
}

并且该代码使Selected Item运行在Items.

谢谢。

4

2 回答 2

2

谢谢你汉斯帕桑特!!这很简单,我以前也试过这样做,但我一定是用不同的方式做的。

LstBxTaskList.SelectedIndex= - 1;

再次感谢汉斯帕桑特!!

于 2013-09-17T19:27:05.130 回答
1

这个怎么样:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int index = 0;

        public Form1()
        {
            InitializeComponent();

            this.theListBox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.theListBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler( this.theListBox_DrawItem );

            this.theTimer.Start();
        }

        void theTimer_Tick( object sender, EventArgs e )
        {
            this.theListBox.SelectedIndex = index;

            if ( ++index >= this.theListBox.Items.Count )
            {
                index = 0;
            }
        }

        void theListBox_DrawItem( object sender, DrawItemEventArgs e )
        {
            e.Graphics.FillRectangle( SystemBrushes.Window, e.Bounds );

            if ( e.Index >= 0 )
            {
                e.Graphics.DrawString( this.theListBox.Items[ e.Index ].ToString(), this.theListBox.Font, SystemBrushes.WindowText, e.Bounds );
            }

            // Comment out the following line if you don't want
            // the see the focus rectangle around the currently
            // selected item.
            //

            e.DrawFocusRectangle();

        }
    }
}
于 2013-09-17T19:31:46.650 回答