【问题标题】:How to access this variable?如何访问这个变量?
【发布时间】:2017-01-30 20:21:22
【问题描述】:

我是 VB 新手,我目前正在将一个我没有写入 .net 的 vb6 应用程序迁移到这个错误中,

If TypeOf Application.OpenForms.Item(i) Is frmAddChangeDelete Then
            'UPGRADE_ISSUE: Control ctrlAddChangeDelete1 could not be resolved because it was within the generic namespace Form. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="084D22AD-ECB1-400F-B4C7-418ECEC5E36E"'

            If **Application.OpenForms.Item(i).ctrlAddChangeDelete1.ParentFormNumber = intFormNumber** Then

                If Application.OpenForms.Item(i).Text = "Add Proofed Expense Items" Then
                    boolAddProofed = True
                    Exit For

ctrlAddChangeDelete1 据说是从一个单独的 VB 文件中调用朋友类 ctrlAddChangeDelete,所以我不知道为什么会这样说

"'ctrlAddChangeDelete1' is not a member of 'System.Windows.Forms.Form'."

感谢您的帮助,谢谢!

【问题讨论】:

    标签: vb.net vb6 vb6-migration


    【解决方案1】:

    Application.OpenForms 是一个非强类型的集合。
    当您在那里引用元素时,您会得到一个通用的表单。
    在通用表单中没有名为 ctrlAddChangeDelete1

    的控件

    如果您有一个名为 frmAddChangeDelete 的表单派生类,并且该类有一个名为 ctrlAddChangeDelete1 的控件,那么您需要将存储在 OpenForms 集合中的引用转换为您的特定表单在尝试引用该控件之前进行类。

    此外,要从外部代码访问该控件,您还应该将 Modifiers 属性设置为 Public 而不是默认的 Internal。否则,您将无法从类外部的任何代码访问控件。

    要正确检索您的表单,您可以编写

    Dim delForm = Application.OpenForms.
                             OfType(Of frmAddChangeDelete)
                             FirstOrDefault()
    If delForm Is Nothing Then
        ' No form of type frmAddChangeDelete is present in the collection
        ' write here your message and exit ?
    Else 
        ' Now you can use delForm without searching again in the collection
        ......
    

    上面的代码使用IEnumerable.OfType 扩展名,这需要Imports System.Linq
    如果您不想使用它,那么您可以随时使用TryCast 运算符来获取对正确类的引用

    ' Still you need a for loop up ^^^^ before these lines
    Dim delForm = TryCast(Application.OpenForms(i), frmAddChangeDelete)
    if delForm Is Nothing then 
       ....
    else
       ....
    

    【讨论】:

    • 我现在理解错误好多了,谢谢!虽然现在我想知道我的命名空间/引用有问题,因为 OpenForms 中显然不存在 OfType ...
    • IEnumerable.OfType extensions 需要 System.Linq 命名空间(您应该已经有了参考,但仍然需要 Imporst System.Linq)
    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 2017-12-30
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多