Results 1 to 2 of 2

Thread: Draw and Fill Rectangle using Classes

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    14

    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


  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    VB Code:
    1. 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:
    1. Dim myRectangle As New CRectangle
    2. 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:
    1. Imports System.Drawing
    2. Imports System.Drawing.Drawing2d
    3. Class CRectangle
    4. Public Sub DrawRectangle(ByRef G as Graphics, ByRef MyPen As Pen, ByRef MyBrush As Brush, _
    5. RX As Integer, RY As Integer, RWidth As Integer, RHeight As Integer, FillRect As Boolean)
    6.  
    7. G.DrawRectangle(MyPen, New Rectangle(RX,RY,RWidth,RHeight) )
    8. If FillRect Then G.FillRectangle(MyBrush,New Rectangle(RX,RY,RWidth,RHeight))
    9.  
    10. End Sub
    11. End Class

    And to make it work in your project, you would need to:
    VB Code:
    1. Private Sub MenuItem2_Click(ByVal sender As System.Object, _
    2.  ByVal e As System.EventArgs) Handles MenuItem2.Click
    3.  
    4.  
    5. RX = InputBox("Enter X Value: ", "X Value")
    6. RY = InputBox("Enter Y Value: ", "Y Value")
    7. RWidth = InputBox("Enter Width Value: ", "Width Value")
    8. RHeight = InputBox("Enter Height Value: ", "Height Value")
    9.  
    10. 'Instantiate a copy of our class
    11. Dim MyRectangle As New CRectangle
    12. 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:
    1. 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
  •  



Click Here to Expand Forum to Full Width