How to hide and show panels on a form and change its size?

I have a form with three panels. I want the top two to be a fixed height and the bottom to fill the rest of the space. The dialog is changed, so everyone should change the width to resixze, and the bottom to height. This is important, the user should be able to stretch the form, as well as the program through the code.

If I set the panel to visible = false, I want the height of the form to decrease, so that the rest remain the same height. If I set the panel to visible = true, I want the height of the form to increase by the height of the panel.

I will control the hide / show of panels using buttons. The idea is that I show specific panels for the "advanced" mode in my form and hide them for the "simple" mode. I cannot have empty space if I hide the panel and I want the form to compress the bundle for simple mode.

I tried to do this with panels attached to the top, but resizing the form by the user will not change the height of the panel. So this is the main trick that I ask for help.

+4
source share
4 answers

, , , . , , , .

4 . - ​​ . . , .

, . / 2, .

namespace ProgTesting {
public partial class Form5 : Form {
private bool doNothing = false;

public Form5() {
  InitializeComponent();
  cmdAdvanced.Visible = false;
}

private void cmdSimple_Click(object sender, EventArgs e) {
  if (panel2.Visible) {
    panel2.Visible = false;
    doNothing = true;
    this.MinimumSize = new Size(this.Width, this.Height - panel2.Height);
    this.Height = this.Height - panel2.Height;
    doNothing = false;
    cmdSimple.Visible = false;
    cmdAdvanced.Visible = true;
  }
}

private void cmdAdvanced_Click(object sender, EventArgs e) {
  if (!panel2.Visible) {
    panel2.Visible = true;
    doNothing = true;
    this.Height = this.Height + panel2.Height;
    this.MinimumSize = new Size(this.Width, this.Height);
    doNothing = false;
    cmdAdvanced.Visible = false;
    cmdSimple.Visible = true;
  }
}

private void Form5_SizeChanged(object sender, EventArgs e) {
  if (!doNothing)
    if (panel2.Visible)
      panel3.Height = this.ClientSize.Height - panel1.Height - panel2.Height - panel4.Height;
    else
      panel3.Height = this.ClientSize.Height - panel1.Height - panel4.Height;
}
}
}

, , . : Form working

+3

:

, .

, !!!

+3

- TableLayoutPanel .

, . 1. TableLayoutPanel (TLP) , 2 , 1 .
2. , 3. TLP TLP. 4. . 5. 3- ( 3) TLP .

Panel3 button_Click. , , "" , Panel3 . .

private void button1_Click(object sender, EventArgs e)
{   
    if (panel3.Visible)
    { 
        // make invisible
        panel3.Visible = false;

        // storedHeight is a private member of the Form
        storedHeight = Form1.ActiveForm.Height;
        Form1.ActiveForm.Height = minimumHeight;  // Set to predetermined minimum height 
     }
     else
     {
         // make visible
         panel3.Visible = true;
         Form1.ActiveForm.Height = storedHeight;
     }
}

, Panel3, TLP, , Panel3 . , . MinSize , , 3 .

0

, , Expander. , , .

100%, Expander , /. , Expander, , Expander .

(/) WinForm. Expander Windows Forms, , .

Update:

, , , , Expander. , , Expanders , , , , , .

, :

Toggleable Panel Form

  • , , .
  • , , btnTogglePanel.
  • , , , , pnlToggled.
  • , .

:

public partial class ToggleableExpanderForm : Form
{
    public ToggleableExpanderForm()
    {
        InitializeComponent();
        SizeChanged += (s, args) => 
        {  
            if (_LastSize.HasValue)
            {
                var diff = Size - _LastSize.Value;
                if (_LastHeight.HasValue
                    && !IgnoreNextResizeForLastHeightAdjustment)
                {
                    _LastHeight += diff.Height;
                }
            }

            _LastSize = Size;
        };
    }

    private Size? _LastSize;
    private bool IgnoreNextResizeForLastHeightAdjustment = true;
    private int? _LastHeight;

    private void btnTogglePanel_Click(object sender, EventArgs e)
    {
        var newVisibility = !pnlToggled.Visible;
        int heightAdjustment = 0;

        if (newVisibility)
        {
            if (_LastHeight.HasValue)
            {
                heightAdjustment = _LastHeight.Value;
                _LastHeight = null;
            }
        }
        else
        {
            _LastHeight = pnlToggled.Height;
            heightAdjustment = -pnlToggled.Height;
        }

        pnlToggled.Visible = newVisibility;
        IgnoreNextResizeForLastHeightAdjustment = true;
        Height += heightAdjustment;
    }
}

, , , . , , , , , SizeChanged. " MUCH ", .

, , , .

0

All Articles