Instantiating Controls Without A Form Reference...
Hello,
I'm just starting to convert from VB 6.0 to VB.NET, and thought I'd see if a solution for this situation has been provided by .NET:
If you create a control through code in a Class Module, VB 6.0 required that you added it to the Control Collection of a form. Is there a way around this in VB.NET ??
For example:
VB Code:
Public Class clsCommTest
Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
Public Sub InitializeCommControl(ByRef frmHost As Form)
frmHost.Controls.Add(mscMSComm)
With mscMSComm
.InputLen = 1
.InBufferSize = 1024
.SThreshold = 0
.EOFEnable = False
.DTREnable = False
.Handshaking = MSCommLib.HandshakeConstants.comNone
.InputMode = MSCommLib.InputModeConstants.comInputModeText
.NullDiscard = False
.ParityReplace = ""
.OutBufferSize = 0
.RThreshold = 1
.Settings = "56000,N,8,1"
.CommPort = 1
.PortOpen = True
End With
End Sub
End Class
For the MSComm control to be used, it needs to be added to a form's control collection, otherwise an error is generated when the first property is accessed.
Having to pass the class a reference to a form just seems messy - Does anyone know if this can be avoided by using the System.Windows.Forms reference, or some other .NET trick ??
The result would look something like:
VB Code:
Public Class clsCommTest
Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
Public Sub New()
System.Windows.Forms.Form.ControlCollection.Add(mscMSComm)
With mscMSComm
.InputLen = 1
.InBufferSize = 1024
.SThreshold = 0
.EOFEnable = False
.DTREnable = False
.Handshaking = MSCommLib.HandshakeConstants.comNone
.InputMode = MSCommLib.InputModeConstants.comInputModeText
.NullDiscard = False
.ParityReplace = ""
.OutBufferSize = 0
.RThreshold = 1
.Settings = "56000,N,8,1"
.CommPort = 1
.PortOpen = True
End With
End Sub
End Class
This dosn't work of course - the System.Windows.Forms.Form.ControlCollection.Add statement is incorrect, but this would configure the control when the class first initializes without having to pass the reference of an outside Form.
Thanks for any help !!!