|
-
Oct 9th, 2003, 08:39 AM
#1
Thread Starter
Hyperactive Member
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 !!!
-
Oct 9th, 2003, 10:01 AM
#2
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:
Public Class clsCommTest
Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
[b]Dim frm As New Form[/b]
Public Sub New()
[b]frm.Controls.Add(mscMSComm)[/b]
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
-
Oct 9th, 2003, 10:53 AM
#3
Thread Starter
Hyperactive Member
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:
Public Class clsCommTest
Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
Dim frm As New Form
Public Sub New()
frm.Show
frm.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
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:
Public Class clsCommTest
Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
Dim frm As New Form
Public Sub New()
frm.Show
frm.Visible = False
frm.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
What is going on here ?????
-
Oct 9th, 2003, 11:11 AM
#4
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.
-
Oct 9th, 2003, 11:23 AM
#5
Thread Starter
Hyperactive Member
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:
Public Class clsCommTest
Private WithEvents mscMSComm As AxMSCommLib.AxMSComm = New AxMSCommLib.AxMSComm()
Dim frm As New Form
Public Sub New()
frm.Show
frm.Controls.Add(mscMSComm)
frm.Visible = False
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|