如何让Windows :: Forms :: GroupBox调整大小?

时间:2010-10-14 13:37:44

标签: .net winforms c++-cli resize

我有Windos::Forms::GroupBox,其中包含Windows::Forms::ListView。当我添加项目时,我会将项目添加到ListView我告诉它调整大小但是如何为其父GroupBox执行相同操作?

[edit] 这是我的自定义大小调整列表控件:

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

所以当我完成添加项目时,我会调用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);

2 个答案:

答案 0 :(得分:2)

为GroupBox

设置AutoSizetrue 祝你好运。

答案 1 :(得分:1)

当您将GroupBox的AutoSize属性设置为True时,它会自动生成,它会根据需要增长以适合ListView。这在大多数典型布局中并不常见,因为这会使其易于与其他控件重叠或超出表单边缘。