Results 1 to 5 of 5

Thread: [RESOLVED] PictureBox .Paint() event is not firing

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Resolved [RESOLVED] PictureBox .Paint() event is not firing

    I put together a small bit of code that I wanted to use to test out some chart scrolling, zooming, moving code on.

    In the process, I'm stuck at just getting the Paint() event to trigger.

    I put a break in the Paint() routine and it is never hit.

    I've set AutoRedraw on both Form and Picturebox to true, also added it in code. Nothing.

    I tried pctChart.Refresh and nothing.

    When I search for answers, it suggests doing what I've listed above. But nothing.

    BTW I cannot say whether the code inside the Paint() event even works or not as it isn't getting triggered and I'd like to find out why more than just taking this code and putting it into a separate sub and call it. Educational reasons.

    Here is the complete code:


    frmChart (Form)
    Code:
    Private Function LoadPrices() As Long
    Dim numRows As Long
    Dim fileNum As Integer
    Dim csvLine As String
    Dim i As Integer
    Dim csvValues() As String
    
        'Open the CSV file
        fileNum = FreeFile
        Open "D:\A_TRUNC\DAILY\AN_REV.CSV" For Input As #fileNum
        
        ' Count the number of rows in the CSV file
        numRows = 0
        Do Until EOF(fileNum)
            Line Input #fileNum, csvLine
            numRows = numRows + 1
        Loop
        
        ' Re-dimension the priceData() array with the number of rows
        ReDim priceData(0 To numRows - 1)
        
        ' Rewind the CSV file to the beginning
        Seek #fileNum, 1
        
        ' Loop through the CSV file and populate the priceData() array
        For i = 0 To numRows - 1
            ' Read each line of the CSV file into a string variable
            Line Input #fileNum, csvLine
            ' Split the string into an array of values
            csvValues = Split(csvLine, ",")
            ' Parse the values into the corresponding fields of the PriceData structure
            priceData(i).Date = DateValue(csvValues(0))
            priceData(i).Open = CDbl(csvValues(1))
            priceData(i).High = CDbl(csvValues(2))
            priceData(i).Low = CDbl(csvValues(3))
            priceData(i).Close = CDbl(csvValues(4))
        Next i
        
        ' Close the CSV file
        
        Close #fileNum
     
        LoadPrices = numRows 'return the number of data records loaded
    
    End Function
    
    Private Sub Form_Load()
        
        ' Set the AutoRedraw property to True.
        pctChart.AutoRedraw = True
       
       'Resize the picturebox to fill the form
        pctChart.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
        
        'Load price data
        LoadPrices
        
        
    End Sub
    
    Private Sub pctChart_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
        'Capture the starting position of the mouse when the user left-click-hold the mouse
        If Button = vbLeftButton Then
            pctChart.Tag = x & "," & Y
        End If
    End Sub
    
    Private Sub pctChart_Paint()
        ' Get the width and height of the chart.
        Dim chartWidth As Integer
        Dim chartHeight As Integer
        chartWidth = pctChart.Width
        chartHeight = pctChart.Height
        
        ' Calculate the size of the candlesticks.
        Dim candlestickWidth As Integer
        Dim candlestickHeight As Integer
        candlestickWidth = (chartWidth - 30) / 100
        candlestickHeight = (chartHeight - 30) / 2
        
        ' Draw the candlesticks.
        Dim i As Integer
        For i = 0 To UBound(priceData) - 1
        
            ' Draw the body of the candlestick.
            Dim bodyTop As Integer
            Dim bodyBottom As Integer
            bodyTop = chartHeight - candlestickHeight * priceData(i).Close
            bodyBottom = chartHeight - candlestickHeight * priceData(i).Open
            pctChart.PSet (i * candlestickWidth, bodyTop)
            pctChart.PSet (i * candlestickWidth, bodyBottom)
            
            
        
            ' Draw the wicks of the candlestick.
            Dim wickTop As Integer
            Dim wickBottom As Integer
            wickTop = bodyTop
            wickBottom = bodyBottom
            If priceData(i).Open > priceData(i).Close Then
                wickTop = chartHeight - candlestickHeight * priceData(i).Open
            End If
            If priceData(i).Open < priceData(i).Close Then
                wickBottom = chartHeight - candlestickHeight * priceData(i).Close
            End If
            pctChart.PSet (i * candlestickWidth, wickTop)
            pctChart.PSet (i * candlestickWidth, wickBottom)
        
        Next
        
        ' Clear the drawing buffer.
        pctChart.Cls
    
    End Sub
    Globals Module
    Code:
    Option Explicit
    
    Type priceData
        Date As Date
        Open As Double
        High As Double
        Low As Double
        Close As Double
    End Type
    
    Public priceData() As priceData
    I sure could use some help with this. And if the CSV data file would help I've attached it.

    AN_REV.zip

    TIA

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: PictureBox .Paint() event is not firing

    Per MSDN help, when .AutoRedraw is True, the Paint event is never raised.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: PictureBox .Paint() event is not firing

    Quote Originally Posted by OptionBase1 View Post
    Per MSDN help, when .AutoRedraw is True, the Paint event is never raised.
    Thank you. Unfortunately not all info I find online is reliable. ;-b

    It triggers now. Now I can focus on the code inside that isn't doing anything *yet*.

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: PictureBox .Paint() event is not firing

    Per MSDN help, when .AutoRedraw is True, the Paint event is never raised.
    Quote Originally Posted by webbiz View Post
    Unfortunately not all info I find online is reliable. ;-b
    Well, consider me surprised... (SCNR)

    Olaf

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: PictureBox .Paint() event is not firing

    Quote Originally Posted by Schmidt View Post
    Well, consider me surprised... (SCNR)

    Olaf
    Nothing gets past you Olaf... ;-b


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