通过动态构建控件名称来更新控件值 [英] Update a Controls value by dynamically building the control name
问题描述
我在一个列中有一个包含15个ComboBox的表单。被称为cbo_T_Project_01到15
每个ComboBox右边都有一个Label。名为lbl_T_Init_01至15
当用户从Combobox中选择一个值时,我需要更新与其相邻的标签文本。
在我努力不写代码的过程中,我试图构建适用标签的控件名称,因为我不知道用户会选择哪个ComboBox。
I have a form with 15 ComboBoxes in a column. Called cbo_T_Project_01 to 15
Each ComboBox has a Label to the right of it. Called lbl_T_Init_01 to 15
When the user selects a value from the Combobox I need to update the text of the Label adjacent to it.
In my endeavour not to write reams of code I am attempting to 'build' the Control name for the applicable Label because I dont know which ComboBox the user will select.
Private Sub cbo_Project_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo_T_Project_01.SelectedIndexChanged,
cbo_T_Project_02.SelectedIndexChanged,
cbo_T_Project_03.SelectedIndexChanged,
cbo_T_Project_04.SelectedIndexChanged,
cbo_T_Project_05.SelectedIndexChanged,
cbo_T_Project_06.SelectedIndexChanged,
cbo_T_Project_07.SelectedIndexChanged,
cbo_T_Project_08.SelectedIndexChanged,
cbo_T_Project_09.SelectedIndexChanged,
cbo_T_Project_10.SelectedIndexChanged,
cbo_T_Project_11.SelectedIndexChanged,
cbo_T_Project_12.SelectedIndexChanged,
cbo_T_Project_13.SelectedIndexChanged,
cbo_T_Project_14.SelectedIndexChanged,
cbo_T_Project_15.SelectedIndexChanged,
cbo_O_Project_01.SelectedIndexChanged,
cbo_O_Project_02.SelectedIndexChanged,
cbo_O_Project_03.SelectedIndexChanged,
cbo_O_Project_04.SelectedIndexChanged,
cbo_O_Project_05.SelectedIndexChanged,
cbo_O_Project_06.SelectedIndexChanged,
cbo_O_Project_07.SelectedIndexChanged,
cbo_O_Project_08.SelectedIndexChanged,
cbo_O_Project_09.SelectedIndexChanged,
cbo_O_Project_10.SelectedIndexChanged,
cbo_O_Project_11.SelectedIndexChanged,
cbo_O_Project_12.SelectedIndexChanged,
cbo_O_Project_13.SelectedIndexChanged,
cbo_O_Project_14.SelectedIndexChanged,
cbo_O_Project_15.SelectedIndexChanged,
cbo_O_Project_16.SelectedIndexChanged,
cbo_O_Project_17.SelectedIndexChanged,
cbo_O_Project_18.SelectedIndexChanged,
cbo_O_Project_19.SelectedIndexChanged,
cbo_O_Project_20.SelectedIndexChanged
Dim ProjCtrl As Control = Me.ActiveControl
Dim InitCtrl As Label
InitCtrl.Name = "lbl_T_Init_" & Microsoft.VisualBasic.Right(ProjCtrl.Name, 2)
If Me.ActiveControl.Text = "xxxxxx" Then
InitCtrl.Text = "Primary Project"
Else
InitCtrl.Text = "Secondary Project"
End If
End Sub
显然我在Dim InitCtrl阶段出错了。
甚至可以将Label与适用的ComboBox关联起来吗?
任何人都可以提供协助。在此先感谢
Clearly I am going wrong at the Dim InitCtrl stage.
Is it even possible to associate the Label with the applicable ComboBox?
Could anyone please assist. Thanks in advance
推荐答案
只是为了更好地坚持你的榜样...
你的代码:
Just to stick to your example as more as possible...
Your code:
Dim InitCtrl As Label
InitCtrl.Name = "lbl_T_Init_" & Microsoft.VisualBasic.Right(ProjCtrl.Name, 2)
应该更改为:
Should change to something like:
Dim sInitCtrlName As String
sInitCtrlName = "lbl_T_Init_" & Microsoft.VisualBasic.Right(ProjCtrl.Name, 2)
Dim InitCtrl As Label = Me.Controls(sInitCtrlName)
甚至还有其他方法,例如:
- 根据标签的位置查找标签,如果每个标签始终与其ComboBox配合位于同一侧且距离。
- 如果标签和 ComboBox 控件放在 TableLayoutPanel 内(有2列,行数与Label + ComboBox一样多)对是),您可以使用 GetPositionFromControl 和 GetControlFromPosition 方法从任何其他控件中查找配对控件。
- 具有自定义的ComboBox派生具有标签类型的专用属性的类,在设计时将其分配给正确的标签控件,但忘记了名称。
- 分配标签控制为标记道具初始化时 ComboBox 的erty。
- 在初始化时将相同的值分配给 Label <的属性 Tag / b>和 ComboBox 并使用它来查找匹配的标签循环 Me.Controls 集合。
- 创建复合 UserControl 包含 Label 和 ComboBox ,处理所有事件本身并公开内部属性,然后将每个现有的Label + ComboBox对替换为此控件的一个实例。
- 按照 nilesh sawardekar 的建议,在运行时创建并添加控件,在此过程中以某种方式链接它们。
问候,
Daniele。
There are even other approaches, like:
- Finding the label by its location, if each label is always at the same side and distance from its ComboBox mate.
- If the Label and ComboBox controls are layed inside a TableLayoutPanel (with 2 columns and as many rows as the Label+ComboBox pairs are), you can use GetPositionFromControl and GetControlFromPosition methods to find the mate control from any other control.
- Having a custom ComboBox-derived class with a dedicated property of type Label to assign at design time to the correct Label control, though forgetting about the names.
- Assigning the Label control as Tag property of the ComboBox at initialization time.
- Assigning at initialization time the same value to the property Tag of both the Label and ComboBox and using it to find the matching label looping Me.Controls colletion.
- Creating a composite UserControl containing both a Label and a ComboBox , handling all events itself and exposing the internal properties, then replacing each existing pair of Label+ComboBox with one instance of this control.
- As suggested by nilesh sawardekar, create and add the controls at run-time and during this process link them in some way.
Regards,
Daniele.
这篇关于通过动态构建控件名称来更新控件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!