当我点击一行以删除其内容并将上下行的内容合并为一行时,我的listview会间歇性地收到错误。当我在程序的另一部分中使用代码时它可以工作,但由于某种原因,它在下面的代码列表中给出了错误。有没有办法从列表视图中所选行的上下行获取代码?
if ( iIndex > 0 && iIndex +1 < listView1.Items.Count)
{///get my error on the row below for content one 'InvalidArgument=Value of '-1' is not valid for 'index'.'
string content1 = listView1.Items[listView1.SelectedIndices[-1]].SubItems[0].Text;
string content2 = listView1.Items[listView1.SelectedIndices[1]].SubItems[0].Text;
string NumCellAbove = listView1.Items[listView1.SelectedIndices[0] - 1].SubItems[1].Text;
string NumCellBelow = listView1.Items[listView1.SelectedIndices[0] + 1].SubItems[1].Text;
int TotalFsCt1 = int.Parse(content);
int TotalFsCtAbove1 = int.Parse(NumCellAbove);
int TotalFsCtBelow1 = int.Parse(NumCellBelow);
int TotalFsCt= TotalFsCt1 + TotalFsCtAbove1 + TotalFsCtBelow1;
int TotalFsCtAbove = TotalFsCt1 + TotalFsCtAbove1;
int TotalFsCtBelow = TotalFsCt1 + TotalFsCtBelow1;
if (content1 != "Free Space" && content2 != "Free Space")
{
listView1.Items.Remove(listView1.FocusedItem);
ListViewItem item3 = new ListViewItem("Free Space");
item3.BackColor = Color.Yellow;
item3.UseItemStyleForSubItems = false;
item3.SubItems.Add(new ListViewItem.ListViewSubItem(item3,
content, Color.Black, Color.Yellow, item3.Font));
item3.SubItems.Add(new ListViewItem.ListViewSubItem(item3,
" 0", Color.Black, Color.White, item3.Font));
listView1.Items.Insert(iIndex, item3);
totalMBct1 += newTotalct;
SetHeight(listView1, 256 / listView1.Items.Count);
if (button1.Enabled == false)
{
MessageBox.Show("Enabling button again memory less then full!");
button1.Enabled = true;
}
}
}
////Yet this code below this line works fine and never gives the error
if (iIndex > 0 && iIndex < listView1.Items.Count)
{
string content1 = listView1.Items[listView1.SelectedIndices[0]].SubItems[0].Text;
string content2 = listView1.Items[listView1.SelectedIndices[0] - 1].SubItems[0].Text;
string NumCellAbove = listView1.Items[listView1.SelectedIndices[0] -1].SubItems[1].Text;
int TotalFsCt1 = int.Parse(content);
int TotalFsCtAbove1 = int.Parse(NumCellAbove);
int TotalFsCtAbove = TotalFsCt1 + TotalFsCtAbove1;
if (content1 != "Free Space" && content2 == "Free Space")
{
string Above = TotalFsCtAbove.ToString();
listView1.Items[iIndex -1].Remove();
listView1.Items.Remove(listView1.FocusedItem);
ListViewItem item3 = new ListViewItem("Free Space");
item3.BackColor = Color.Yellow;
item3.UseItemStyleForSubItems = false;
item3.SubItems.Add(new ListViewItem.ListViewSubItem(item3,
Above, Color.Black, Color.Yellow, item3.Font));
item3.SubItems.Add(new ListViewItem.ListViewSubItem(item3,
" 0", Color.Black, Color.White, item3.Font));
listView1.Items.Insert(iIndex-1, item3);
totalMBct1 += newTotalct;
SetHeight(listView1, 256 / listView1.Items.Count);
if (button1.Enabled == false)
{
MessageBox.Show("Enabling button again memory less then full!");
button1.Enabled = true;
}
}
答案 0 :(得分:0)
您无法从SelectedIndices
获取位置-1的元素,因为位置-1处没有项目,因此您将获得异常。您可能想要做的是将项目放在selectedIndex的位置 - 1.因此,您必须做类似的事情:
string content1 = listView1.Items[listView1.SelectedIndices[0]-1].SubItems[0].Text;
string content2 = listView1.Items[listView1.SelectedIndices[0]+1].SubItems[0].Text;
但是,我建议首先检查一个项目是否实际被选中:
if (listView1.SelectedIndices.Count == 0)
return;
你应该检查你的清单中是否包含足够的元素:
if (listView1.Items.Count == 0 || listView2.Items.Count < 2)
return;
我不太明白为什么你在第二个例子中使用了正确的尝试,但是在第一个例子中没有。