gpt4 book ai didi

vb.net - VB6 到 VB.Net 的转换问题 - 名称 'Forms' 未声明

转载 作者:行者123 更新时间:2023-12-01 14:00:01 28 4
gpt4 key购买 nike

将 VB6 代码转换为 VB.NET 时,我遇到了几个问题。我将在这里特别处理一个问题。

原来的VB6代码是:

Public Sub dynForm(sFormName As String, loadingForm As Form, Optional resizeMe As Boolean = True)
On Error GoTo ErrHandler
'Used to dynamically open a form based on its name.

Dim oForm As Form

'Add a Form to the collection
Set oForm = Forms.Add(sFormName)

'Load the Form
Load oForm
If resizeMe Then
setFrmSize oForm
End If
centerForm oForm
'Show The Form
oForm.Show 1, loadingForm

If oForm Is Nothing Then Exit Sub
Set oForm = Nothing

Exit Sub
ErrHandler:

logError Err.Description & vbCrLf & "sFrm:" & sFormName & " not found!", Err.Number, "common.dynForm", ErrorMsg
End Sub

转换过程产生以下结果(我缩短了转换注释,它指的是不再有效的链接):

    Public Sub dynForm(ByRef sFormName As String, ByRef loadingForm As System.Windows.Forms.Form, Optional ByRef resizeMe As Boolean = True)
On Error GoTo ErrHandler
'Used to dynamically open a form based on its name.

Dim oForm As System.Windows.Forms.Form

'Add a Form to the collection
'UPGRADE_ISSUE: Forms method Forms.Add was not upgraded.
oForm = Forms.Add(sFormName)


'Load the Form
'UPGRADE_ISSUE: Load statement is not supported.
Load(oForm)
If resizeMe Then
setFrmSize(oForm)
End If
centerForm(oForm)
'Show The Form
VB6.ShowForm(oForm, 1, loadingForm)

If oForm Is Nothing Then Exit Sub
'UPGRADE_NOTE: Object oForm may not be destroyed until it is garbage collected.
oForm = Nothing

Exit Sub
ErrHandler:

logError(Err.Description & vbCrLf & "sFrm:" & sFormName & " not found!", Err.Number, "common.dynForm", ErrorType.ErrorMsg)
End Sub

返回了以下错误:
名称“Forms”未声明。 名称“Load”未声明。

我刚刚注释掉了 Load 语句。但事实证明,将表格添加到集合中更难破解。

我尝试了几种变体:

    oForm = System.Windows.Forms.Form.Add(sFormName)

返回错误:“Add”不是“System.Windows.Forms.Form”的成员

    oForm = System.Windows.Forms.Form.AddOwnedForm(sFormName)

返回错误:对非共享成员的引用需要对象引用。

    oForm = My.Forms.Add(sFormName)

返回错误:“Add”不是“RSC_Reports.My.MyProject.MyForms”的成员。

如何将作为参数传递的表单名称添加到集合中?

最佳答案

VB6 代码正在使用类名创建表单的新实例。 VB.Net 的方法是反射。

试试这个代码 from here进行编辑以使其不区分大小写。

Imports System 
Imports System.Windows.Forms
Imports System.Reflection

Public Class ObjectFinder

Public Shared Function CreateObjectInstance(ByVal objectName As String) As Object
' Creates and returns an instance of any object in the assembly by its type name.

Dim obj As Object

Try

If objectName.LastIndexOf(".") = -1 Then
'Appends the root namespace if not specified.

objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName

End If

obj = [Assembly].GetEntryAssembly.CreateInstance(objectName, True)

Catch ex As Exception

obj = Nothing

End Try

Return obj

End Function

Public Shared Function CreateForm(ByVal formName As String) As Form
' Return the instance of the form by specifying its name.
Return DirectCast(CreateObjectInstance(formName), Form)
EndFunction

然后替换你的行。

oForm = Forms.Add(sFormName) 

用这条线

oForm = ObjectFinder.CreateForm(sFormName)

关于vb.net - VB6 到 VB.Net 的转换问题 - 名称 'Forms' 未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21563129/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com