Frage

This is the error I've been recieving when I attempt to run my program. (Excuse the Minecraft in the background, it's a key part of what I'm attempting to create)

Error Recieved

Here's the C# code that I was trying to execute

    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;
        }

    }
War es hilfreich?

Lösung

Solution 1 : You need to remember that Array index always starts with Zero and ends with Count-1

Replace This:

playerList.Items[playerList.Items.Count]

With This:

playerList.Items[playerList.Items.Count-1]

OR

Solution 2 : Just use oldCount variable which holds the value playerList.Items.Count - 1

Replace This:

playerList.Items[playerList.Items.Count]

With This:

playerList.Items[oldCount]
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top