如何通过名称访问自定义组件属性?

How to access to a custom component properties by name?

我有一个自定义组件,里面有几个复选框和一个文本框;还有一个 property/variable,我称之为 "seconds" 来存储时间。

此自定义控件在 运行 次被多次添加到位于(嵌套)另外两个 TabPage 中的 TabPage。

这些控件中的每一个都有在 运行 时间创建时指定的顺序名称。

TimerCtrl1

TimerCtrl2

TimerCtrl3

等...

现在我想设置这些复选框、文本框内的文本和按名称命名的变量秒数,以从具有每个控件的名称和属性的文件中加载配置文件。

我可以使用以下代码更改在设计时创建的文本框等其他控件

Dim TxtIndex = ProgTab.Controls.Find(Values(0), True)
If TxtIndex.Length > 0 Then
   TxtIndex(0).Text = Values(1) 'Value to TextBox
End If

但是我无法以同样的方式访问我自己的自定义控件的属性。

我尝试做:

 Dim TimerIndex = ProgTab.Controls.Find(Values(0), True)
 If TimerIndex.Length > 0 Then
    TimerIndex(0).seconds = Values(1) 'Syntax ERROR
 End If

知道如何解决这个问题吗?

TimerIndex 将是一个控件数组 (Control())。如果您想访问 属性.

,您需要将您访问的对象强制转换为您的特定控件
DirectCast(TimerIndex(0), <user control type name here>).seconds = Values(1)

例如:

DirectCast(TimerIndex(0), TimerUserControl).seconds = Values(1)

-这里,TimerUserControl是我的自定义用户控件的通用名称。

MSDN documentation 上阅读有关 DirectCast 的更多信息。