源程序:
我的程序为:
这个怎么解决?书上的程序没问题,我的也一样,就出错!
有,通不过?要是删掉了,就通过了。31行是我自己添加的,把错误删了,结果和书上的一样结果。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace BeautifulListBox2
{
public partial class DrawListBox :ListBox
{
public DrawListBox()
{
InitializeComponent();
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ListBox_DrawItem);
}
#region
private static Brush[] listBoxBrushes;
private static int place = -1;
private static bool naught = true;
#endregion
private bool TGradualC = false;
[Browsable(true), Category("控件的重绘设置"), Description("判断是否进行渐变色的设置")]
public bool GradualC
{
get { return TGradualC; }
set
{
TGradualC = value;
this.Invalidate();
}
}
private Color TColorSelect = Color.Gainsboro;
[Browsable(true), Category("控件的重绘设置"), Description("项被选中后的高亮度颜色")]
public Color ColorSelect
{
get { return TColorSelect; }
set
{
TColorSelect = value;
this.Invalidate();
}
}
private Color TColor1 = Color.CornflowerBlue;
[Browsable(true), Category("控件的重绘设置"), Description("第一个颜色的设置")]
public Color Color1
{
get { return TColor1; }
set
{
TColor1 = value;
this.Invalidate();
}
}
private Color TColor1Gradual = Color.Thistle;
[Browsable(true), Category("控件的重绘设置"), Description("第一个颜色的渐变色设置")]
public Color Color1Gradual
{
get { return TColor1Gradual; }
set
{
TColor1Gradual = value;
this.Invalidate();
}
}
private Color TColor2 = Color.PaleGreen;
[Browsable(true), Category("控件的重绘设置"), Description("第二个颜色的设置")]
public Color Color2
{
get { return TColor2; }
set
{
TColor2 = value;
this.Invalidate();
}
}
private Color TColor2Gradual = Color.DarkKhaki;
[Browsable(true), Category("控件的重绘设置"), Description("第二个颜色的渐变色设置")]
public Color Color2Gradual
{
get { return TColor2Gradual; }
set
{
TColor2Gradual = value;
this.Invalidate();
}
}
private void ListBox_DrawItem(object sender, DrawItemEventArgs e)
{
Rectangle r = new Rectangle(0, 0, this.Width, this.Height);
SolidBrush SolidB1 = new SolidBrush(Color.CornflowerBlue);
SolidBrush SolidB2 = new SolidBrush(Color.PaleGreen);
LinearGradientBrush LinearG1 = new LinearGradientBrush(r, Color.CornflowerBlue, this.Color1Gradual, LinearGradientMode.BackwardDiagonal);
LinearGradientBrush LinearG2 = new LinearGradientBrush(r, this.Color2, this.Color2Gradual, LinearGradientMode.BackwardDiagonal);
listBoxBrushes = new Brush[] { SolidB1, LinearG1, SolidB2, LinearG2 };
e.DrawBackground();
if (this.Items.Count <= 0)
return;
if (e.Index == (this.Items.Count - 1))
{
bool tem_bool = true;
if (e.Index == 0 && tem_bool)
naught = false;
}
if (naught)
{
Brush brush = listBoxBrushes[place = (GradualC) ? (((e.Index % 2) == 0) ? 1 : 3) : (((e.Index % 2) == 0) ? 0 : 2)];
e.Graphics.FillRectangle(brush, e.Bounds);
bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? true : false;
if (selected)
{
e.Graphics.FillRectangle(new SolidBrush(ColorSelect), e.Bounds);
}
e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, Brushes.Black, e.Bounds);
}
e.DrawFocusRectangle();
}
}
}