2010-10-14 25 views
2

我有一個Windos::Forms::GroupBox它包含一個Windows::Forms::ListView。當我添加項目時,我將項目添加到ListView我告訴它調整大小,但我如何爲它的父項GroupBox做同樣的事情?如何讓Windows :: Forms :: GroupBox調整大小?

[編輯]這是我的自定義調整大小列表控件:

ResizingListView::ResizingListView(void) 
{ 

} 

void ResizingListView::ResizeVerticallyToItems(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 work out how big the header is 
     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; 
} 

int ResizingListView::GetHeaderSize(void) 
{ 
    return Items[0]->Bounds.Top; 
} 

void ResizingListView::OnResize(System::EventArgs^ e) 
{ 
    if(this->Scrollable == false) 
    { 
     ResizeVerticallyToItems(); 
    } 
} 

所以,當我完成aadding項目我打電話ResizeVerticallyToItems()其調整大小沒有任何問題的控制。父GroupBox有一些填充,但當我的列表調整大小時會消失。所以我的想法是,我需要問父母GroupBox調整。

這是組框的初始化:

this->grpMyStatus->AutoSize = true; 
this->grpMyStatus->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(64)), 
    static_cast<System::Int32>(static_cast<System::Byte>(64)), static_cast<System::Int32>(static_cast<System::Byte>(64))); 
this->grpMyStatus->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Stretch; 
this->grpMyStatus->Controls->Add(this->lstMyStatus); 
this->grpMyStatus->Dock = System::Windows::Forms::DockStyle::Top; 
this->grpMyStatus->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, 
    System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); 
this->grpMyStatus->ForeColor = System::Drawing::Color::SkyBlue; 
this->grpMyStatus->Location = System::Drawing::Point(3, 166); 
this->grpMyStatus->Name = L"grpMyStatus"; 
this->grpMyStatus->Padding = System::Windows::Forms::Padding(3, 3, 3, 20); 
this->grpMyStatus->Size = System::Drawing::Size(270, 92); 
this->grpMyStatus->TabIndex = 5; 
this->grpMyStatus->TabStop = false; 
this->grpMyStatus->Text = L"My Status"; 

...這是孩子列表的初始化:

this->lstMyStatus->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(64)), 
    static_cast<System::Int32>(static_cast<System::Byte>(64)), static_cast<System::Int32>(static_cast<System::Byte>(64))); 
this->lstMyStatus->BorderStyle = System::Windows::Forms::BorderStyle::None; 
this->lstMyStatus->Columns->AddRange(gcnew cli::array<System::Windows::Forms::ColumnHeader^>(1) {this->columnHeader6}); 
this->lstMyStatus->Dock = System::Windows::Forms::DockStyle::Top; 
this->lstMyStatus->Font = (gcnew System::Drawing::Font(L"Verdana", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
    static_cast<System::Byte>(0))); 
this->lstMyStatus->ForeColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(224)), 
    static_cast<System::Int32>(static_cast<System::Byte>(224)), static_cast<System::Int32>(static_cast<System::Byte>(224))); 
this->lstMyStatus->HeaderStyle = System::Windows::Forms::ColumnHeaderStyle::None; 
this->lstMyStatus->HideSelection = false; 
this->lstMyStatus->Items->AddRange(gcnew cli::array<System::Windows::Forms::ListViewItem^>(3) {listViewItem3, 
    listViewItem4, listViewItem21}); 
this->lstMyStatus->LabelWrap = false; 
this->lstMyStatus->Location = System::Drawing::Point(3, 18); 
this->lstMyStatus->Name = L"lstMyStatus"; 
this->lstMyStatus->RightToLeft = System::Windows::Forms::RightToLeft::Yes; 
this->lstMyStatus->RightToLeftLayout = true; 
this->lstMyStatus->Scrollable = false; 
this->lstMyStatus->ShowGroups = false; 
this->lstMyStatus->ShowItemToolTips = true; 
this->lstMyStatus->Size = System::Drawing::Size(264, 54); 
this->lstMyStatus->SmallImageList = this->imgLights; 
this->lstMyStatus->TabIndex = 18; 
this->lstMyStatus->UseCompatibleStateImageBehavior = false; 
this->lstMyStatus->View = System::Windows::Forms::View::Details; 
this->lstMyStatus->SelectedIndexChanged += gcnew System::EventHandler(this, &Status::lstMyStatus_SelectedIndexChanged); 
+1

你可以發佈代碼示例嗎?組件有什麼調整策略(GrowAndShrink等),什麼是佈局,你如何告訴它調整大小?你打電話給PerformLayout()嗎? – Stefan 2010-10-14 13:41:48

+0

PerformLayout(在組框)得到它:)如果你添加作爲答案我會接受它。 – 2010-10-14 14:24:43

回答

1

當您將GroupBox的AutoSize屬性設置爲True時,它會自動進行,它會根據需要增長以適應ListView。這在大多數典型佈局中並不常見,因爲這會使其容易與其他控件重疊或超出表單邊緣。

+0

我們有一些來自某些硬件的狀態信息,並且取決於插入了哪些額外的設備,會有更多或更少的信息可用。因此需要調整大小的佈局。 – 2010-10-14 14:02:01

+0

那麼,不要低估一個滾動條:)但是聽起來我還需要使表單自動調整大小。 – 2010-10-14 14:08:34

2

設置AutoSizetrue爲分組框中

好運。

+0

我已經完成了 – 2010-10-14 14:00:33