簡體   English   中英

在C ++ Builder XE 8(firemonkey)中手動創建TOpenDialog

[英]Creating TOpenDialog manually in C++ Builder XE 8 (firemonkey)

我正在使用C ++ Builder XE8。 由於TOpenDialog在Android上不起作用,因此我嘗試自己制作此類東西。 我的邏輯很簡單。 它將開始從“ / storage”檢查文件和文件夾,並在TListView上顯示所有項目。 如果我觸摸一個文件夾(名稱),它將打開該文件夾;如果我觸摸一個文件,它將在標簽上顯示該名稱。 因此,我為TListViewOnItemClick事件分配了一個函數。

這是代碼。 fpath是String,Label1顯示當前文件夾,Label2顯示所選文件。

void __fastcall TForm1::lviewitemclck(TObject * const Sender, TListViewItem * const AItem)
{
if (AItem->Text == "<< BACK") {
        if (!fpath.LastDelimiter("/") == 0) {
            fpath = fpath.SubString(0, fpath.LastDelimiter("/"));

            Label1->Text = fpath;
            Form1->showfiles(fpath);
        }
   }
   else if ( DirectoryExists(fpath+ AItem->Text)) {
            fpath = fpath+ AItem->Text;

            Label1->Text = fpath;
            Form1->showfiles(fpath);
    }
    else if (FileExists(fpath+ AItem->Text)) {
         Label2->Text ="File: "+ fpath+ AItem->Text;
   }
}

以下是掃描文件和文件夾並顯示它們的功能代碼。 字符串列表是TStringList。

void __fastcall TForm1::showfiles (String path)
{

TSearchRec sr;  // for scaning files and folders
TSearchRec fr;  // to check whether the folder is accessible or not.

if (FindFirst(path+"/*", faAnyFile, sr) == 0)
    {
        stringlist->Clear();
        stringlist->Add("<< BACK");  // being used to replace the ".."

        do{
            if(sr.Name != "."   &&   sr.Name != ".."){

                    if (DirectoryExists(path+"/"+sr.Name)) {
                        if (FindFirst(path+"/"+sr.Name+"/*", faAnyFile, fr) == 0) { // to check if the folder is accessible
                            stringlist->Add("/"+ sr.Name);
                        }
                        FindClose(fr);
                    }
                    else{
                        stringlist->Add("/"+ sr.Name);
                    }

            }
        }  while (FindNext(sr) == 0);
    }
    FindClose(sr);

  stringlist->Sort();

  Form1->Item->Free();

  Form1->ListView1->BeginUpdate();

  Form1->ListView1->ClearItems();

for( int i =0;i< stringlist->Count; i++){
     Form1->Item = Form1->ListView1->Items->Add();
     Form1->Item->Text = stringlist->Strings[i];
}
 Form1->ListView1->EndUpdate();

}

這里的問題是,如果我在TForm1 :: showfiles中使用ListView1->ClearItems() ,它將顯示一個錯誤,提示“地址訪問沖突(隨機數),訪問地址00000009”。 而且,如果我不使用ClearItems()那么只需添加更多已存在的行即可。 我是一個初學者,所以我不知道我在哪里做錯了。

您應該使用:

ListView1->Items->Clear

到目前為止,我發現的最好方法是動態創建TListView並在每次要添加新項(或調用showfiles函數)時將其刪除。 我編寫了一個小函數(命名為refresh)以release已經創建的TListView並調用另一個函數(命名為create_lview),該函數可以再次創建實例,然后調用showfiles方法。

void __fastcall TForm1::refresh()
{
 if (!lview1->Released()) {
  try{
    lview1->Release();
    Form1->create_lview();
    Form1->showfiles(fpath);
}
catch(...){
    Label2->Text = "error in cleaning";
    }
  }
}

這是在需要時創建TListView的代碼。

void __fastcall TForm1::create_lview()
{
    lview1 = new TListView(Form1);
    lview1->Parent = Form1;
    lview1->Height = 600;
    lview1->Width = 400;
    lview1->Position->X = 0;
    lview1->Position->Y = 0;
    lview1->Visible = true;
    lview1->Enabled = true;
    lview1->OnItemClick = lviewitemclck;
    lview1->CanSwipeDelete = false;
}

如果發現任何錯誤或可以更有效地進行處理,請發表評論。

我嘗試了另一種方法來避免錯誤,方法是使用更新項文本替換Clear方法,然后刪除ListView1Change事件中ListView的未使用的最后一行。

void __fastcall TForm1::ListView1Change(TObject *Sender)
{
  //Delete last item
  while (ListView1->Items->Count>stringlist->Count){
  ListView1->Items->Delete(ListView1->Items->Count-1);
  }
}

void __fastcall TForm1::showfiles (String path)
{


TSearchRec sr;  // for scaning files and folders
TSearchRec fr;  // to check whether the folder is accessible or not.
             //path+PathDelim+

if (FindFirst(path+PathDelim+'*', faAnyFile, sr) == 0)
    {
        stringlist->Clear();
        stringlist->Add("<<--BACK");  // being used to replace the ".."

        do{
            if(sr.Name != "."   &&   sr.Name != ".."){

                    if (DirectoryExists(path+PathDelim+sr.Name)) {
                        if (FindFirst(path+PathDelim+sr.Name+PathDelim+"*", faAnyFile, fr) == 0) { // to check if the folder is accessible
                            stringlist->Add(sr.Name);

                        }
                        FindClose(fr);
                    }
                    else{
                        stringlist->Add(sr.Name);
                    }

            }
        }  while (FindNext(sr) == 0);
    }
    FindClose(sr);

  stringlist->Sort();

  for( int i =0;i< ListView1->Items->Count; i++){
     ListView1->Items->Item[i]->Text="";
  }



 ListView1->BeginUpdate();
 try {


      for( int i =0;i< stringlist->Count; i++)
        {
           if (ListView1->Items->Count-1<i)
           {
            TListViewItem* Item=ListView1->Items->Add();
            Item->Text=stringlist->Strings[i];
           } else
           {
            TListViewItem* Item=ListView1->Items->Item[i];
            Item->Text=stringlist->Strings[i];

           }
        }

     }
 catch (...) {
             }


ListView1->EndUpdate();

/* */
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM