I'm getting "InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index". I'm don't which part I have messed up.
private void bankerCard_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Brush myBrush = Brushes.Black;
if (bankerCard.Items[e.Index].ToString().Contains("♥"))
myBrush = Brushes.Red;
else if (bankerCard.Items[e.Index].ToString().Contains("♦"))
myBrush = Brushes.Red;
else
myBrush = Brushes.Black;
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
Above code tested, no problem. Problem happens below. any clue?
private void playerCard_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Brush myBrush = Brushes.Black;
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); `error over here`
e.DrawFocusRectangle();
}
The error is self explanatory, probably here e.Index
value is -1
and you are trying to access (ListBox)sender).Items[-1]
((ListBox)sender).Items[e.Index].ToString()
To fix this you can add an if statement to check whether index is greater than -1:
if(e.Index >= 0)
{
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.