如何按名称访问自定义组件属性? [英] How to access to a custom component properties by name?
问题描述
我有一个自定义组件,里面有几个复选框和一个文本框;还有一个属性/变量,我称之为秒"来存储时间.
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.
此自定义控件在运行时多次添加到位于(嵌套)另外两个 TabPage 内的 TabPage.
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.
定时器Ctrl1
定时器Ctrl2
定时器Ctrl3
等等...
现在我想设置这些复选框、文本框内的文本和按名称的变量 seconds 以从具有每个控件的名称和属性的文件中加载配置文件.
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.
我尝试这样做:
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?
推荐答案
TimerIndex
将是一个控件数组 (Control()
).如果您想访问该属性,您需要将您访问的对象强制转换为您的特定控件.
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)
例如:
DirectCast(TimerIndex(0), TimerUserControl).seconds = Values(1)
- 这里,TimerUserControl
是我的自定义用户控件的通用名称.
- Here, TimerUserControl
is the generic name of my custom user control.
在 MSDN 文档.
Read more about DirectCast
on the MSDN documentation.
这篇关于如何按名称访问自定义组件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!