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:
  1. Public Class clsCommTest
  2.  
  3.   Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
  4.  
  5.   Public Sub InitializeCommControl(ByRef frmHost As Form)
  6.     frmHost.Controls.Add(mscMSComm)
  7.     With mscMSComm
  8.       .InputLen = 1
  9.       .InBufferSize = 1024
  10.       .SThreshold = 0
  11.       .EOFEnable = False
  12.       .DTREnable = False
  13.       .Handshaking = MSCommLib.HandshakeConstants.comNone
  14.       .InputMode = MSCommLib.InputModeConstants.comInputModeText
  15.       .NullDiscard = False
  16.       .ParityReplace = ""
  17.       .OutBufferSize = 0
  18.       .RThreshold = 1
  19.       .Settings = "56000,N,8,1"
  20.       .CommPort = 1
  21.       .PortOpen = True
  22.     End With
  23.   End Sub
  24.  
  25. 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:
  1. Public Class clsCommTest
  2.  
  3.   Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
  4.  
  5.   Public Sub New()
  6.     System.Windows.Forms.Form.ControlCollection.Add(mscMSComm)
  7.     With mscMSComm
  8.       .InputLen = 1
  9.       .InBufferSize = 1024
  10.       .SThreshold = 0
  11.       .EOFEnable = False
  12.       .DTREnable = False
  13.       .Handshaking = MSCommLib.HandshakeConstants.comNone
  14.       .InputMode = MSCommLib.InputModeConstants.comInputModeText
  15.       .NullDiscard = False
  16.       .ParityReplace = ""
  17.       .OutBufferSize = 0
  18.       .RThreshold = 1
  19.       .Settings = "56000,N,8,1"
  20.       .CommPort = 1
  21.       .PortOpen = True
  22.     End With
  23.   End Sub
  24. 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 !!!