示例1(此示例轉自ccrun 妖哥):
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // 切換ListView的顯示風格為詳細信息 ListView1->ViewStyle = vsReport; // 清除ListView所有列和行的內容 ListView1->Columns->Clear(); ListView1->Items->Clear(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { // 添加第一列 TListColumn *lc = ListView1->Columns->Add(); lc->Caption = "第一列"; lc->Width = 120; // 添加第二列 lc = ListView1->Columns->Add(); lc->Caption = "第二列"; lc->Width = 80; // 添加第三列 lc = ListView1->Columns->Add(); lc->Caption = "第三列"; lc->Width = 100; Application->ProcessMessages(); // 修改第二列的標題 ShowMessage("點擊確定后修改第二列的標題"); if (ListView1->Columns->Count > 1) { lc = ListView1->Columns->Items[1]; lc->Caption = "第二列新標題"; lc->Width = 120; } // 添加第一行數據 TListItem *li = ListView1->Items->Add(); li->Caption = "一行一列"; li->SubItems->Add("一行二列"); li->SubItems->Add("一行三列"); // 添加第二行數據 li = ListView1->Items->Add(); li->Caption = "二行一列"; li->SubItems->Add("二行二列"); li->SubItems->Add("二行三列"); // 添加第三行數據 li = ListView1->Items->Add(); li->Caption = "二行一列"; li->SubItems->Add("二行二列"); li->SubItems->Add("二行三列");
//加Application->ProcessMessages()只是為了讓窗口處理一下其他的消息,界面上的顯示更新一下而已.在有些情況下,窗口內容變化但界面沒有刷新時,可以加這么一句. Application->ProcessMessages(); // 修改第三行的數據 ShowMessage("點擊確定后修改第三行的數據"); if (ListView1->Items->Count > 2) { li = ListView1->Items->Item[2]; li->Caption = "二行一列新內容"; li->SubItems->Strings[0] = "二行二列新內容"; li->SubItems->Strings[1] = "二行三列新內容"; } }
示例2:
ListView右鍵點擊圖標彈出菜單的用法
將ListView的PopupMenu屬性設為MyPopupMenu,這樣只要選中ListView點右鍵即可出現我的MyPopupMenu菜單。
但這種方式,無論右擊ListView上的任何位置,都會彈出菜單,要想必須選中某個子項后才能進行菜單事件的話,可以在菜單事件中加入判斷,沒有選中則返回
if(ListView1->Selected == NULL)
return;
另一種方式是,將ListView的PopupMenu屬性留空,在ListView的OnMouseDown事件中處理
void __fastcall TMainForm::ListView1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { //不是右擊或者沒有點中則返回,不作反應 if(Button != mbRight || NULL==ListView1->GetItemAt(X, Y)) return; //只點選圖標,不能點文字,可以加以下3行 THitTests ht=ListView1->GetHitTestInfoAt(X,Y); if(!ht.Contains(htOnIcon)) return; PopupMenuZW->Popup(Mouse->CursorPos.x, Mouse->CursorPos.y); }
示例3
多個ListView共用一個PopupMenu,菜單事件中要傳遞被選中的項目信息,如果是數字,可以在點擊事件中復制給PopupMenu的Tag,在菜單事件中調用Tag值即可,如果是文字信息,目前想到的是如下方法:
void __fastcall TMainForm::N2Click(TObject *Sender) { if (PopupMenuZW->PopupComponent->Name=="ListView1") { if(ListView1->Selected == NULL) return; Edit4->Text=ListView1->Selected->Caption; } if (PopupMenuZW->PopupComponent->Name=="ListView2") { if(ListView2->Selected == NULL) return; Edit4->Text=ListView2->Selected->Caption; } if (PopupMenuZW->PopupComponent->Name=="ListView3") { if(ListView3->Selected == NULL) return; Edit4->Text=ListView3->Selected->Caption; } if (PopupMenuZW->PopupComponent->Name=="ListView4") { if(ListView4->Selected == NULL) return; Edit4->Text=ListView4->Selected->Caption; } if (PopupMenuZW->PopupComponent->Name=="ListView5") { if(ListView5->Selected == NULL) return; Edit4->Text=ListView5->Selected->Caption; } }
示例4
獲取ListView中某行某列的值,可以
ListView1->Items->Item[i]->Caption = "第i行第1列的值"; ListView1->Items->Item[i]->SubItems->Strings[0]= "第i行第2列的值"; ListView1->Items->Item[i]->SubItems->Strings[1]= "第i行第3列的值"; ListView1->Items->Item[i]->SubItems->Strings[2]= "第i行第4列的值";
已知某項的值,修改對應的圖標,目前想到的方法是
for(int i=0;i<=ListView1->Items->Count-1;i++) { if(ListView1->Items->Item[i]->Caption=="5排8座")
{ ListView1->Items->Item[i]->ImageIndex=1; break; } }
其他搜集到的示例:
轉自C++Builder 移動開發研究 (92227302) QQ群 讓ListView控件失去焦點后仍保持選中狀態: void __fastcall TForm1::FormCreate(TObject *Sender) { ListView1->HideSelection = false; // 失去焦點后仍保持選中狀態 //suiTreeView1->HideSelection = false; // 失去焦點后仍保持選中狀態 } 對suiDBMemo控件等同樣適用,但ListBox控件沒有這樣的屬性。 使用圖標: //ListView1->LargeImages=ImageList1; // 設置存放大圖標的圖標列表,當列表處於大圖標顯示方式時,列表使用這個圖標列表中的圖標顯示 ListView1->SmallImages=ImageList1; // 設置存放小圖標的圖標列表,當列表處於小圖標顯示方式時,列表使用這個圖標列表中的圖標顯示 //ListView1->StateImages=ImageList1; // 狀態圖標,用不同的的圖像來表示節點的不同狀態 // 如果要在不是當前被選中的節點的左邊顯示圖像,那么應該在編輯框Image Index // 中輸入圖像的索引號。要禁止顯示圖像可以把這個節點Image Index設置為缺省值-1。 // 如果要在被選中的節點的左邊顯示圖像,應該在編輯框Selected Index中指定圖像的索引號,索引號是從0開始的。要禁止顯示圖像可以把它設置為缺省值-1。 // 如果要在節點的左邊多顯示一個圖像,可以在編輯框State Index中輸入圖像的索引號。這個索引號代表Tree View組件中的StateImages屬性所表示的圖像列表的索引。要禁止顯示圖像可以把這個項目設置為缺省值-1。 // 注意:Image Index與 Selected Index 使用的是Images指定的ImageList;而State Index使用的是StateImages 指定的ImageList。 ListView1->Items->Item[ListView1->Items->Count-1]->ImageIndex=0; // 最后一個節點是使用第一個圖標 1、常用代碼: void __fastcall TForm1::Button2Click(TObject *Sender) { //Edit1->Text=ListView1->SelCount; // 取得被選中的節點總數 //ListView1->Selected->ImageIndex=1; // 改變選中節點圖標 //if(suiListView1->Items->Count<=0) // 判斷是否為空 //Edit1->Text=ListView1->Items->Count; // 取得節點總數 //ListView2->Items=ListView1->Items; // 在兩個ListView控件之間傳遞結構 //ListView1->Items->Item[a] = ListView1->Items->Item[a+1]; // 在兩個節點之間傳遞結構 //ListView1->Items->Add()->Caption = Edit1->Text; // 增加新節點 //ListView1->Items->Add()->SubItems->Add("str"); // 僅增加子節點 //ListView1->RowSelect = true; // 允許整行選中 //ListView1->Clear(); // 清空 或: //ListView1->Items->Clear(); // 清空 //Edit1->Text=ListView1->Items->Count; // 取得總行數 //Edit1->Text=ListView1->Selected->Index; // 取得選中行的行號 或: //Edit1->Text=ListView1->Items->IndexOf(ListView1->Selected); // 取得選中行的行號 //Edit1->Text=ListView1->Selected->SubItems->Text; // 取得選中節點下所有子節點的文本,不同節點的文本之間用空格分開 //ListView1->Selected->Delete(); // 從ListView視圖中將選中的條目刪除 //Memo1->Lines->Add(ListView1->Items->Item[0]->Top); // 指定節點的豎向位置 //Memo1->Lines->Add(ListView1->Items->Item[0]->Left); // 指定節點的橫向位置 //Edit1->Text=ListView1->Items->Item[0]->Caption; // 取得指定節點的文本 //Edit1->Text=ListView1->Selected->Caption; // 取得選中節點的文本 或: //Edit1->Text=ListView1->Items->Item[ListView1->Selected->Index]->Caption; // 取得選中節點的文本 //ListView1->Items->Item[0]->Caption = "sdfs"; // 修改第一層指定節點的文本 //ListView1->Items->Item[ListView1->Items->Count-1]->SubItems->Add("buffer"); // 在最后一行追加子節點 //ListView1->Items->Item[0]->SubItems->Add("buffer"); // 在指定行追加子節點 // 本教程由 C++Builder 移動開發研究 (92227302) QQ群原創並群內首發,轉載請注明出處,謝謝合作! ListView1->ViewStyle=vsIcon; // 大圖標 //ListView1->ViewStyle=vsSmallIcon; // 小圖標 //ListView1->ViewStyle=vsList; // 列表 //ListView1->ViewStyle=vsReport; // 詳細資料 } 3、追加節點: void __fastcall TForm1::Button1Click(TObject *Sender) { //ListView1->Clear(); // 清空 ListView1->Items->Add(); // 增加節點 ListView1->Items->Item[ListView1->Items->Count-1]->Caption = "hniuh"; // 給節點命名 } 4、當鼠標指針移動到節點上時,改變字體顏色為藍色: void __fastcall TForm1::FormCreate(TObject *Sender) { ListView1->HotTrack=true; // 當鼠標指針移動到節點上時,改變字體顏色為藍色 } 或: void __fastcall TForm1::ListView1CustomDrawItem(TCustomListView *Sender, TListItem *Item, TCustomDrawState State, bool &DefaultDraw) { ListView1->HotTrack=true; // 當鼠標指針移動到節點上時,改變字體顏色為藍色 } 5、禁止用戶通過鼠標直接點擊的方法修改節點的文本: void __fastcall TForm1::FormCreate(TObject *Sender) { ListView1->ReadOnly=true; // 禁止用戶直接修改節點的標題 } 6、設置指定的行處於選中狀態: void __fastcall TForm1::Button2Click(TObject *Sender) { ListView1->Items->Item[0]->Selected=True; // 設置指定的行處於選中狀態,行號從0開始 ListView1->SetFocus(); // 獲得焦點 或: //ListView1->Selected = ListView1->Items->Item[0]; //ListView1->SetFocus(); // 獲得焦點 } 7、在控件中增加新列並顯示列圖標: void __fastcall TForm1::Button2Click(TObject *Sender) { TListColumn* NewColumn; // 列表列 //ListView1->RowSelect = true; // 准許可以整行選中 NewColumn = ListView1->Columns->Add(); // 增加一列 NewColumn->Caption = "自轉周期(天)"; // 列的名稱 NewColumn->Width = 100; // 列的寬度 NewColumn->ImageIndex=1; // 列頭圖標號 //delete NewColumn; // 使用后添加的項目被清除 } 8、添加新節點並指定節點圖標: void __fastcall TForm1::Button2Click(TObject *Sender) { //ListView1->Clear(); // 清空 TListItem * pList = ListView1->Items->Add(); pList->Caption = Edit1->Text; pList->SubItems->Add("str"); pList->ImageIndex=1; //delete pList; // 使用后添加的項目被清除 } 9、更換選中節點的圖標: void __fastcall TForm1::Button1Click(TObject *Sender) { if(ListView1->SelCount>0) // 如果有節點被選中 ListView1->Selected->ImageIndex=1; // 更換選中節點的圖標 } 10、更換所有節點的圖標: void __fastcall TForm1::Button1Click(TObject *Sender) { for(int i=0;i<=ListView1->Items->Count-1;i++){ ListView1->Items->Item[i]->ImageIndex=1; } }