I'm upgrading a program I wrote in VB6 to VB2005 and am having a problem with updating a text box.
I have a module that manages serial port communications. When data is received in the serial port buffer, it should update a text box (txtSerialIn) on a form (frmSerialInOut). Here's the code:
Public
Sub Handler(ByVal strInMessage As String)frmSerialInOut.txtSerialIn.Text = strRXData & vbCrLf & frmSerialInOut.txtSerialIn.Text
End SubThe text box is not updating with the text that the variable strRXData contains (checked!). This worked fine in VB6.
Any help would be appreciated.
Thanks.

Add text to a text box from a module
gauz09
Spotty:
I added the forms counter and it returned the correct number of forms, so we can safely say that there are no hidden forms being created.
I followed your instructions for adding a watch but the Add Watch... option is not on there.
Application.doevents made no difference.
Any further ideas
Thanks.
ZPOW
I am assuming that if you stick a breakpoint on the line
My.Forms. ......
That you are seeing that strlnMessage contains some text. And that you have a only a single instance of FrmSerialInOut loaded and visible which has a textbox called txtserialIn on it.
If this is correct then the after stepping through this line you should be able to verify the value on the form (you can even add a watch on the My.Forms.FrmSerialInOut.txtSerialIn.text)
The calling convention looks ok to be updating the textbox from a module.
I'm just concerned that you may create an instance of frmserialInOut somewhere else in you code and you actually updating a new default instance in this code - which is hidden so your only seeing the instance and not the default instance that your setting the property.
You may want to verify this - look at the my.application.Openforms collection to verify that this isnt happening.
DarkDream
I still say that the problem is most likely a threading issue. Many serial components use another thread to return data from devices like this. So when the data is returned, its on another thread. This wasn't an issue with VB 6, but with .NET you cannot update the UI on another thread. You must execute the change back on the UI's thread. To do this, you use the "Invoke" method to execute a call on the UI's thread.
Give it a shot and at least rule that out before you go too much further. I posted an example in this same thread, so let me know if that helps.
Olly127863
OK, I didn't realize you had this code in a module. Try putting the name of your form in place of "Me".
I think you have: My.Forms.frmSerialInOut
BenCh
This may be a threading issue when your data comes back from the serial port module.
Consider trying to "invoke" your update. You'll have to forgive. I'm usually a C# guy but I'm trying to learn some VB.NET. Here's how I "solved" this issue:
First, Create a delegate in the form's scope
Delegate Sub UpdateTextBox(ByVal message As String)
Next, create a method from which to update your textbox.
Private Sub UpdateTextBoxMethod(ByVal message As String)frmSerialInOut.txtSerialIn.Text = message & vbCrLf & frmSerialInOut.txtSerialIn.Text
End Sub
Change your handler method to the following code:
Public Sub Handler(ByVal strInMessage As String)
My.Forms.frmSerialInOut.Invoke(New UpdateTextBox( _
AddressOf UpdateTextBoxMethod), New Object() {"message"})
End Sub
Obviously, there is one problem here. You have two methods with the same signature for no real purpose. If you can make the event that calls "Handler" do the invoke instead of calling into handler, you'll get rid of this problem. However, if you cannot modify the code that calls "Handler" to do the invoke, this should get you set. Let me know if that helps.
Emily B
strInMessage does contain text when I add a breakpoint and I only have a single instance of frmSerialInOut loaded and visible.
How do I add a watch on My.Forms.frmSerialInOut.txtSerialIn.text Also how do I look at the my.application.Openforms collection to verify that a new instance of frmSerialInOut isn't being created. Remember I'm new to VB2005.
Thanks.
JohnMorales
MarlonM
Ok, thanks again Spotty, unfortunately your suggestions have made no difference.
I have ensured that the variables are populated. Valid point about the variables mix up, I have resolved this, although they did both hold the correct data so it doesn't appear to matter which one I use (perhaps I'm wrong though). The form is already visible at the time the module is called.
Here is the code as it stands now:
Imports
System.TextImports
System.IO.PortsModule
Comms Public WithEvents serialport As New IO.Ports.SerialPort Public strRXData As String Public Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialport.DataReceived If serialport.BytesToRead Then Call Checkinput() End If End Sub Public Sub Checkinput() Dim strRxChar As StringstrRxChar = serialport.ReadLine()
strRXData = strRXData + strRxChar
Call Handler(strRXData) If Right(strRXData, 1) = Chr(13) Then strRXData = "" End Sub Public Sub Handler(ByVal strInMessage As String) My.Forms.frmSerialInOut.txtSerialIn.Text = strInMessage & vbCrLf & My.Forms.frmSerialInOut.txtSerialIn.Text End SubEnd
ModuleKely
Also to try and determine that the convention of calling is correct.
Create a completely separate function something like in the module.
Public Sub Foo
My.Forms.frmSerialInOut.txtSerialIn.Text = "foo"
End Sub
Then on a form stick a button or something that will call this function. This should result in the text 'foo' being correctly displayed in the textbox - then at least you can confirm that the call to display text in a textbox from a module works correctly.
nziese
Well also in the Handler function - you appear to be have a problem with your variables names.
You are Passing a parameter call strlnmessage as a string but in the code you are using strRXData.
Put a breakpoint on the line in Handler and ensure that the variables are populated.
I think you need to
My.Forms.frmSerialInOut.txtSerialIn.Text = strInMessage & vbCrLf & My.Forms.frmSerialInOut.txtSerialIn.Text
This also looks strange as your adding it before the existing contents of the textbox.
The use of My.Forms. should create a default instance of the frmserialinout form in 2005. Are you seeing the a form being displayed when you call this. I'd also add something like a
My.Forms.frmSerialInOut.Show after this to ensure it is visible.
Angna
Add a line msgbox(My.Application.OpenForms.Count) and see if this value matches that of the amount of forms you are seeing on the screen.
Adding a watch is simple as highlighting the Property My.Forms.FrmSerialInOut.TxtSerialIn.Text and then right click and Selecting Add Watch...
Also try sticking and Application.doevents just after you try setting the textbox property.
kitpan
How did you the Sub get the reference to the frmSerialInOut as its not passed in as a parameter.
It may be because in VB6 you had an ability to create a default instance of a form by using just the formName and not creating and instance.
If using VB Express / 2005 then type chnaging the line
My.Forms.FrmSerialInOut.Text = strRXData & vbCrLf & My.Forms.frmSerialInOut.txtSerialIn.Text
Graham Harris
Thanks for your posting Spotty, unfortunately the addition of the My.Forms didn't work (I copied and pasted your text in).
At the moment there's no reference to the form in the module. This is the new VB2005 stuff that I'm trying to get my head around. Here's the entire module:
Imports
System.TextImports
System.IO.PortsModule
Comms Public WithEvents serialport As New IO.Ports.SerialPort Public strRXData As String Public Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialport.DataReceived If serialport.BytesToRead Then Call Checkinput() End If End Sub Public Sub Checkinput() Dim strRxChar As StringstrRxChar = serialport.ReadLine()
strRXData = strRXData + strRxChar
Call Handler(strRXData) If Right(strRXData, 1) = Chr(13) Then strRXData = "" End Sub Public Sub Handler(ByVal strInMessage As String) My.Forms.frmSerialInOut.txtSerialIn.Text = strRXData & vbCrLf & My.Forms.frmSerialInOut.txtSerialIn.Text End SubEnd
ModuleThanks again.
gadams00
MS Tobin Titus:
Thanks for your input. I followed your instructions but got an error ''Me' is not valid within a module'.