Remove not hide User Control, posting again

M

Mike TI

March 24, 2006

Hi all

I am new to VB.NET and am using VB.NET 2005.

I have an MDI form with a Split Container Control. On demand I am adding and
removing User Controls on Panel 2. I am using Show() and Hide(). It works
perfectly. The only thing is I would like to remove the User Control from
memory instead of hiding. I tried Dispose(), however then I cannot show this
user control again.

UserControl.Parent=Form.SplitContainer1.Panel2
UserControl.Dock=DockStyle.Fill
UserControl.Show()
..
..
UserControl.Hide()

Thanks.
 
A

Armin Zingler

Mike TI said:
March 24, 2006

Hi all

I am new to VB.NET and am using VB.NET 2005.

I have an MDI form with a Split Container Control. On demand I am
adding and removing User Controls on Panel 2. I am using Show() and
Hide(). It works perfectly. The only thing is I would like to remove
the User Control from memory instead of hiding. I tried Dispose(),
however then I cannot show this user control again.

This is a contradiction: Remove from memory .... show again.

If you removed something from memory, you can not show it again because it
does not exist anymore.
UserControl.Parent=Form.SplitContainer1.Panel2
UserControl.Dock=DockStyle.Fill
UserControl.Show()
.
.
UserControl.Hide()

Thanks.


Armin
 
M

Mike TI

I mean is it possible for me to show the user control again and again on
demand with the same behaviour as it was displayed for first time (firing
load event etc.) instead of using Hide() which probably is the same thing as
Visible=False.

Thank you.
 
S

Stephany Young

Of course you can. That's the whole rationale behind instantiating objects.

Just create an new instance of the component, set all the relevant
properties as required and hook up the event handlers as required and Bob's
your uncle.
 
C

Cor Ligthert [MVP]

Mike,

Of course you only have to instance it from its class and add it to your
form again.

Dim myControl as New myUsercontrol.
me.controls.add(myControl)

And set than the right properties again.

I hope this helps,

Cor
 
A

Armin Zingler

Mike TI said:
I mean is it possible for me to show the user control again and
again on demand with the same behaviour as it was displayed for
first time (firing load event etc.) instead of using Hide() which
probably is the same thing as Visible=False.



If you do not want to use Hide, you have to remove it from the container
control, call Dispose and remove remaining references. Whenever you need a
control again, you can create it, set it's properites and add it to the
container (TheContainer.Controls.Add). If you have a variable declared using
"WithEvents" that was previously pointing to the former control, you can
assign the new control to this variable again and all event procedures will
be working just like with the former control. If you don't have a WithEvents
variable, use the AddHandler statement to attach event handlers to the new
control.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top