2015-06-07 97 views
-5
__fastcall TForm1::FillList() { 
TSearchRec FoundedFile; 
int r, i; 
int count = 0; 
int ir = 1; 
char ext[3] = {'jpg', 'gif', 'png'}; 
for (i = 0; i < 3; i++) { 

r = FindFirst(Path + "*." + ext[i], faAnyFile, FoundedFile); 

ListBox1->Items->Clear(); 
if (r == 0) { 

    ListBox1->Items->Add(FoundedFile.Name); 

    while (FindNext(FoundedFile) == 0) { 
     ListBox1->Items->Add(FoundedFile.Name); 

    } 
    ListBox1->ItemIndex = 0; 

    String File = 
     String(Path + ListBox1->Items->Strings[ListBox1->ItemIndex]); 
    //ShowMessage(File); 
    Image1->Picture->LoadFromFile(File); 
    count = ListBox1->Items->Count; 
    } 

} 

if (ir == 0) { 
    return -1; 
} 
else { 
    count = ListBox1->Items->Count; 
    return count; 
} 

} 

按鈕不會讀取圖片擴展數組ext [i]。 程序不顯示帶.gif,.png和.jpg擴展名的圖片。 如果沒有數組,並且只是r = FindFirst(Path +「(my extension)」,faAnyFile,FoundFile),那麼它會顯示帶有我的擴展名的圖片。圖片瀏覽器C++構建器

回答

0

您正在嘗試填充ListBox並在相同的代碼中顯示圖像。它應該作爲單獨的操作來實現。 FillList()應該只填寫列表框,那麼您可以在ListBox的OnClick事件顯示所選擇的圖像(沒有OnChange事件,除非你子類列表框,直接攔截LBN_SELCHANGE通知),例如:

void __fastcall TForm1::FillList() 
{ 
    TSearchRec FoundedFile; 
    int r, i; 
    char* ext[3] = {"jpg", "gif", "png"}; 

    ListBox1->Items->BeginUpdate(); 
    try 
    { 
     ListBox1->Items->Clear(); 

     for (i = 0; i < 3; i++) 
     { 
      r = FindFirst(Path + "*." + ext[i], faAnyFile, FoundedFile); 
      if (r == 0) 
      { 
       do 
       { 
        ListBox1->Items->Add(FoundedFile.Name); 
       } 
       while (FindNext(FoundedFile) == 0); 
       FindClose(FoundedFile); 
      } 
     } 
    } 
    __finally 
    { 
     ListBox1->Items->EndUpdate(); 
    } 

    ListBox1->ItemIndex = 0; 
} 

void __fastcall TForm1::ListBox1Click(TObject *Sender) 
{ 
    int index = ListBox1->ItemIndex; 
    if (index != -1) 
    { 
     String File = Path + ListBox1->Items->Strings[ListBox1->ItemIndex]); 
     //ShowMessage(File); 
     Image1->Picture->LoadFromFile(File); 
    } 
    else 
     Image1->Picture->Assign(NULL); 
} 
+0

@VladfromMoscow:我很着急,並在平板電腦上打字。我現在修好了。 –

0

它應該是char *ext[3] = {"jpg", "gif", "png"};而不是char ext[3] = {'jpg', 'gif', 'png'};