Groups | Blog | Home
all groups > vb.net controls > july 2006 >

vb.net controls : ComboBox text Property


Lou
7/24/2006 4:25:47 PM
I get an error message about "Cross threaded operation not valid"
I have a combo box on a form and I simply add two items and then want to
read
the value of the selected item.

I don't have any additional threads running?

Dim sMsg as string

sMsg="The new value is : & Combo1.Text

-Lou



Claes Bergefall
7/25/2006 10:59:03 AM
Are you using any timers or something?
When/how are you reading the value?
When/how are you populating the combo?

/claes

[quoted text, click to view]

Lou
7/26/2006 8:40:21 AM
Its in the Serial Controls datareceived event.

[quoted text, click to view]

Claes Bergefall
7/26/2006 10:15:35 AM
From the docs for DataReceived event:
"The DataReceived event is raised on a secondary thread when data is
received from the SerialPort object"

You need to marshal your call to the thread that created the combobox. Check
out Control.Invoke for more info

/claes

[quoted text, click to view]

Dick Grier
7/27/2006 2:50:12 PM
If the text that you are sending from the combobox are ADDED to it in the
DataReceived event, then (see my example using a Delegate) Invoke or
BeginInvoke syntax is demanded.

In your code, all you are attempting to do is to read a string from the
combo and to send that string when you see a specific character (right?).
Assuming this is so, then, marshaling is not needed. However, see my other
response to make more clear why your code may not be working.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.

Lou
7/27/2006 2:51:30 PM
I'm still stuck. How do I "Marshall"
I should have used VB6!

-Lou

[quoted text, click to view]

Lou
7/27/2006 2:54:56 PM
Can you explain that in English?
What is "Marshal"
2 dats to get data from a combo box out a serial port..Bring back VB6!


[quoted text, click to view]

Lou
7/27/2006 3:19:39 PM
All I need is a way to do this: PLEASE HELP...

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles
SerialPort1.DataReceived

Dim DataRead As String = sender.ReadExisting

Select Case Mid(DataRead, 2, 1)

Case Chr(6)

SerialPort1.Write(cmbBoxData.Text)

End Select

End Sub

[quoted text, click to view]

Lou
7/27/2006 4:41:36 PM
I added items in the form_Load event of the same for the control is on.

[quoted text, click to view]

Jim Wooley
7/27/2006 7:24:52 PM
It sounds like you have added the items to the combo box in a thread other
than the one that created the form. Winforms require that controls on a form
can only be accessed via the thread that they were created under. If you
try to access them from another thread, you will raise an exception. Even
if you didn't implicitly create a new thread, Winforms often have multiple
threads working under the covers.

In this case, you need to use the form's MethodInvoker method to call into
the thread that the form was created on to have it work with it's controls.
See http://msdn2.microsoft.com/en-us/library/system.windows.forms.methodinvoker.aspx.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx

[quoted text, click to view]

Jim Wooley
7/27/2006 9:22:06 PM
Perhaps, but your SerialPort object is running under a different thread.
You need the handler for the DataReceived method to use the form's MethodInvoker
to do the work (possibly adding the items on the combo box?)

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx

[quoted text, click to view]

Lou
7/27/2006 11:07:02 PM
How do I do that?
Any examples I can look at...


[quoted text, click to view]

Lou
7/28/2006 10:15:50 AM
The combo box items are added in the Form_Load.
So When I try to send it(SerialPort1.Write combo1.text) in the DataReceived
event I get the error.
I guess I undersatnd the comboBox is on 1 thread and the DataReceived is on
another, but whats the solution?
I hear about "Marshal" and "Delegates", those are all Latin to me.
i did look at your example code but don't see any examples of what I need.
Your "txtTerm_KeyPress" doesn't show it.
I am so frustrated right now. I know its my ignorance but I really want to
get to understand this .NET
stuff but find it difficult.

Why is it so easy in VB6 and so difficult in .Net?


[quoted text, click to view]

Claes Bergefall
7/28/2006 10:22:12 AM
Something like this should do it:

Private Delegate Function GetComboBoxTextDelegate(ByVal combo As ComboBox)
As String
Private Function GetComboBoxText(ByVal combo As ComboBox) As String
Return combo.Text
End Function

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles
SerialPort1.DataReceived
Dim DataRead As String = sender.ReadExisting
Dim text As String = cmbBoxData.Invoke(New
GetComboBoxTextDelegate(AddressOf GetComboBoxText), cmbBoxData).ToString()
Select Case Mid(DataRead, 2, 1)
Case Chr(6)
SerialPort1.Write(text)
End Select
End Sub

/claes

[quoted text, click to view]

AddThis Social Bookmark Button