1

I am trying to make a collapsible panel in win forms. I can decrease the height to make it looks like animated collapsing however I could not expand it as the original design height is lost after the collapsing.

while (panel1.Height > label1.Height) 
    {
        panel1.Height--;
    }

The above code animate the panel collapsing. But how do I expand it back as the panel1.Height now equals to label1.Height? How do I get the design stage panel1.Height? Is there a method to get the design size of the panel?

I do not wish to put the original height during collapsing into a stored variable as I could have hundreds of the panel. Also I do not wish to hard code the height as all panel during design will have different heights.

Bridge
  • 29,818
  • 9
  • 60
  • 82
ishtarsg
  • 133
  • 10
  • Please put a code sample of what you have tried so far – Nick Aug 01 '17 at 01:10
  • Hi Nick Code added – ishtarsg Aug 01 '17 at 01:51
  • You can use Tag property of Label and set to initial height on form_load. Another approach would be to create new control inheriting from Panel and have new property InitialHeight. Use the control and set that property value in form_load and use it to increase the panel height. – Chetan Aug 01 '17 at 02:14
  • 'original height during collapsing into a stored variable as i could have hundreds of the panel' - Then you should write this as a derived control that contains all the necessary logic and state variables. Define a new Class that inherits from Panel. – TnTinMn Aug 01 '17 at 02:15
  • Have a dictionary of string key and int value. Store initial height of panels in the dictionary with panelid as key. Use the dictionary to get the initial height when expanding the panel. – Chetan Aug 01 '17 at 02:17
  • Hi all thanks for the reply, however i do not wish to create another control nor store the values in runtime. This is because the project can be edited by anyone. This is also to reduce the need for new programmers reading and understanding and hence avoiding confusion.I wish to keep it as simple as possible. – ishtarsg Aug 01 '17 at 02:56
  • storing initial heights in dictionary is simple and easy to understand. Every project is contributed by more than one programmers and there is a understanding between them about how to write the best code. They understand each other's code very well as long as their logic is clear. If logic is not clear then they can't read even their own code. Just for not to confuse other developers you can not have something which not there at all. – Chetan Aug 01 '17 at 03:31
  • that wouldn't be good practice, but you can store any values in `Tag` property of your panel (and other visual components as well) – Anton Semenov Aug 01 '17 at 07:34

3 Answers3

1

You could create a custom and add a property like DefaultHeight which you could fill with the current height.

Something like this:

public class CustomPanel : Panel
{
    public int DefaultHeight { get; private set; }

    public CustomPanel()
    {
        // Add an event, which gets triggered at the next resize.
        // We need this event, because at initializing the Control have the default Height.
        // The Resize event getting triggered, when the Form load and initializes the Controls.
        this.Resize += this.Initial_Resize;
    }

    private void Initial_Resize(object sender, EventArgs e)
    {
        // Set the DefaultHeight to the value of the new Size
        this.DefaultHeight = this.Height;
        // Remove the event, otherwise DefaultHeight would get overridden at every resize.
        this.Resize -= this.Initial_Resize;
    }
}

With this code you can use the new CustomPanel in your Form and the DefaultHeight property to expand the control after collapsing.

There may be better ways, but this would help you without further effort. Any suggestion is welcome.

Nik Bo
  • 1,410
  • 2
  • 17
  • 29
0

You must store panel1.Height somewhere in your class (field or property) before collapse panel. Design size is it only initial field value of your class and you can't get them in runtime.

More detail about initial field value you may read Get default value of class member or How can I get the default value of a field in a class in C#?

If you have many panel control then use Dictionary<Control, int> to store height before collapse per panel.

In addition, this solution will allow you to restore the panel to the position that was before collapse if users change the size of the panel in the application, and not only in design time.

Dmitry
  • 512
  • 7
  • 15
0

Thanks for all the suggestion. I guess the best way is actually to create another control which Nik Bo and Chetan Ranpariya suggested. As this project could be pass around and edit by other programmer, new programmer may mistook this new control as a standard panel and use a standard panel on other parts of the project. This is also the reason why I didn't like the idea of a new control if in later stage the information are not passed down.

Currently I am using a panel within a panel that will determine the boundary which control the autosize effect. While the collapsing function is still the same, I expand the panel by using autosize = true. Below are the code.

For collapsing:

while (panel1.Height > label1.Height) 
{
    panel1.AutoSize = false;
    panel1.Height--;
}

For expanding:

panel1.AutoSize = true;
Nik Bo
  • 1,410
  • 2
  • 17
  • 29
ishtarsg
  • 133
  • 10