C# 如何在列表视图中获取选定行上方和下方的行的内容?
当我单击一行以删除其内容并将上面和下面行的内容合并为一行时,我的listview会间歇性地出现错误。当我在程序的另一部分使用代码时,它可以工作,但由于某种原因,它在下面列出的代码中给出了一个错误。是否有方法从listview中我所选行的上下行获取代码C# 如何在列表视图中获取选定行上方和下方的行的内容?,c#,listview,listviewitem,C#,Listview,Listviewitem,当我单击一行以删除其内容并将上面和下面行的内容合并为一行时,我的listview会间歇性地出现错误。当我在程序的另一部分使用代码时,它可以工作,但由于某种原因,它在下面列出的代码中给出了一个错误。是否有方法从listview中我所选行的上下行获取代码 if ( iIndex > 0 && iIndex +1 < listView1.Items.Count) {///get my error on the row bel
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;
}
}
if(iIndex>0&&iIndex+10&&iIndex
您无法从您的SelectedDiges
中获取位置-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;
if(listView1.Items.Count==0 | | listView2.Items.Count<2)
返回;
我不太明白为什么您在第二个示例中使用了正确的尝试,但在第一个示例中无效。如前所述,'-1'的值无效,因为'index'是罪魁祸首。索引从0开始。因此出现了错误。我明白了这一点,但我不明白为什么它在代码的第二部分中有效,以及如何获取行above my selected row?我只需要能够在我的listview中单击一行,如果上面和下面的行中有“空闲空间”字样,则保存它们的内容,然后删除上面和下面的行并将它们合并为一行。使字符串content1=listView1.Items[listView1.SelectedDices[-1]]。子项[0].Text;as string content1=listView1.Items[listView1.selecteddinces[0]].SubItems[0].Text;但这只会选择我正在单击的行。上面的行呢?你是说它在其他代码中起作用,因为我有string content1=listView1.Items[listView1.selecteddinces[0].SubItems[0].Te在使用content1=listView1.Items[listView1.SelectedIndices[-1]]之前先存储xt。子项[