Sandesh Shakya Sandesh Shakya - 3 months ago 8
Vb.net Question

How can I reference an object drawn using the Graphics Class in VB.NET?

I'm fairly new to VB.NET.

I'm making a program where I have to draw shapes using the e.Graphics Class, and not using PowerPacks. I have been looking for a way to reference the created Object, to modify them, or to use their values. I was wondering if anyone could help me on this.

For Example:

e.Graphics.DrawLine(Pens.Black,20,40, 60, 40)


I want to do something like this:

dim temp as ::Something::
temp = e.Graphics.DrawLine(Pens.Black,20,40, 60, 40)

e.Graphics.DrawLine(Pens.Black, temp.X2, temp.Y1, temp.X2+ 45, temp.Y1)


Where
X2
is the x coordinate of the end of the line drawn by variable temp, and so on.

The closest thing I found was by creating an array of Graphics Objects, but the DrawLine Method doesn't return a value, so I cannot not add it to the array.

This is my first question in stackOverflow, so I apologize if something wasn't clear. I will try my best to be responsive in the comments.

Thank you.

EDIT:
I'm trying to draw this.
https://s32.postimg.org/flk3b5e0l/Untitled.png

The above picture was done using PowerPacks, but since not all computers have PowerPacks, I'm trying to use the Graphics Class.

I think that using the value of a shape will make it easier to align another shape, which is why I'm looking for a way to reference the drawn shapes.

Answer

There is no Object since you're just drawing, but you can use points or create your own structure which contains the coordinates of the lines you're drawing

Using the Point structure:

Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim p1 As New Point(20, 40)
        Dim p2 As New Point(60, 40)
        e.Graphics.DrawLine(Pens.Black, p1.X, p1.Y, p2.X, p2.Y)
        e.Graphics.DrawLine(Pens.Black, p2.X, p1.Y, p2.X + 45, p1.Y)
End Sub

Using a custom structure:

Public Structure lineCoords
    Public x1 As Integer
    Public x2 As Integer
    Public y1 As Integer
    Public y2 As Integer
End Structure

Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim _lineCoords1 As New lineCoords
        With _lineCoords1
            .x1 = 20
            .y1 = 40
            .x2 = 60
            .y2 = 40
        End With

        e.Graphics.DrawLine(Pens.Black, _lineCoords1.x1, _lineCoords1.y1, _lineCoords1.x2, _lineCoords1.y2)

        Dim _lineCoords2 As New lineCoords
        With _lineCoords2
            .x1 = _lineCoords1.x2
            .y1 = _lineCoords1.y1
            .x2 = _lineCoords1.x2 + 45
            .y2 = _lineCoords1.y1
        End With

        e.Graphics.DrawLine(Pens.Black, _lineCoords2.x1, _lineCoords2.y1, _lineCoords2.x2, _lineCoords2.y2)

End Sub
Comments