I have created a user control that has a public function in it called
Getstatus()
I am loading 3 of these user controls dynamically upon starting my application. How do i access each controls GetStatus function individually?
Printable View
I have created a user control that has a public function in it called
Getstatus()
I am loading 3 of these user controls dynamically upon starting my application. How do i access each controls GetStatus function individually?
vb Code:
dim uc1 as new usercontrol me.controls.add(uc1) uc1.getstatus
Like any object, to get access to your user control's you need a variable that references them. When you add a control to your form at design time the IDE generates a member variable for you and it assigns that control to that variable when the form is initialised. If you're adding the controls yourself in code then this doesn't happen, so it's up to you to declare your own variable.
The code that .paul. provided is only so much use because it only allows you to access the control within the method it was created. If you want to access the control outside that method then you need a variable that exists outside that method, i.e. a member variable. If you know exactly how many controls there will be then you could declare individual variables for each of them, or you might choose to have a single array. If you don't know how many controls there will be then you'd be best to use a collection, to which you can add as many or as few instances as you like.
That said, if you know for a fact that there will be three instances of this control then I'd question why you're adding them in code anyway. There may be a legitimate reason but I'd be interested to hear it.
Your correct i do not knowhow many there will be added at once the number may be different each time.i have tried this. but there is one problem with it. It works with all objects that are part of the >NET library but it wont allow me to use the GetStatus function in my usercontrol. Here is my code..Quote:
Originally Posted by jmcilhinney
The problem is when i call this:Code:
Dim ControlShowing As Control
Dim idx As Integer
idx = 0
For Each ControlShowing In Me.Controls
idx = idx + 1
If TypeOf ControlShowing Is MyUserControl Then
If ControlShowing.Name = "Exx" & idx Then
ControlShowing.Getstatus()
End If
End If
Next ControlShowing
ControlShowing.Getstatus()
It says is not a member of System.Windows.Forms.Controls
Any other input?
Exactly as the error message says. GetStatus is a member of YOUR class, not of the Control class. If you want to access members of YOUR class then you have to use a reference of YOUR type, not Control. In the case of the code you're using there that means casting the Control reference as your type:That said, I really recommend not looping needlessly through every control on the form and casting when you can do what I suggested earlier and just added each instance you create to a List that is already typed to your class.vb.net Code:
DirectCast(ControlShowing, MyUserControl).Getstatus()You can then just loop through that List and you know that you'll only be referencing the controls of your type and there will be no need to cast.vb.net Code:
Private myUserControls As New List(Of MyUserControl)
Wow Thank you very much!! I am new to .NET and so much for me to learn. Where did you learn what you know?Quote:
Originally Posted by jmcilhinney
I do have a Computer Science degree, so all the fundamentals were there to begin with. that said, I did teach myself .NET programming from scratch using the MSDN Library as my primary resource.Quote:
Originally Posted by Halisco
Quote:
Originally Posted by jmcilhinney
Wish i had that knowledge. You must have a prosperous and wealthy career.
Thanks again!