When converting VB6 code to VB.NET, I am getting several problems. I will deal with one in particular here.
The original VB6 code is:
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 yields the following (I shortened the conversion comments, which referred 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:
Name 'Forms'is not declared.
Name 'Load' is not declared.
I just commented out the Load statement. But adding the form to the collection has proven to be a tougher nut to crack.
I have tried several variations:
oForm = System.Windows.Forms.Form.Add(sFormName)
returns error: 'Add' is not a member of 'System.Windows.Forms.Form'
oForm = System.Windows.Forms.Form.AddOwnedForm(sFormName)
returns error: Reference to a non-shared member requires an object reference.
oForm = My.Forms.Add(sFormName)
returns error: 'Add' is not a member of 'RSC_Reports.My.MyProject.MyForms'.
How can I get the form name passed as a parameter added to the collection?
The VB6 code is creating a new instance of a form using the class name. The VB.Net way to do this is reflection.
Try this code from here with an edit to make it case-insensitive.
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
Then replace your line.
oForm = Forms.Add(sFormName)
With this line
oForm = ObjectFinder.CreateForm(sFormName)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With