我有一个想要自动滚动ListBox
的客户端,我需要让它不显示Selected Item
上的蓝色条。这就是我对蓝条的意思......:
......“任务4”上方的蓝色条。
我见过这样的代码会删除它:
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();
}
但是这段代码对我不起作用,因为我在Timer
中运行选择事件,所以我不能做e.whatever
这样的事情,有什么方法可以做到这一点,在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
列表中运行。
感谢。
答案 0 :(得分:2)
谢谢Hans Passant !!这很简单,我之前尝试过这样做,但我必须采取不同的方式。
LstBxTaskList.SelectedIndex= - 1;
再次感谢Hans Passant !!
答案 1 :(得分: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();
}
}
}