switch between usercontrols

  • Thread starter Thread starter Johannes Hammersen
  • Start date Start date
J

Johannes Hammersen

Hi everyone,

I have a windows forms application with several full screen Usercontrolls.
Now I would like to be able to switsch between them. So that when you click
a button UC1 will become invissible an UC2 will become vissible.

Doing this from my Form that contains the UCs works fine. I have a Sub that
makes all UC invissible and then the needed UC Vissible again. That way I
can call any usercontrol from my Form. But how can i do the same from one of
the UC? So that when you push a button on UC1, UC1 will become Invissible
and UC2 will become vissible?

If possible I would like to put most of the needed code in my Form, or even
better a sepperate class, so that I don't have to add a lot to everysingle
UC.

Thank you for any help

Johannes
 
Cast the UserControl's Parent property to the type of form it's put on:

Inside the UserControl:

Public Sub DoSwitch()
If (Not Me.Parent Is Nothing) AndAlso (TypeOf Me.Parent Is
TheParentForm) Then
DirectCast(Me.Parent, TheParentForm).CallParentMethod
End If
End Sub

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
You might want to try raising an even, ...
...Now in your form that contains the user control, create a handler for
the event.

First of all thank you for your help!

I have a lot of usercontrols with a quite a few buttons so using your way I
would have to bulid a lot of eventhandler. Even if I would combine a lot of
them, I'd still would have to add the Event to the Handles list for every
button in every UC. That would make one long confusing list.

Johannes
 
Hi Jay,
Now the question becomes, does a single button on each UC take you to the
next UC? (like Prev & Next in a Wizard).
Or does each UC have a button for every other UC? (I have 15 UC, so I have
15 buttons).
Kind of like that.
Every UC has a few butons to a few other UCs which have buttons to other
UCs.
So at the end you will have to "navigate" through a few UCs to get to a
certian one.
But on the other hand every UC will have a button to take you back to the
first UC after executing some code.

....
Protected Sub ShowUC(uc As UCBase)
' hide previous uc
' show new uc
End Sub
....

I think I understood your sample. So I'll try to build it into my project.
But I don't see how I could change the UC.visible Property form UCBase,
doesn't that need to be done in the form?

I tried somesthing like this before:
Private _myBaseForm as Form1
_myBaseForm = Me.ParentForm
_myBaseForm.HideAllUserControls()
_myBaseForm.UserControls(2).Visible = True

Which semed to work, execpt that I had to remove the "as Form1" because I
kept getting an message box saying: "Cast not valid" when I tried to add the
uc to the form.
Why can't I dim _myBaseForm as Form1?

Thanks for your help
Johannes
 
Back
Top