Hi :)
Hi can i set the text of a label from a class.
Printable View
Hi :)
Hi can i set the text of a label from a class.
Yes of course.
In that class write a function which accepts the Test & the referenced label as an argument. Call the function whenever u needed. Make sure to pass by refernce. But I think this is waste of time.
If you wanted to keep the UI out of your class you could also fire off an event that your form is subscribed to. Then the form could change the text when the event is handled.
can anyone please post any example
VB Code:
Public Class Example Public Shared Sub SetText(ByVal lbl As Label, ByVal text As String) lbl.Text=text End Sub End Class 'syntax Example.SetText(Label1,"Some Text")
VB Code:
'Code In Class Public Class cSampleClass Public Event InfoForTextbox(ByVal strInfo As String) Public Function DoSomething() '..... RaiseEvent InfoForTextbox("Hello") '..... End Function End Class
VB Code:
Public Class Form1 Inherits System.Windows.Forms.Form WithEvents cSample As cSampleClass '|| form designer code here Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load cSample.DoSomething() End Sub Private Sub CalledWhenInfoIsPassed(ByVal strInfo As String) Handles cSample.InfoForTextbox LabelName.Caption = strInfo End Sub End Class
Just consider i am using the code posted by <ABX.
Now if i creates multiple instances(mutithreads) of cSampleClass class in a loop will CalledWhenInfoIsPassed handle each instance.
I don't think you can use WithEvents with an array still, so you'[d have to add each new instance using the AddHandler instead of WithEvents.