我有以下代码。
我正在尝试将值插入到列表框中,然后能够按字母顺序对这些值进行求值并将它们重新显示在同一个列表框中。出于某种原因代码不起作用(没有错误 - 就在我按下按钮时列表框清除)
protected void sortButton_Click(object sender, ImageClickEventArgs e)
{
string[] movieArray = new string [cartListBox.Items.Count];
for (int i = 0; i < cartListBox.Items.Count; i++)
{
movieArray[i] = cartListBox.Items[i].ToString();
}
Array.Sort(movieArray);
cartListBox.Items.Clear();
for (int i = 0; i < cartListBox.Items.Count; i++)
{
cartListBox.Items.Add(movieArray[i].ToString());
}
}
最佳答案
我认为问题出在最后一个循环中。
像下面那样做:
cartListBox.Items.Clear();
for (int i = 0; i < movieArray.Length; i++)
{
cartListBox.Items.Add(movieArray[i].ToString());
}
当您清除 cartListBox.Items.Clear();
时, 它不应该像 for (int i = 0; i < cartListBox.Items.Count; i++)
这样的循环计数器
cartListBox.Items.Count
正在制造问题。
关于c# - 数组值到列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16712273/