중복 된 질문처럼 보일 수 있습니다. (이 오류에 대해 묻는 다른 많은 질문을 보았습니다.) 그러나 나는 답을 찾을 수 없습니다. 제가 겪고있는 문제를 설명합니다. 오류가 내가 읽은 바로는추가 정보 : InvalidArgument = '0'의 값이 'index'에 유효하지 않습니다.
invList.SelectedItems[0].Text //invList is a ListView
호출에 의해 유발되며,이 명령에 의해 자극 오류가 시도 비어있는 목록에 대한 액세스 (또는 배열?) ListView에있는 선택한 항목의 표시한다. 그러나 내가 액세스하려고 시도하는 ListView는 명령 실행시 항목으로 채워집니다.
방법 # 1 : 여기서 I는이 명령 실행되는 방법이다의 deleteItem는
public bool deleteItem()
{
if (invList.SelectedItems.Count == 0) //if no item selected...
{
MessageBox.Show("Error: No item selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
else
{
DialogResult dialogResult = MessageBox.Show(
"Are you sure you want to delete item: " + invList.SelectedItems[0].Text + "?",
"Error", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dialogResult == DialogResult.Yes)
{
for (int i = 0; i < itemRecords.Count; i++)
{
if ((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == clinicName))
{
itemRecords.Remove(itemRecords[i]);
listRefresh();
}
}
return true; //return true to indicate that data has been edited
}
return false; // return false to indicate that nothing has changed
}
}
방법 # 2에 updateItem
public bool updateItem()
{
if (invList.SelectedItems.Count == 0) //if no item selected
{
MessageBox.Show("Error: No item selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
else
{
for (int i = 0; i < itemRecords.Count; i++)
{
//if id == the id of the selected item
if((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == this.Text))
{
ItemAddition itemAddition = new ItemAddition(itemRecords, itemRecords[i], this);
itemAddition.ShowDialog();
}
}
return true;
}
}
listRefresh :
public void listRefresh()
{
invList.Items.Clear();
loadItems();
}
loadItems :
private void loadItems()
{
foreach (Record r in itemRecords)
{
if (r.practice == clinicName)
invList.Items.Add(r.ToString());
}
}
첫 번째 메서드가 호출 될 때 오류가 호출되지만 두 번째 메서드가 호출 될 때는 오류가 호출되지 않습니다. 이 불일치는 내 혼란의 이유입니다. 이 오류가 첫 번째 방법에서만 발생하는 이유가 있습니까?
listRefresh 메소드의 코드를 추가 할 수 있습니까? – Damith
@Damith 물론, 그것을 추가했습니다. – AustinC
'for (int i = 0; i