2010-06-15 2 views
0

TableLayoutPanel이 포함 된 패널이 있는데 그 자체에 ListViewsLabels이 포함되어 있습니다.ListView 항목의 크기가 항상 모든 행을 표시하도록 조정되는지 확인하려면 어떻게합니까?

각 목록보기의 내용을 세로로 (즉 모든 행을 볼 수 있도록) 모든 내용에 맞게 크기를 조정할 수 있습니다. TableLayoutPanel은 수직 스크롤링을 처리해야하지만 행 수에 따라 ListView의 크기를 조정하는 방법을 알아낼 수 없습니다.

OnResize을 처리해야하며 수동으로 크기를 조정해야합니까, 아니면 이미 처리 할 항목이 있습니까?

+0

사용하는보기를 문서화해야합니다. 열 헤더 높이를 얻기가 어렵습니다. –

+0

나는 잘 모르겠다. 당신은 정교 할 수 있습니까? –

답변

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; 
    } 


};