Reputation: 2495
I have a couple of user controls, which I show in my main form.
So let's say here's what i want:
Sub Show_Control1()
UserControl1.Show
'CODE TO HIDE EVERY OTHER DISPLAYED USER CONTROL ON THE MAIN FORM
End sub
right now I have to hide them one by one with hide, because I don't know what's the current displayed form.
Upvotes: 0
Views: 5762
Reputation: 3635
I tried your situation on a Windows Forms application (.NET 4) and the following worked. I'm not sure why your way of using GetType(T1) Is GetType(T2)
isn't working (maybe it has different semantics, like it doesn't consider inheritance) but you can use this instead:
Sub Show1()
For Each ctrl As Control In Controls
If TypeOf ctrl Is UserControl Then
ctrl.Hide()
End If
Next
UserControl11.Show()
End Sub
Update:
I checked the MSDN to see why is your code not working. Firstly the TypeOf .. Is
operator is the one used to check for whether a type of an object is compatible with another. Here's what they MSDN article at (Link: TypeOf keyword) says:
The TypeOf keyword introduces a comparison clause that tests whether an object is derived from or implements a particular type, such as an interface.
However you're using the Is keyword (very different from that used in C# to check whether an object is of some particular type). The "Is" keyword is used to check whether two references reference the same object. Here's what MSDN at (Link: Is keyword) says:
[Is] Compares two object reference variables.
So I was right: you're using an operator with different semantics than your intentions. I rarely write VB .NET code these days. Nice question.
Upvotes: 1
Reputation:
Give this a try:
Sub Show_Control1()
For Each cont In Me.Controls
cont.Hide
Next cont
UserControl1.Show
End sub
This will basically hide all controls, and then show just the one you want. Faster and simpler than checking on each control if it's not the one you want to stay visible.
Upvotes: 0