Results 1 to 9 of 9

Thread: Backgroundworker and Delegate (center Text)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    86

    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

  2. #2
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    86

    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.

  4. #4
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    86

    Thumbs up 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.

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    86

    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

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Backgroundworker and Delegate (center Text)

    Quote Originally Posted by UCPocoAPoco View Post
    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.

  9. #9
    New Member
    Join Date
    Mar 2012
    Posts
    1

    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
  •  



Click Here to Expand Forum to Full Width