Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7880075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:03:27+00:00 2026-06-03T04:03:27+00:00

I am trying to create a simple User Control (not WPF) in VS2008 which

  • 0

I am trying to create a simple User Control (not WPF) in VS2008 which effectively is a SplitContainer with a button in Panel1, which when pressed, toggles the Panel2Collapsed property and resizes the control to the size of Panel1.

Here are the bare bones of the control:

private int _openHeight;
private int _closedHeight;

public MyUserControl(bool open)
{
  InitializeComponent();

  _openHeight = this.Height;
  _closedHeight = splitContainer1.SplitterDistance;
  Open = open;  
}    

private bool _open;
private bool Open
{
  get { return _open; }
  set 
  { 
    _open = value;
    splitContainer1.Panel2Collapsed = !_open;
    this.Height = _open ? _openHeight : _closedHeight;
  }
}

private void button1_Click(object sender, EventArgs e)
{
  Open = !Open;
}

The problem is that this.Height in Runtime is the value which the control is in the User Control Designer, rather than what is set at Design-time in the main form’s designer.

Any help would be greatly appreciated.

UPDATE

Following on from Lucas’ solution, this way means that the _openHeight is only set once, resulting in the desired effect:

private int? _openHeight;
private int _closedHeight;

public MyUserControl(bool open)
{
  InitializeComponent();

  //the _closedHeight doesn't change so can be defined in the constructor
  _closedHeight = splitContainer1.SplitterDistance;

  //set value
  Open = open;  

  this.SizeChanged += new EventHandler(MyUserControl_SizeChanged);
  this.Load += new EventHandler(MyUserControl_Load);
}    

void MyUserControl_SizeChanged(object sender, EventArgs e)
{
  //this event is called BEFORE the _Load event so gets the height set in the designer
  //  and not any changes at run time (e.g. when you collapse the control)

  if (_openHeight == null)
    _openHeight = this.Height;
}

private bool _open;
private bool Open
{
  get { return _open; }
  set 
  { 
    _open = value;

    if (_open)
    {
      //sets height only if it has been initialized
      if (_openHeight != null)
        this.Height = (int)_openHeight;
    }
    else
    {
      this.Height = (int)_closedHeight;
    }
  }
}

void MyUserControl_Load(object sender, EventArgs e)
{
  //now that control is loaded, set height
  Open = Open;
}

private void button1_Click(object sender, EventArgs e)
{
  Open = !Open;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:03:29+00:00Added an answer on June 3, 2026 at 4:03 am

    You are reading the control’s Height property in it’s constructor, which means it’s probably BEFORE it shows in the form. The thing is, the size seems to be adjusted when the control needs to be show in the form. Before this, it’s a value set in the control’s designer, which you are now getting.

    The easiest way to solve the issue is to read the Height value when you’re sure that the control was already drawn in the form i.e. you can get open parameter out of the control’s constructor, add a new method that initializes Open and _closedHeight and call it in Load event of the form.

    Something like this:

    public MyUserControl()
    {
        InitializeComponent(); 
    }
    
    public AdjustControlSize(bool open)
    {
        _openHeight = this.Height;
        _closedHeight = splitContainer1.SplitterDistance;
        Open = open; 
    }
    
    //the rest of the control's code is unchanged
    ...
    

    Then call AdjustControlSize method from form’s Load event.

    Solution with eventing mechanism

    You can also use control’s own events to read the Height when appropriate. This way you don’t have to change anything in the Form‘s code.

    So, in this case, the code can look like this (I haven’t tested this though):

    public MyUserControl(bool open)
    {
        InitializeComponent();
    
        _openHeight = this.Height;
        _closedHeight = splitContainer1.SplitterDistance;
        Open = open;
        this.SizeChanged += new EventHandler(MyUserControl_SizeChanged);
    }
    
    void CustomPictureBox_SizeChanged(object sender, EventArgs e)
    {
        _openHeight = this.Height;
        _closedHeight = splitContainer1.SplitterDistance;
    }
    

    This way the control can self adjust itself each time its size is changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a user control within a WPF application that will
I am trying to create this simple application in c#: when the user double
Im trying to create a simple input box with form validation, but im not
I'm trying to create a simple search page, but I'm not 100% sure how
Using the Windows 8 developer preview I'm trying to use a simple User Control
I am trying to create a simple program that will allow the user to
I'm trying to create a custom control in WPF to display the game tree
I'm trying to create a very simple WPF application. I'd like to have an
I'm trying to create a simple data entry application for the Mac (not iPhone).
I'm studying webrat and cucumber and trying to create simple example. Here is my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.