2010-06-15 34 views
0

我有一個面板,其中包含TableLayoutPanel其本身包含ListViewsLabels的數量。如何確保ListView項總是調整大小以顯示所有行?

我想爲每個列表視圖調整大小以適應垂直方向上的所有內容(即每行都可見)。 TableLayoutPanel應該處理任何垂直滾動,但我不能解決如何讓ListView根據行數調整自身的大小。

我是否需要處理OnResize並手動調整大小或者是否有處理此問題的內容?

+0

您需要記錄您使用的視圖。列標題高度很難獲得。 – 2010-06-15 17:18:31

+0

我不知道我關注。你能詳細說明嗎? – 2010-06-15 18:31:22

回答

0

一個類似的問題建議使用ObjectList,但它看起來像我想要的矯枉過正。相反,我做了這個簡單的重載(在下面)根據列表中的項目調整大小。

我只在Windows Vista上測試了這個細節模式的顯示,但它很簡單,看起來很好。

#pragma once 

/// <summary> 
/// A ListView based control which adds a method to resize itself to show all 
/// items inside it. 
/// </summary> 
public ref class ResizingListView : 
public System::Windows::Forms::ListView 
{ 
public: 
    /// <summary> 
    /// Constructs a ResizingListView 
    /// </summary> 
    ResizingListView(void); 

    /// <summary> 
    /// Works out the height of the header and all the items within the control 
    /// and resizes itself so that all items are shown. 
    /// </summary> 
    void ResizeToItems(void) 
    { 
     // Work out the height of the header 
     int headerHeight = 0; 
     int itemsHeight = 0; 
     if(this->Items->Count == 0) 
     { 
      // If no items exist, add one so we can use it to work out 
      this->Items->Add(""); 
      headerHeight = GetHeaderSize(); 
      this->Items->Clear(); 

      itemsHeight = 0; 
     } 
     else 
     { 
      headerHeight = GetHeaderSize(); 
      itemsHeight = this->Items->Count*this->Items[0]->Bounds.Height; 
     } 

     // Work out the overall height and resize to it 
     System::Drawing::Size sz = this->Size; 
     int borderSize = 0; 
     if(this->BorderStyle != System::Windows::Forms::BorderStyle::None) 
     { 
      borderSize = 2; 
     } 
     sz.Height = headerHeight+itemsHeight+borderSize; 
     this->Size = sz; 
    } 

protected: 
    /// <summary> 
    /// Grabs the top of the first item in the list to work out how tall the 
    /// header is. Note: There _must_ at least one item in the list or an 
    /// exception will be thrown 
    /// </summary> 
    /// <returns>The height of the header</returns> 
    int GetHeaderSize(void) 
    { 
     return Items[0]->Bounds.Top; 
    } 


}; 
相關問題