-
Feb 26th, 2012, 07:10 PM
#1
Thread Starter
Lively Member
Backgroundworker and Delegate (center Text)
Hi All,
I have a BackgroundWorker and using a delegate to update an UI on a different thread. Everything is working except the "lblInfo.Left = (Me.Width / 3) - (lblInfo.Width / 4)".
I need this line since it allows me to center the text on the UI as I have several Subroutines and they all have various lengths in text. If possible, Can someone help me
pass the lblInfo.Left = (Me.Width / 3) - (lblInfo.Width / 4) from the delegate so I don't get a IllegalCrossThread. ANY help will be greatly appreciated.
Thanks in Advance.
Delegate Sub SetLabelTextInvoker(ByVal lblInfo As Label, ByVal Text As String)
Sub SetLabelText(ByVal lblInfo As Label, ByVal Text As String)
If lblInfo.InvokeRequired = True Then
lblInfo.Invoke(New SetLabelTextInvoker(AddressOf SetLabelText), lblInfo, Text)
Else
lblInfo.Text = Text
End If
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
lblInfo.Left = (Me.Width / 3) - (lblInfo.Width / 4) ' line that done work unless I set the Control.CheckForIllegalCrossThreadCalls = False which I don't want to.
SetLabelText(lblInfo, "File is being copied and will take a while. Please Wait...")
BackgroundWorker1.ReportProgress(20)
Copy_5GB_Image()
lblInfo.Left = (Me.Width / 3) - (lblInfo.Width / 4)
SetLabelText(lblInfo, "Image Copied Successfully.")
End Sub
-
Feb 26th, 2012, 08:36 PM
#2
Addicted Member
Re: Backgroundworker and Delegate (center Text)
Use an additional parameter for the labels position
Code:
Delegate Sub SetLabelTextInvoker(ByVal lblInfo As Label, ByVal Text As String, ByVal lblposition As Integer)
Sub SetLabelText(ByVal lblInfo As Label, ByVal Text As String, ByVal lblposition As Integer)
If lblInfo.InvokeRequired = True Then
lblInfo.Invoke(New SetLabelTextInvoker(AddressOf SetLabelText), lblInfo, Text, lblposition)
Else
lblInfo.Text = Text
lblInfo.Left = lblposition
End If
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim lblposition As Integer
lblposition = (Me.Width / 3) - (lblinfo.Width / 4)
SetLabelText(lblinfo, "File is being copied and will take a while. Please Wait...", lblposition)
BackgroundWorker1.ReportProgress(20)
Copy_5GB_Image()
lblposition = (Me.Width / 3) - (lblinfo.Width / 4)
SetLabelText(lblinfo, "Image Copied Successfully.", lblposition)
End Sub
-
Feb 26th, 2012, 08:53 PM
#3
Thread Starter
Lively Member
Re: Backgroundworker and Delegate (center Text)
Hi Mc_VB,
It Work like a charm. Thanks a million and one. As simple as it may be to you. I Spent an entire day trying to figure it out. Thanks again and hope your day goes well.
-
Feb 26th, 2012, 09:26 PM
#4
Addicted Member
Re: Backgroundworker and Delegate (center Text)
Hi, an alternative that would probably be better would be to calculate the position just once in the SetLabelText Sub, so using your original code remove all instances of lblInfo.Left = (Me.Width / 3) - (lblInfo.Width / 4) and just use it once in your sub routine
Code:
Sub SetLabelText(ByVal lblInfo As Label, ByVal Text As String)
Dim lblposition As Integer
If lblInfo.InvokeRequired = True Then
lblInfo.Invoke(New SetLabelTextInvoker(AddressOf SetLabelText), lblInfo, Text)
Else
lblInfo.Text = Text
lblposition = (Me.Width / 2) - (lblInfo.Width / 4)
lblInfo.Left = lblposition
End If
End Sub
-
Feb 26th, 2012, 10:34 PM
#5
Thread Starter
Lively Member
Re: Backgroundworker and Delegate (center Text)
Thanks again MC_VB. I use:
lblposition = (Me.Width / 2) - (lblInfo.Width / 3)
SetLabelText(lblInfo, "File is being copied and will take a while. Please Wait...", lblposition)
As I have several "lblposition" so I can't use one in the subroutine. Thanks for helping me fine tune my code though. Appreciate it.
-
Feb 26th, 2012, 11:09 PM
#6
Re: Backgroundworker and Delegate (center Text)
I generally use
Code:
Object.Left=(Me.Width-Object.Width)/2
to center an object on a form and in the case of a label I would also set the align property to center.
Not sure how well the code in the posts above works but the code I use works perfectly with any object regaurdless of the content.
-
Mar 8th, 2012, 08:22 PM
#7
Thread Starter
Lively Member
Re: Backgroundworker and Delegate (center Text)
Hi DataMiser,
Thanks again. Now you got me alll curious. I use
"Object.Left=(Me.Width-Object.Width)/2" but to no vail.
The below works.
"lblposition = (Me.Width / 2) - (lblInfo.Width / 3)"
How exactly would you do it based on what I have above. Thanks
-
Mar 9th, 2012, 01:55 AM
#8
Re: Backgroundworker and Delegate (center Text)
Originally Posted by UCPocoAPoco
Hi DataMiser,
Thanks again. Now you got me alll curious. I use
"Object.Left=(Me.Width-Object.Width)/2" but to no vail.
The below works.
"lblposition = (Me.Width / 2) - (lblInfo.Width / 3)"
How exactly would you do it based on what I have above. Thanks
What I posted was not intended to be used literally. Object refers to whatever object you are centering. It could be a label, a textbox, a picture, another form or anything else that has these properties. You would use the actual name of your object in place of the word Object.
-
Mar 9th, 2012, 03:46 AM
#9
New Member
Re: Backgroundworker and Delegate (center Text)
Hello, I am new to the forum.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|