VB6 to VB.Net Conversion Issues - "Forms" Name Not Announced

When converting VB6 code to VB.NET, I have several problems. I will consider here, in particular, here.

Original code 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

The conversion process gives the following (I shortened conversion comments that link to links that are no longer valid):

    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

The following errors were returned:
  The name "Forms" was not declared. The name "Download" has not been announced.

I just commented on the Load statement. But adding forms to the collection turned out to be a tougher nut to crack.

I tried several options:

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

returns an error: "Add" is not a member of "System.Windows.Forms.Form"

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

returns an error: a link to a non-general user requires a link to an object.

    oForm = My.Forms.Add(sFormName)

: "" "RSC_Reports.My.MyProject.MyForms".

, , ?

+3
1

VB6 . VB.Net - .

, .

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)
+1

All Articles