This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?

(4 个回答)


3个月前关闭。




我收到以下错误:

对于以下代码。
private void button_confirm_Click(object sender, EventArgs e)
    {
        listBox_savedata.Items.Add(textBox_ordertostart.Text +" "+ comboBox_suburb.SelectedItem.ToString() + " " + dateTimePicker2.Value.ToShortDateString());


            for (int i = 0; i <= listBox_savedata.Items.Count; i++)
            {
                string s1 = listBox_savedata.Items[i].ToString();
                int startpos = s1.IndexOf("PM");
                string sub = s1.Substring(0, 5);
                if(sub+" "+"PM"==DateTime.Now.ToString("HH:mm tt"))
                    {
                    mplayer.PlayLooping();

            }
        }
    }

最佳答案

更改您的 for 循环:
老的:

for (int i = 0; i <= listBox_savedata.Items.Count; i++) // <=
新的:
for (int i = 0; i < listBox_savedata.Items.Count; i++) // <

关于C# InvalidArgument= '1' 的值对 'index' 无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40449611/

10-12 22:30