dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] ProgressBar does not update as expected when its color changes from the default green

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    13

    Resolved [RESOLVED] ProgressBar does not update as expected when its color changes from the default green

    Firends,

    With default green, progressbar works great...
    Name:  Untitled2.png
Views: 174
Size:  2.0 KB


    But with setting another color, It does not work as expected? Please try the below code (2 methods) and explain me what I am doing wrong or this is a issue with the progressBar?

    In a module


    Code:
    Imports System.Runtime.InteropServices
    Module Module1
    
        <DllImportAttribute("user32.dll")>
        Public Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        End Function
        Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    
    End Module
    In Form Class - Method 1 - With Report Progress
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ProgressBar1.Maximum = 100
            ProgressBar1.Minimum = 0
            SetProgressBarColor(ProgressBar1, 2) 'Set to Red Color
            BackgroundWorker1.RunWorkerAsync()
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    
            Dim i As Long
    
            For i = 1 To 10
                Threading.Thread.Sleep(1000)
                BackgroundWorker1.ReportProgress((i / 10) * 100)           
            Next
    
        End Sub
    
        Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    
            ProgressBar1.Value = e.ProgressPercentage
            ProgressBar1.Refresh()
    
        End Sub
    
        Private Sub SetProgressBarColor(pbar As ProgressBar, clv As Integer)
            SendMessage(pbar.Handle, &H410, clv, 0)
        End Sub
    In Form Class - Method 2 - General
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ProgressBar1.Maximum = 100
            ProgressBar1.Minimum = 0
            SetProgressBarColor(ProgressBar1, 2)
            BackgroundWorker1.RunWorkerAsync()
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    
            Dim i As Long
    
            For i = 1 To 10
                Threading.Thread.Sleep(1000)           
                ProgressBar1.Invoke(Sub() ProgressBar1.Value = i * 10)
            Next
    
        End Sub
    
        Private Sub SetProgressBarColor(pbar As ProgressBar, clv As Integer)
            SendMessage(pbar.Handle, &H410, clv, 0)
        End Sub
    The result of the above methods with different colors
    ==================================
    Name:  Untitled3.png
Views: 170
Size:  2.0 KB
    Attached Images Attached Images  

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,314

    Re: ProgressBar does not update as expected when its color changes from the default g

    Why have you started another thread on this subject? You cant expect your program to work if you do not understand anything you have "wrote".

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    13

    Re: ProgressBar does not update as expected when its color changes from the default g

    Quote Originally Posted by ident View Post
    Why have you started another thread on this subject? You cant expect your program to work if you do not understand anything you have "wrote".
    Hello,

    I Started this thread because, the previous one is about applying colors (so it is still open - may be I can go for a workaround by having few progressbars instead of one there), but as you can see, this thread is about Not updating the progressbar with different colors properly. I can understand everything I write and I am not a newbie. However, what I am trying to explain with this thread is something strange situation that I have not experienced with a progressbar; Just want to get people ideas on why this is happening possibly getting a solution or a workaround for this.

  4. #4
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,250

    Re: ProgressBar does not update as expected when its color changes from the default g

    Quote Originally Posted by shannarad View Post
    Just want to get people ideas on why this is happening possibly getting a solution or a workaround for this.
    Take a look here for a hacky workaround.

    Basically, set the PB's Value property to the target value, and then immediately set the .Value to a lower value (target value -1). Works best when the PB has a large value range. Otherwise you are into juggling the value of the Maximum property as well.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    13

    Re: ProgressBar does not update as expected when its color changes from the default g

    Quote Originally Posted by Inferrd View Post
    Basically, set the PB's Value property to the target value, and then immediately set the .Value to a lower value (target value -1).
    Thank you very much Inferrd, The workaround works great.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width