problem with calling a paint procedure

candice

Active member
Joined
Jan 23, 2007
Messages
30
Programming Experience
Beginner
hello guys:
I have a problem here with calling procedure. The two small procedures I have here are
1, read the data in the Excel file as X1 Y1 X2 Y2
2,use the X1 Y1 X2 Y2 to draw rectangles
Now suppose I have 3 rows of data in the excel file, and I use a for loop to excute it. The result I expect is a form with 3 rectangles on it.
VB.NET:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Dim X1 As Integer
    Dim X2 As Integer
    Dim Y1 As Integer
    Dim Y2 As Integer
 
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        Dim oXLApp As Excel.Application         'Declare the object variables
        Dim oXLBook As Excel.Workbook
        Dim oXLSheet As Excel.Worksheet
        oXLApp = New Excel.Application    'Create a new instance of Excel
        oXLBook = oXLApp.Workbooks.Open("C:\Documents and Settings\intern\My Documents\godblessme.xls")
        'Open an existing workbook
        oXLSheet = oXLBook.Worksheets(1)  'Work with the first worksheet
        For i = 1 To 3
            X1 = oXLSheet.Cells.Item(i, 1).Value
            Y1 = oXLSheet.Cells.Item(i, 2).Value
            X2 = oXLSheet.Cells.Item(i, 3).Value
            Y2 = oXLSheet.Cells.Item(i, 4).Value
        Next i
        oXLApp.Visible = True                'Show it to the user
        oXLSheet = Nothing               'Disconnect from all Excel objects (let the user take over)
        oXLBook = Nothing
        oXLApp = Nothing
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        Dim myGraphics As Graphics
        myGraphics = e.Graphics
        Dim myBrush As New SolidBrush(Color:=Color.Yellow)
        Dim myRectangle As Rectangle
        'return the current form as a drawing surface
        myGraphics = Graphics.FromHwnd(hwnd:=ActiveForm().Handle)
        'create a rectangle object
        myRectangle = New Rectangle(x:=X1, y:=Y1, Width:=X2, Height:=Y2)
        'draw the rectangle to the surface
        myGraphics.DrawRectangle(pen:=New Pen(Color.Black), rect:=myRectangle)
        'fill the rectangle 
        myGraphics.FillRectangle(brush:=myBrush, rect:=myRectangle)
    End Sub
End Class
The question is where should I put the call procedure statement in my code? And what should I put? I tried to put
VB.NET:
Call OnPaint(e)
before the
VB.NET:
Next i
, and the error appeared is"Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.PaintEventArgs'."
Thanks in advance!
 
Last edited:
To make Paint event happen call the objects Refresh method, or better, its Invalidate method when you are able to tell it only to repaint a particular rectangle.
 
thanks JohnH,
I tried to replace Call onpaint(e) with Me.Refresh BUT only the third rectangle is on the form...
Seems every time it runs, the previous rectangle will be overrwrite. Is there any way to solve this?
Coz if I wanna draw many rectangles but not only three. Say, the program will draw as many rectangles as the rows in the excel file which indicate the X1 Y1 X2 Y2 of the rectangle. If I do not use a loop, it is impossible to achieve. But if I use a loop, the overwrite problem will come out.
 
You can draw to a bitmap and assign that bitmap to image/backgroundimage property of control.
Or draw all the rectangels by in Paint event by call the DRawRectangle method many times.
 
Back
Top