这是我尝试运行程序时收到的错误。 (请原谅我的背景,这是我试图创造的关键部分)
这是我尝试执行的C#代码
int oldCount;
string RecentPlayer;
string val;
public void SetBalloonTip()
{
NewPlayerMessage.BalloonTipTitle = "A Player Has Joined!";
NewPlayerMessage.BalloonTipText = RecentPlayer + " has joined your server " + ConfigKey.ServerName.GetString();
NewPlayerMessage.BalloonTipIcon = ToolTipIcon.Info;
}
private void PlayerListUpdates_Tick(object sender, EventArgs e)
{
oldCount = playerList.Items.Count - 1;
if (oldCount < playerList.Items.Count)
{
if (playerList.Items.Count > 0)
{
RecentPlayer = playerList.Items[playerList.Items.Count].ToString();
val = playerList.Items[playerList.Items.Count].ToString();
NewPlayerMessage.Visible = true;
SetBalloonTip();
NewPlayerMessage.ShowBalloonTip(50000);
}
}
else {
return;
}
}
答案 0 :(得分:7)
解决方案1:您需要记住,数组索引始终以Zero
开头,以Count-1
替换它:
playerList.Items[playerList.Items.Count]
有了这个:
playerList.Items[playerList.Items.Count-1]
OR
解决方案2:只需使用包含值oldCount
的{{1}}变量
替换它:
playerList.Items.Count - 1
有了这个:
playerList.Items[playerList.Items.Count]