|
I am getting data from Com1 from a scale. I need to save info in a textbox. I have read that I need a backgroundworker in order to access the textbox that was created on a different thread. I just need a little help with this. Let me show you what I have.
BackgroundWorker.WorkerReportsProgress = True
BackgroundWorker.WorkerSupportsCancellation = True
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialPort.DataReceived
stringCom = serialPort.ReadExisting.ToString()
BackgroundWorker.RunWorkerAsync()
End Sub
Private Sub backgroundworker_Dowork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker.DoWork
Dim argument As Object = e.Argument
For Value As Integer = 0 To 1
If BackgroundWorker.CancellationPending Then
Exit For
End If
BackgroundWorker.ReportProgress(Value, stringCom)
BackgroundWorker.CancelAsync()
Next
Threading.Thread.Sleep(100)
End Sub
Private Sub backgroundworker_Progresschanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker.ProgressChanged
Me.txtbxWeight.Text = e.UserState
End Sub
I know I have somethings in here I probably don't need but I was copying code from a book and was adding and taking out things. My goal is to get the weight stored in txtbxweight.text. This code gives me an error still saying it is of a different thread.
Any help is appreciated.
|
|
|
|
|
I would guess that it might be from your use of the stringCom variable. It looks like it must be declared somewhere on form, then set in the DateReceived method, and then you are referencing it inside the background workers DoWork event. What you need to do is pass it into the DoWork when you call the BackgroundWorker.RunWorkerAsync() instead of just referencing it. If this is not the issue, please post more information about the error you are getting and where in the code you are getting it.
Hope this helps.
|
|
|
|
|
I passed it in like you suggested
code Changes:
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialPort.DataReceived
Dim stringCom As String = serialPort.ReadTo("G")
serialPort.ReadExisting()
BackgroundWorker.RunWorkerAsync(stringCom)
End Sub
Private Sub backgroundworker_Dowork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker.DoWork
Dim argument As Object = e.Argument
For Value As Integer = 0 To 1
If BackgroundWorker.CancellationPending Then
Exit For
End If
BackgroundWorker.ReportProgress(Value, argument)
BackgroundWorker.CancelAsync()
Next
' Threading.Thread.Sleep(100)
End Sub
Private Sub backgroundworker_Progresschanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker.ProgressChanged
Me.txtbxWeight.Text = e.UserState This is where it says "Cross-thread operation not valid: Control 'txtbxWeight' accessed from a thread other than the thread it was created on."
End Sub
Any ideas?
|
|
|
|
|
I'm a little stumped. Perhaps it is because of your Backgroundworker.CancelAsync statement. I'm not exactly sure why you have that in there. Also, you may want to use Me.txtbxWeight.Text = e.UserState.ToString instead....to make sure the cast to a string is done when setting the text....
What happens if you put a break point on the statement that causes the error, and when it hits the break point look at some of the values...does it work the first time it hits hit (When Value is zero) but crashes the second time (when value is one)? Or is it crashing right away?
|
|
|
|
|
It is because you are starting the background worker when you recieve data from the serial port.
"DataRecieved" gets called on a different thread, so, in this instance your background worker is being called on this different thread. I think that you can ditch the background worker altogether, as it doesnt seem to be doing anything important. Instead:
Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
Dim stringCom As String = serialPort.ReadExisting.ToString()
TextChange(StringCom)
End Sub
Delegate Sub TextChangeDelegate(ByVal Str As String)
Sub TextChange(ByVal Str As String)
If Me.txtbxWeight.InvokeRequired Then
Dim d As New TextChangeDelegate(AddressOf TextChange)
Me.Invoke(d, Str)
Else
Me.txtbxWeight.Text = Str
End If
End Sub
This aught to work - I havent compiled it, but it looks about right )
|
|
|
|
|
I have a circle, or any polygon, drawn on a form via VB.NET (2k5 in this case). I would like to know if my cursor is inside of the circle/polygon. Is there an easy way to know this. I've been tinkering with the use of the x,y location of the mouse pointer, but don't know how to convert this into the location of the circle.
Thanks in advance for any advice.
|
|
|
|
|
I am not 100% sure of how to do this, but you could try looking at the Region.Intersect function.
For example, if each of your graphics objects have an associated region, you could test to see if a point (mouse cursor) intersects with your region. If the result region is empty then there is no hit.
|
|
|
|
|
|
I have a functional program in Visual Basic 6.0 that I would like to turn into a .dll.
I set the project type to ActiveX DLL, but when I try to make the project, it pops up a message box that says, "No creatable component detected." Everything I've found on the web says to fix this by changing the instancing property on the class modules to something other than private. Unfortunately, this project doesn't have any class modules because it was functinally programmed (it has .bas files instead of .cls files). So, the modules don't have an instancing property in the properties panel. Does anybody know how to make a publicly creatable component in a functionally programmed project?
By the way, I'm totally new to Visual Basic 6.0, so don't be afraid to totally talk down to me. ;o)
Thanks, in advance, for your help.
|
|
|
|
|
I think the answer is in your question. You will need to rewrite the program to use classes, or at least one class that then calls the code in the .BAS file.
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
That did the trick. I added a class module to access the other modules and now it compiles. Thanks!
|
|
|
|
|
Hai all,
i had one anchor tag apply style through html in aspx. as blue color.
but i need to change the color of the link(anchor tag) to red by using vb.net.
objanch.style()..like that
Thanks in advance
|
|
|
|
|
Now VB.NET is object-oriented language. The following are some of the differences:
Data Type Changes
The .NET platform provides Common Type System to all the supported languages. This means that all the languages must support the same data types as enforced by common language runtime. This eliminates data type incompatibilities between various languages. For example on the 32-bit Windows platform, the integer data type takes 4 bytes in languages like C++ whereas in VB it takes 2 bytes. Following are the main changes related to data types in VB.NET:
. Under .NET the integer data type in VB.NET is also 4 bytes in size.
. VB.NET has no currency data type. Instead it provides decimal as a replacement.
. VB.NET introduces a new data type called Char. The char data type takes 2 bytes and can store Unicode characters.
. VB.NET do not have Variant data type. To achieve a result similar to variant type you can use Object data type. (Since every thing in .NET including primitive data types is an object, a variable of object type can point to any data type).
. In VB.NET there is no concept of fixed length strings.
. In VB6 we used the Type keyword to declare our user-defined structures. VB.NET introduces the structure keyword for the same purpose.
Declaring Variables
Consider this simple example in VB6:
Dim x,y as integer
In this example VB6 will consider x as variant and y as integer, which is somewhat odd behavior. VB.NET corrects this problem, creating both x and y as integers.
Furthermore, VB.NET allows you to assign initial values to the variables in the declaration statement itself:
br> Dim str1 as string = Hello
VB.NET also introduces Read-Only variables. Unlike constants Read-Only variables can be declared without initialization but once you assign a value to it, it cannot be changes.
Initialization here
Dim readonly x as integer
In later code
X=100
Now x can’t be changed
X=200 *********** Error **********
Property Syntax
In VB.NET, we anymore don't have separate declarations for Get and Set/Let. Now, everything is done in a single property declaration. This can be better explained by the following example.
Public [ReadOnly | WriteOnly] Property PropertyName as Datatype
Get
Return m_var
End Get
Set
M_var = value
End Set
End Property
Example:
Private _message as String
Public Property Message As String
Get
Return _message
End Get
Set
_message = Value
End Set
End Property
ByVal is the default - This is a crucial difference betwen VB 6.0 and VB.NET, where the default in VB 6.0 was by reference. But objects are still passed by reference.
Invoking Subroutines In previous versions of VB, only functions required the use of parentheses around the parameter list. But in VB.NET all function or subroutine calls require parentheses around the parameter list. This also applies, even though the parameter list is empty.
User-Defined Types - VB.NET does away with the keyword Type and replaces it with the keyword Structure
Public Structure Student
Dim strName as String
Dim strAge as Short
End Structure
Procedures and Functions
In VB6 all the procedure parameters are passed by reference (ByRef) by default. In VB.NET they are passed by value (ByVal) by default. Parantheses are required for calling procedures and functions whether they accept any parameters or not. In VB6 functions returned values using syntax like: FuntionName = return_value. In VB.NET you can use the Return keyword (Return return_value) to return values or you can continue to use the older syntax, which is still valid.
Scoping VB.NET now supports block-level scoping of variables. If your programs declare all of the variables at the beginning of the function or subroutine, this will not be a problem. However, the following VB 6.0 will cause an issue while upgrading to VB .NET
Do While objRs.Eof
Dim J as Integer
J=0
If objRs("flag")="Y" then
J=1
End If
objRs.MoveNext
Wend
If J Then
Msgbox "Flag is Y"
End If
In the above example the variable J will become out of scope just after the loop, since J was declared inside the While loop.
Exception Handling
The most wanted feature in earlier versions of VB was its error handling mechanism. The older versions relied on error handlers such as "On Error GoTo and On Error Resume Next. VB.NET provides us with a more stuructured approach. The new block structure allows us to track the exact error at the right time. The new error handling mechanism is refered to as Try...Throw...Catch...Finally. The following example will explain this new feature.
Sub myOpenFile()
Try
Open "myFile" For Output As #1
Write #1, myOutput
Catch
Kill "myFile"
Finally
Close #1
End try
End Sub
The keyword SET is gone - Since everything in VB.NET is an object. So the keyword SET is not at all used to differentiate between a simple variable assignment and an object assignment. So, if you have the following statement in VB 6.0
Set ObjConn = Nothing
Should be replaced as
ObjConn = Nothing.
Constructor and Destructor
The constructor procedure is one of the many new object-oriented features of VB.NET. The constructor in VB.NET replaces the Class_Initialize in VB 6.0. All occurance of Class_Initialize in previous versions of VB should now be placed in a class constructor. In VB.NET, a constructor is added to a class by adding a procedure called New. We can also create a class destructor, which is equivalent to Class_Terminate event in VB 6.0, by adding a sub-procedure called Finalize to our class. Usage of Return In VB.NET, we can use the keyword return to return a value from any function. In previous versions, we used to assign the value back with the help of the function name itself. The following example explains this:
Public Function Sum (intNum1 as Integer, intNum2 as Integer) as Integer
Dim intSum as Integer
intSum = intNum1 + intNum2
Return intSum
End Function
Static Methods
VB.NET now allows you to create static methods in your classes. Static methods are methods that can be called without requiring the developer to create instance of the class. For example, if you had a class named Foo with the non-static method NonStatic() and the static method Static(), you could call the Static() method like so:
Foo.Static()
However, non-static methods require than an instance of the class be created, like so:
Create an instance of the Foo class
Dim objFoo as New Foo()
Execute the NonStatic() method
ObjFoo.NonStatic()
To create a static method in a VB.NET, simply prefix the method definition with the keyword Shared.
Regards,
Munna Sarfraz Ahmad
--------------------------
Senior Software Engineer
Fiserv, Noida UP INDIA
Email msskythelimit@yahoo.com
|
|
|
|
|
|
And your point is?
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Wow - I've seen people write questions in articles, now it's articles in forums.
The answer is much shorter - VB6 is rubbish, and VB.NET is a real language.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
VB6 is great --- VB.Net is just greater.
Ask not whether it is useful. Ask what it is useful for.
|
|
|
|
|
Munna Sarfraz Ahmad wrote: But objects are still passed by reference.
Not quite.
Consider the following:
public sub sub1(byref x as collection)
x.add(45)
x=new collection
x.add(23)
end sub
public sub sub2(byval x as collection)
x.add(45)
x=new collection
x.add(23)
end sub
If 'x' is a collection, and I perform sub1(x), then x will now point to a new collection containing the value 23. If a reference to the old collection exists anywhere, the collection pointed to by that reference will now have contain the value 45. If x was the only reference to that collection, it will go bye-bye.
In the second scenario, when I perform sub2(x), then the collection pointed to by 'x' will have whatever it had in it before, along with 45. A new collection will be created and have 23 put into it, but that collection will go bye-bye sometime after the function exits.
|
|
|
|
|
i cant believe you bothered to read that!
|
|
|
|
|
Munna Sarfraz Ahmad wrote: The keyword SET is gone - Since everything in VB.NET is an object.
No, that's not the reason, because that is not true.
There is still a difference between simple variables (value types) and objects (reference types). A reference is however a value type, so whenever you make an assignment, you are copying a value type. As you can't assign the object itself, there is no use for the Set keyword.
Munna Sarfraz Ahmad wrote: We can also create a class destructor, which is equivalent to Class_Terminate event in VB 6.0, by adding a sub-procedure called Finalize to our class.
The memory management in .NET doesn't use reference counting, so the Finalize method is not a destructor. It's not equivalent to the Class_Terminate event. If you need to control the life cycle of an object, the class should implement the IDisposable interface.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi All,
I need a control that work like a Combobox, but accepted more than one selection like a Listbox control.
Someone help me?
Thanks,
|
|
|
|
|
Why not just use the listbox? is there some reason you dont want to use it?
|
|
|
|
|
Thank you Kevin,
Because I want a control that open a dropdown list when got the focus, and close it when lost the focus like a Combobox, but accepted more than one selection like a Listbox control.
|
|
|
|
|
Why not on the show the listbox when your textbox gets focus and hide the listbox when you textbox looses focus?
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
Thank you Steve Jowett,
OK. It is a good idea.
But I have a little problem. My experience in VS User Control construction is very poor. So I'm looking for something already done. A VB or C# User Control that I can downloaded and put in my forms.
Thanks
|
|
|
|
|