Combobox不是System.Windows.Forms.Form的成员,从模块

时间:2016-04-05 23:08:47

标签: vb.net forms vb6

我见过thisthisthis,但他们并没有完全回答我的问题。

我有一个项目是将VB6程序升级到VB.NET(2008),最终升级到今天的版本。我有一个模块,可以为许多形式做各种事情。活动表单的名称存储在全局变量(Current_Active_Form)中。从表单到表单的许多表单上都有相同的控件(例如comboX)。我现在关注的是一个函数,它使用for循环加载一个组合框(comboX)和一个项目列表。还有许多其他功能与许多其他组合框和数据网格视图一起执行此操作,因此这将帮助我到处。

示例:

Public Function fillComboX(ByRef fncString As Object) As String
   'Current_Active_Form is a global variable
    Dim startPoint as Integer    'substring starting point
    Dim items as Integer         'number of items in item string
    Dim strItem as String        'string for individual item
    dim strItems as String       'string for all items

    ...'code that fills strItems based on fncString 
       '(don't ask me why it's an object)
    ...'items is filled at the same time

    startPoint = 1
    With Current_Active_Form
        .comboX.Items.Add("")
        '^^ line gives error comboX is not a member of System.Windows.Forms.Form
        For i = 1 to items
            strItem = strItems.Substring(startPoint, 50)
            .comboX.Items.Add(strItem)
            '^^so does this line
            startPoint = startPoint + 50
        Next i
    End With
End Function

有没有办法像我在这里做的那样?我可以做出我需要的任何改变,但这是一个足够大的升级。谢谢!

1 个答案:

答案 0 :(得分:0)

您需要引用Current_Active_Form中的Controls集合,并使用控件名称作为键来获取对要修改的控件的引用。我在下面提供了代码来说明我的意思。代码确实依赖于将Current_Active_Form定义为Form。

Public Current_Active_Form As Form

Public Function fillComboX(ByRef fncString As Object) As String
    'Current_Active_Form is a global variable
    Dim startPoint As Integer    'substring starting point
    Dim items As Integer         'number of items in item string
    Dim strItem As String        'string for individual item
    Dim strItems As String = CStr(fncString)      'string for all items

    'code that fills strItems based on fncString 
    '(don't ask me why it's an object)
    'items is filled at the same time

    startPoint = 0
    With Current_Active_Form
        If .Controls.ContainsKey("comboX") Then
            '^^ line gives error comboX is not a member of System.Windows.Forms.Form
            For i = 0 To strItems.Length - 1
                strItem = strItems.Substring(startPoint, 4)
                DirectCast(.Controls("comboX"), ComboBox).Items.Add(strItem)
                '^^so does this line
                startPoint = startPoint + 4
            Next i
        End If
    End With
End Function

此外,您的代码被定义为函数但不返回值,因此除非您打算返回值,否则应将其定义为Sub。

我还减少了用于测试目的的子串长度,您可以轻松地将其更改回50