简体   繁体   中英

Expand/Collapse Win Form in C#

I want to create UI like this in Windows form application C#. First my form should appear like this when the chechBox is not checked.

在此处输入图片说明

And if the checkBox checked my form changes to like this

在此处输入图片说明

How can I do this?

Change the height of the form dynamically on CheckedChanged event of check box. Don't forget to set anchor of below fields or set visible on expand and invisible on collapse.

EDIT: The most simple way to achieve the results is given below.

private readonly int _collapsedHeight;
public Form1()
{
    //Set Anchor of Connect button to Right and Bottom and leave default for others
    //Optionally you need to hide controls except Connect button on collapse and vice versa.
    //Set Form Border Style to FixedSingle and MaximizeBox to false           
    InitializeComponent();
    _collapsedHeight = Height;
}

private void chkAdvancedOption_CheckedChanged(object sender, EventArgs e)
{
    //Set Y value to collapse eg. 140, adjust it as required...
    Height = chkAdvancedOption.Checked ? _collapsedHeight + 140 : _collapsedHeight;
}
void Page_Load(Object sender, EventArgs e)
  {
    // Manually register the event-handling method for the   
     // CheckedChanged event of the CheckBox control.
     checkbox1.CheckedChanged += new EventHandler(this.Check_Clicked);
  }

void Check_Clicked(Object sender, EventArgs e) 
  {
  **//This is only sample code**
    // do your code

    if (panel2.Visible) 
    {
    panel2.Visible = false;
    cmdAdvanced.Visible = true;
    }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM