How to access to a custom component properties by name?

ⅰ亾dé卋堺 提交于 2019-12-19 11:26:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!