Q&A

  • listview에서 아이템 삭제시 에러가 납니다.
아래와 같이 코딩하니까 에러가 발생하네요...

에러 내용은 "List index out of bounds (7)" 이렇습니다..

왜그런지 이유를 잘모르겠습니다. 고수님들 밑의 코딩좀 잘되게 해결해 주세요.ㅠ.ㅠ







if(mode = 'search') then

begin

search_start := search_start_year.text+'년'+' '+

search_start_month.Text+'월'+' '+

search_start_day.text+'일';

search_end := search_end_year.text+'년'+' '+

search_end_month.text+'월'+' '+

search_end_day.text+'일';

for a:=0 to listview.items.Count -1 do

begin

if((Copy(listview.items[1].SubItems[a],0,16) >= search_start) and

(Copy(listview.items[1].SubItems[a],0,16) <= search_end)) then

begin



end else

begin

listview.Items[a].Delete;

end;

end;

end;

1  COMMENTS
  • Profile
    홍성락 2001.10.13 04:33
    이호림 wrote:

    > 아래와 같이 코딩하니까 에러가 발생하네요...

    > 에러 내용은 "List index out of bounds (7)" 이렇습니다..

    > 왜그런지 이유를 잘모르겠습니다. 고수님들 밑의 코딩좀 잘되게 해결해 주세요.ㅠ.ㅠ

    >

    >

    >

    > if(mode = 'search') then

    > begin

    > search_start := search_start_year.text+'년'+' '+

    > search_start_month.Text+'월'+' '+

    > search_start_day.text+'일';

    > search_end := search_end_year.text+'년'+' '+

    > search_end_month.text+'월'+' '+

    > search_end_day.text+'일';

    > for a:=0 to listview.items.Count -1 do

    > begin

    > if((Copy(listview.items[1].SubItems[a],0,16) >= search_start) and

    > (Copy(listview.items[1].SubItems[a],0,16) <= search_end)) then

    > begin

    >

    > end else

    > begin

    > listview.Items[a].Delete;

    > end;

    > end;

    > end;

    ////////////////////////////////////////////

    아이템을 지우는 순간 아이템 인덱스가 바뀌거든요,

    그래서 for문으로 지정한 카운트 만큼가다가 줄어든 인덱스를 초과해 일어나는 에러입니다.

    아래 방법보다 더 좋은 방법도 있겠으나 데스트해보세요.

    statNum := 0;

    repeat

    for a:= statNum to listview.items.Count -1 do begin

    if .....begin

    listview.Items[a].Delete;

    statNum := a;

    break;

    end;

    end;

    until a = statNum to listview.items.Count-1;