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.
The question is where should I put the call procedure statement in my code? And what should I put? I tried to put
before the
, and the error appeared is"Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.PaintEventArgs'."
Thanks in advance!
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
VB.NET:
Call OnPaint(e)
VB.NET:
Next i
Thanks in advance!
Last edited: