Estoy intentando crear un sencillo control de usuario (no WPF) en VS2008 lo que efectivamente es un SplitContainer
con un botón en Panel1
, que cuando se pulsa, alterna la propiedad Panel2Collapsed
y cambia el tamaño de la de control para el tamaño de Panel1
.Usercontrol anchura de tiempo de ejecución y la altura
Éstos son el esqueleto de 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;
}
El problema es que this.Height
en tiempo de ejecución es el valor, que el control está en el diseñador de control del usuario, en lugar de lo que se establece en tiempo de diseño en el diseñador de la forma principal
Cualquier ayuda sería muy apreciada.
ACTUALIZACIÓN
raíz de solución de Lucas, de esta manera significa que el _openHeight sólo se establece una vez, dando como resultado el efecto deseado:
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;
}
¿Te ha funcionado la solución que sugerí? –
Acaba de probar su segunda solución y, con un poco de modificación, funciona. ¿Es una buena práctica actualizar tu respuesta con mi solución modificada? –
Simplemente haga na ** Actualizar ** en su encabezado de pregunta y actualícelo con la respuesta que tiene y alguna descripción :). –