如何要求 Windows::Forms::GroupBox 调整大小?

发布于 2024-09-27 19:09:29 字数 5121 浏览 3 评论 0原文

我有一个 Windos::Forms::GroupBox,其中包含 Windows::Forms::ListView。当我添加项目时,我将项目添加到 ListView 我告诉它调整大小,但如何对其父 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();
    }
}

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

I have a Windos::Forms::GroupBox which contains a Windows::Forms::ListView. When I add items, I add items to the ListView I tell it to resize but how do I do the same for it's parent GroupBox?

[edit] This is my custom resizing list control:

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

So when I finish aadding items I call ResizeVerticallyToItems() which resizes the control without any problems. The parent GroupBox has some padding though which disappears when my list resizes. So my thoughts were that I needed to ask the parent GroupBox to resize.

This is the group box's initialisation:

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

...and this is the child list's initialisation:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给妤﹃绝世温柔 2024-10-04 19:09:29

将 GroupBox 的 AutoSize 设置为 true

祝你好运。

Set AutoSize to true for the GroupBox

Good luck.

亽野灬性zι浪 2024-10-04 19:09:29

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

It's automatic when you set the GroupBox' AutoSize property to True, it will grow as needed to fit the ListView. This isn't very common in most typical layouts since that will makes it liable to overlap some other control or grow beyond the form edges.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文