|
1. ItemIndex 가 -1인지 비교
if ( ListBox1->ItemIndex != -1 )
{
Label1->Caption = ListBox1->Items->Strings[ListBox1->ItemIndex];
}
2. Create 할때 ItemIndex = 0 으로 설정
ListBox1->ItemIndex = 0 ;
초보자 님이 쓰신 글 :
: 리스트박스에서 엉뚱한 인덱스를 참조할려고하면.
:
: raised exception class EStringListError with message 'List index out of bounds (-1)'.
:
: 에러가 발생합니다.
:
: 아래 예제는 버튼을 클릭하면 Listbox1에서 클릭된 위치에 문자열값을 가져오는건데
:
: 문제는 ListBox1목록중에 아무것도 클릭되지 않은상태에서 버튼을 누르면 out of bounds 에러가 발생합니다
:
: 버튼 누르는걸 막는건 힘든상황입니다.
:
: void __fastcall TForm8::Button2Click(TObject *Sender)
: {
: Label1->Caption = ListBox1->Items->Strings[ListBox1->ItemIndex];
: }
|