|
-
Jun 13th, 2003, 09:47 AM
#1
Thread Starter
New Member
Draw and Fill Rectangle using Classes
Hi,
I posted a code below of a Form that Draw and FillColor a Rectangle using a Menu.
Anyone can help me use 2 classes (CRectangle, CColorRectangle) to Draw and FillColor a Rectangle?
It works using Form but when I tried doing it using classes I couldnt get it to draw nor fillcolor. I am a beginner and a student on VB.NET.
Any help will be much appreciated. Thanks a lot.
_____
Zac 
--------
-------------------------------------------------------------------
Public Class Rectangle
Inherits System.Windows.Forms.Form
Private RX As Integer
Private RY As Integer
Private RWidth As Integer
Private RHeight As Integer
#Region " Windows Form Designer generated code "
#End Region
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
'Dim MyCRec As Object
RX = InputBox("Enter X Value: ", "X Value")
RY = InputBox("Enter Y Value: ", "Y Value")
RWidth = InputBox("Enter Width Value: ", "Width Value")
RHeight = InputBox("Enter Height Value: ", "Height Value")
Dim graphicsObject As Graphics = Me.CreateGraphics
Dim MyPen As Pen = New Pen(Color.Red, 1)
Dim MyRec As Rectangle = New Rectangle()
graphicsObject.DrawRectangle(MyPen, RX, RY, RWidth, RHeight)
End Sub
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Dim graphicsObject As Graphics = Me.CreateGraphics
Dim redBrush As New SolidBrush(Color.Red)
graphicsObject.FillRectangle(redBrush, RX, RY, RWidth, RHeight)
End Sub
End Class
-
Jun 13th, 2003, 05:09 PM
#2
I wonder how many charact
VB Code:
Dim graphicsObject As Graphics = [b]Me.CreateGraphics[/b]
For one... Me will be pointing to the Class itself... and the code you listed does not have a CreateGraphics method. It works in the form version because Forms do have a CreateGraphics method.
For it to work as a class you'll need to create a method that accepts a Graphics object... because you want to be able to do
something like this:
VB Code:
Dim myRectangle As New CRectangle
myRectangle.DrawRectangle(G,MyPen, RX, RY, RWidth, RHeight,True)
and how you would define it in the class would be along the lines of this:
VB Code:
Imports System.Drawing
Imports System.Drawing.Drawing2d
Class CRectangle
Public Sub DrawRectangle(ByRef G as Graphics, ByRef MyPen As Pen, ByRef MyBrush As Brush, _
RX As Integer, RY As Integer, RWidth As Integer, RHeight As Integer, FillRect As Boolean)
G.DrawRectangle(MyPen, New Rectangle(RX,RY,RWidth,RHeight) )
If FillRect Then G.FillRectangle(MyBrush,New Rectangle(RX,RY,RWidth,RHeight))
End Sub
End Class
And to make it work in your project, you would need to:
VB Code:
Private Sub MenuItem2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuItem2.Click
RX = InputBox("Enter X Value: ", "X Value")
RY = InputBox("Enter Y Value: ", "Y Value")
RWidth = InputBox("Enter Width Value: ", "Width Value")
RHeight = InputBox("Enter Height Value: ", "Height Value")
'Instantiate a copy of our class
Dim MyRectangle As New CRectangle
MyRectangle.DrawRectangle(Me.CreateGraphics,New Pen(Color.Red,1), New SolidBrush(Color.Red) ,RX,RY,RWidth,RHeight,False) )
For the other menuItem, use the same as for the first
MenuItem, but pass True to the DrawRectangle method...
VB Code:
MyRectangle.DrawRectangle(Me.CreateGraphics,New Pen(Color.Red,1), MyBrush, RX,RY,RWidth,RHeight,True) )
I didn't test this code in the IDE, just typed it out... so if doesn't work offhand, ring back...
Last edited by nemaroller; Jun 13th, 2003 at 05:30 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|