在SelectedIndex事件触发时修改包含组合框的结构字段 [英] Modify a structure field containing combobox when SelectedIndex event fires
问题描述
我正在尝试使用由标签和值组成的通用小部件。该值由组合框设置。结构如下:
I am trying to have a generic widget composed of a label and a value. The value is set by a combobox. Here is the structure:
Structure tParam
Dim label As Label
Dim comboBox As ComboBox
Dim valueX As String
End Structure
Dim parameter1 As tParam
我想在触发SelectedIndexChanged事件时修改valueX。
I'd like to modify the valueX as the SelectedIndexChanged event is fired.
现在我已经设置
parameter1.label.text = "Id"
parameter1.comboBox.Tag = parameter1 ' the struct itself
AddHandler parameter1.comboBox.SelectedIndexChanged, AddressOf updateParam
并在处理程序中
Private Sub updateParam(sender As Object, e As System.EventArgs)
Dim parameterX As tParam = sender.Tag
With parameterX
Select Case .label.Text
Case "Id"
parameter1.valueX = .comboBox.SelectedIndex
End Select
End Sub
问题是在我有很多(> 50)类型tParam的参数时,我不想用select大小写检查每个参数名称。
The problem is that I have a lot (>50) parameters of type tParam and I like not to check every parameter name with the select case.
请注意,我直接在处理程序,因为parameterX(= sender.Tag)是只读的,因为对parameterX的任何更新都是本地的。
Note that I am calling directly parameter1 in the handler, because parameterX (=sender.Tag) is read-only, as any update to parameterX is local.
推荐答案
I不能完全说出您要做什么,但是 tStruct.ComboBox.Tag = Me
似乎是一种复杂的跟踪小部件的方法。使用一个类,您可以内部化和简化您尝试做的一些 :
I cant quite tell what you are trying to do, but tStruct.ComboBox.Tag = Me
seems a convoluted way to track your widgets. Using a class, you could internalize and simplify some of what it seems you are trying to do:
Public Class CBOWidgetItem
Private WithEvents myCBO As ComboBox
Private myLbl As Label
Public Property Name As String
Public Property Value As String
Public Sub New(n As String, cbo As ComboBox, lbl As Label)
Name = n
myCBO = cbo
myLbl = lbl
End Sub
Private Sub myCBO_SelectedIndexChanged(sender As Object,
e As EventArgs) Handles myCBO.SelectedIndexChanged
Value = myCBO.SelectedIndex.ToString
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Class
该小部件能够处理自身的值更改(再次,我不太清楚您在做什么)。您可能还有其他包装道具来公开小部件正在管理的某些信息:
The widget is able to handle the Value change itself (again, I dont quite know what you are up to). You might have other wrapper props to expose certain info the widget is managing:
Public ReadOnly Property LabelText As String
Get
If myLbl IsNot Nothing Then
Return myLbl.Text
Else
Return ""
End If
End Get
End Property
要使用它:
' something to store them in:
Private widgets As List(Of CBOWidgetItem)
...
widgets = New List(Of CBOWidgetItem)
' long form
Dim temp As New CBOWidgetItem("ID", ComboBox1, Label1)
widgets.Add(temp)
' short form:
widgets.Add(New CBOWidgetItem("foo", ComboBox2, Label2))
在其他地方,如果您需要查找以下内容之一这些家伙:
Elsewhere if you need to find one of these guys:
Dim find = "ID"
Dim specificItem = widgets.Where(Function(s) s.Name = find).FirstOrDefault
If specificItem IsNot Nothing Then
Console.WriteLine(specificItem.Name)
End If
也可以使用 Dictionary(Of String,CBOWidgetItem)
并按名称取回它们。
Alternatively, you could use a Dictionary(Of String, CBOWidgetItem)
and get them back by name.
这篇关于在SelectedIndex事件触发时修改包含组合框的结构字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!