Results 1 to 5 of 5

Thread: Instantiating Controls Without A Form Reference...

  1. #1

    Thread Starter
    Hyperactive Member techman2553's Avatar
    Join Date
    Mar 2001
    Location
    <- To your left.
    Posts
    362

    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:
    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 !!!
    ----------

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I believe since you are using a COM object and not a .NET native object that it will still need a form or a form handle. In this case it doesn't really matter that you switched to .NET since you are still using an ActiveX object and its the ActiveX object that really has the restriction. Adding it to a Form would be like this in .NET:
    VB Code:
    1. Public Class clsCommTest
    2.  
    3.   Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
    4.  
    5.   [b]Dim frm As New Form[/b]
    6.  
    7.   Public Sub New()
    8.     [b]frm.Controls.Add(mscMSComm)[/b]
    9.     With mscMSComm
    10.       .InputLen = 1
    11.       .InBufferSize = 1024
    12.       .SThreshold = 0
    13.       .EOFEnable = False
    14.       .DTREnable = False
    15.       .Handshaking = MSCommLib.HandshakeConstants.comNone
    16.       .InputMode = MSCommLib.InputModeConstants.comInputModeText
    17.       .NullDiscard = False
    18.       .ParityReplace = ""
    19.       .OutBufferSize = 0
    20.       .RThreshold = 1
    21.       .Settings = "56000,N,8,1"
    22.       .CommPort = 1
    23.       .PortOpen = True
    24.     End With
    25.   End Sub
    26. End Class

  3. #3

    Thread Starter
    Hyperactive Member techman2553's Avatar
    Join Date
    Mar 2001
    Location
    <- To your left.
    Posts
    362
    Hey Edneeis,

    OK - now I'm really confused:

    I tried your sugestion, but was still receiving an error at first, then I inserted the frm.Show command, and it worked !!

    The problem now is that it seems that the form must be visible for the msComm control to function, if you use the frm.Hide or frm.Visible = False, the program generates an error when the first property of the msComm Control is accessed.


    The following works fine, but the form is visible:
    VB Code:
    1. Public Class clsCommTest
    2.  
    3.   Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
    4.  
    5.   Dim frm As New Form
    6.  
    7.   Public Sub New()
    8.     frm.Show
    9.     frm.Controls.Add(mscMSComm)
    10.     With mscMSComm
    11.       .InputLen = 1
    12.       .InBufferSize = 1024
    13.       .SThreshold = 0
    14.       .EOFEnable = False
    15.       .DTREnable = False
    16.       .Handshaking = MSCommLib.HandshakeConstants.comNone
    17.       .InputMode = MSCommLib.InputModeConstants.comInputModeText
    18.       .NullDiscard = False
    19.       .ParityReplace = ""
    20.       .OutBufferSize = 0
    21.       .RThreshold = 1
    22.       .Settings = "56000,N,8,1"
    23.       .CommPort = 1
    24.       .PortOpen = True
    25.     End With
    26.   End Sub
    27. End Class

    The following generates an error at line " .InputLen = 1 "

    The error message is: "An unhandled exception of type 'InvalidActiveXStateException' occurred in axinterop.mscommlib.dll"

    VB Code:
    1. Public Class clsCommTest
    2.  
    3.   Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
    4.  
    5.   Dim frm As New Form
    6.  
    7.   Public Sub New()
    8.     frm.Show
    9.     frm.Visible = False
    10.  
    11.     frm.Controls.Add(mscMSComm)
    12.     With mscMSComm
    13.       .InputLen = 1
    14.       .InBufferSize = 1024
    15.       .SThreshold = 0
    16.       .EOFEnable = False
    17.       .DTREnable = False
    18.       .Handshaking = MSCommLib.HandshakeConstants.comNone
    19.       .InputMode = MSCommLib.InputModeConstants.comInputModeText
    20.       .NullDiscard = False
    21.       .ParityReplace = ""
    22.       .OutBufferSize = 0
    23.       .RThreshold = 1
    24.       .Settings = "56000,N,8,1"
    25.       .CommPort = 1
    26.       .PortOpen = True
    27.     End With
    28.   End Sub
    29. End Class


    What is going on here ?????
    ----------

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Hmm it seems that a form doesn't receive a handle until its shown then and I'm sure that is why the control needs the form for its handle probably for some API calls. Well it looks like you may be stuck either showing a form or not using that control.

  5. #5

    Thread Starter
    Hyperactive Member techman2553's Avatar
    Join Date
    Mar 2001
    Location
    <- To your left.
    Posts
    362
    OK - It's a little clearer now:

    The form can be made invisible after the MSComm control is assigned to the form's control collection, and it will still work, however you still see the form for a split second as the class initializes.

    The MSComm control is obviousely not being fully created until the form is shown graphically, but there has to be a way around this. The MSComm control is not a graphical ActiveX control, so it is just very anoying that I can't keep everything hidden in the background.

    I even tried the: mscMSComm.CreateControl command to force the control creation, but it doesn't seem to help.


    Any other ideas ??


    This works, but the form flashes for a second:

    VB Code:
    1. Public Class clsCommTest
    2.  
    3.   Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
    4.  
    5.   Dim frm As New Form
    6.  
    7.   Public Sub New()
    8.     frm.Show
    9.     frm.Controls.Add(mscMSComm)
    10.     frm.Visible = False
    11.  
    12.     With mscMSComm
    13.       .InputLen = 1
    14.       .InBufferSize = 1024
    15.       .SThreshold = 0
    16.       .EOFEnable = False
    17.       .DTREnable = False
    18.       .Handshaking = MSCommLib.HandshakeConstants.comNone
    19.       .InputMode = MSCommLib.InputModeConstants.comInputModeText
    20.       .NullDiscard = False
    21.       .ParityReplace = ""
    22.       .OutBufferSize = 0
    23.       .RThreshold = 1
    24.       .Settings = "56000,N,8,1"
    25.       .CommPort = 1
    26.       .PortOpen = True
    27.     End With
    28.   End Sub
    29. End Class
    ----------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width