Set Control text properties from another class/thread

I got a form name form1 in this form I have a label named label1

In my class named MyTapi I have eventhandlers that handle certain events in new threads.  

in Form1 I call this:

dim tapi as New MyTapi("12354",label1)

in my class MyTapi:

Private FormLable as label

Sub New(ext as string, lbl as label)

Formlabel=lbl

.
.
.
End Sub

When an event get caught I would like to edit the text of the label

when I do label.text="Change text" in my event handler I get this error:

System.InvalidOperationException was unhandled
  Message="Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on."
  Source="System.Windows.Forms"
  StackTrace:
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Control.set_WindowText(String value)
       at System.Windows.Forms.Control.set_Text(String value)
       at System.Windows.Forms.Label.set_Text(String value)
       at WindowsApplication1.MyTapi.CallStateEvent() in C:\Documents and Settings\rick.maldonado\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\MyTapi.vb:line 146
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

anyhelp will be much appreciated

Thank You
LVL 2
boricua1213Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

boricua1213Author Commented:
oh by the way I am using .NET 2.0 (VB2005)
bchoorCommented:
You can use a delegate

In MyTapi Class

Delegate Sub SetFormLabelText(Value As String)

Dim delSetFormLabelText As SetFormLabelText

Sub New()
...

  delSetFormLabelText = AddressOf FormLabelText
End Sub

Sub FormLabelText(Value As String)
  Form1.Label1.Text = Value

End Sub

In your eventhandler:
delSetFormLabelText.Invoke("Your Label Value")

HTH
~BC
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Your class design seems backwards to me...

Instead of passing a Label into MyTapi (which makes it tightly coupled to the form and less resuseable), make MyTapi raise a Custom Event with the new Label value.   The Form should then subscribe to this event and use its own Delegate to marshal the call from the different thread onto its own thread.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

boricua1213Author Commented:
Idle_Mind

That sounds great but how do you do that?  This is my first program that uses events and threads.  So a quick tutorial will be much appreciated.

Thank you
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
boricua1213Author Commented:
thanks Idle_mind,

that works, sorry it took so long to respond I got put on another project.

thanks again
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.