我在vb.net中有一个带有类和带有组合框的用户控件的程序。我想用类中的数组填充组合框。如果用户在usercontrol中选择组合框中的值并将该值返回到类中。我尝试了很多,但没有用。 可能吗?如果是,那么请指导我如何做。
我已将usercontrol的代码编写为container1,如下所示:
Private Sub container1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each i As Double In yl
Me.ComboBox1.Items.Add(i)
Next
End Sub
错误:未声明名称yl。
yl是程序中我主类中的一个数组。
提前致谢
GVG
答案 0 :(得分:0)
好的,你可以分四步完成:
首先向您的用户表单添加一些功能
Public Class CtrlBox
'This is a usercontrol, created in VS
'The designer is not shown in this example
'This event is used to relay changes to the combobox to the outside world
Public Event ComboboxSelectionChanged(ByVal NewIndex As Integer, ByVal NewText As String)
'This Sub sets the combobox items
Public Sub SetComboboxItems(Newitems() As String)
Me.ComboBox1.Items.Clear()
Me.ComboBox1.Items.AddRange(Newitems)
End Sub
'Here the custom event is raised
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
RaiseEvent ComboboxSelectionChanged(Me.ComboBox1.SelectedIndex, Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex).ToString)
End Sub
End Class
包含所选索引的类称为clsStuff。注意我用来返回组合框中所需项目的readonly属性
Public Class clsStuff
'This is like a constant that returns the items you
'want to add to the combobox
Public ReadOnly Property CustomComboItems As String()
Get
Return {"Test 1", "Test 2", "Test 3", "Test 4"}
End Get
End Property
'This variable shall store the selected string
Public SomeString As String = ""
End Class
然后以主表格将所有内容整合在一起
Public Class Form1
'Drag the usercontrol containing the combobox onto your form
'This object is the one used to populate the combobox
'and store the selected item
Dim StuffObj As clsStuff
'Here the object is initialized and the CB-Items are set
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
StuffObj = New clsStuff
Me.CtrlBox1.SetComboboxItems(StuffObj.CustomComboItems)
End Sub
'Here the custom event is handled and used to change the
'string in the object
Private Sub CtrlBox1_ComboboxSelectionChanged(NewIndex As Integer, NewText As String) Handles CtrlBox1.ComboboxSelectionChanged
If StuffObj IsNot Nothing Then
StuffObj.SomeString = NewText
MsgBox("Updated property in object: " & StuffObj.SomeString)
End If
End Sub
End Class
这个方案应该能够很好地满足您的需求。
答案 1 :(得分:0)
虽然有Parent
属性,但它不是从UserControl访问主程序变量的好方法。
(1),您应该创建一个AddItems()
方法,在UserControl中添加以下项目。
Public Sub AddItems(ByVal yl As String())
ComboBox1.Items.Clear()
For Each i As String In yl
ComboBox1.Items.Add(i)
Next
End Sub
(2),创建一个SelectedText()
只读属性,如下所示
Public ReadOnly Property SelectedText()
Get
Return ComboBox1.Text
End Get
End Property
(3),创建一个事件SelectedIndexChanged
Public Event SelectedIndexChanged()
(4),触发事件
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
RaiseEvent SelectedIndexChanged()
End Sub
(5),您可以像这样添加主表单中的项目
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim yl() As String = {"Aa", "Bb", "Cc"}
Container1.AddItems(yl)
End Sub
(6),您可以在主表单中捕获SelectedIndexChanged()
个事件
Private Sub Container1_SelectedIndexChanged() Handles Container1.SelectedIndexChanged
Me.Text = Container1.SelectedText
End Sub