问题
I have a custom component with several checkboxes and a textbox inside; also have a property/variable that I called "seconds" to store the time.
This customized control is added several times to a TabPage that is inside(nested) another two TabPages at running time.
Every one of this controls have sequential names that are assigned when their are created at running time.
TimerCtrl1
TimerCtrl2
TimerCtrl3
etc...
Now I want to set those checkboxes, the text inside the textbox and the variable seconds by name to load a profile from a file that have the name and properties of every control.
I can change another controls like textboxes that was created at design time with the following code
Dim TxtIndex = ProgTab.Controls.Find(Values(0), True)
If TxtIndex.Length > 0 Then
TxtIndex(0).Text = Values(1) 'Value to TextBox
End If
But I'm not able to access to the properties of my own customized control in the same way.
I try to do:
Dim TimerIndex = ProgTab.Controls.Find(Values(0), True)
If TimerIndex.Length > 0 Then
TimerIndex(0).seconds = Values(1) 'Syntax ERROR
End If
Any idea about how to solve this?
回答1:
TimerIndex
will be an array of controls (Control()
). You need to cast the one(s) you access to your specific control if you want to access the property.
DirectCast(TimerIndex(0), <user control type name here>).seconds = Values(1)
For example:
DirectCast(TimerIndex(0), TimerUserControl).seconds = Values(1)
- Here, TimerUserControl
is the generic name of my custom user control.
Read more about DirectCast
on the MSDN documentation.
来源:https://stackoverflow.com/questions/44287766/how-to-access-to-a-custom-component-properties-by-name