Hi!
I was looking on MSDN and I saw that there are no circle and line functions.
How do you make circles and lines in VB.NET?
Thanks!
Printable View
Hi!
I was looking on MSDN and I saw that there are no circle and line functions.
How do you make circles and lines in VB.NET?
Thanks!
I would imagine that it would be something like this:
Dim graphicscontext As Graphics
graphicscontext = MyBase.CreateGraphics
graphicscontext.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)
This code doesn't quite work, and I have a feeling that it's in the way that VB is attempting to carry out inheritance, or in my faulty understanding of the way that VB is attempting to carry out inheritance.
The MyBase keyword accesses methods in the classes parent. CreateGraphics is a part of the Form Class, and for some reason is not accessable when your form class is instanced. I had a similar problem when modifying the title bar of a form during execution. When the IDE tells you "This reference to a non-shared member requires an object reference, and none is supplied." using the mybase keyword seems to make things better.
The MSDN documetation isn't exactly clear, and there are no samples, and there are differences between the class view for forms and what is seen in the IDE as availible functions for the forms parents.
Also, there is a C# version of scribble with the VS.NET install, and if you are feeling extra curious you could go and look at it.
Okay. Let's make a new Form, a Button, and a PictureBox.
In our Button's code, let's add the following:
We do that cause first we have to obtain something called an "object reference". So we "refer" to the PictureBox object. So now we have to give the computer a pen to draw with:Code:Dim g As Graphics = PictureBox1.CreateGraphics
So that's just a default pen, (no fancy dotted-line brushes or anything), and we set the colour to red. After "Color.red" you can just add a comma and type in the value of the width, OR you can just add the following line of code:Code:Dim myPen As New Pen(Color.Red)
So now our pen has the width of 2 pixels. Let's finally draw the stupid circle:Code:myPen.Width = 2
Okay, so referring to our graphics object reference (which is the "g"), we tell it to draw an Elliptical shape (oval or circle). We tell the computer to draw it using the pen we gave it (red and 2 pixels thick), given the co-ordinates of an invisible rectangular region starting (4, 4) and going diagonally to the opposite corner: (50, 50). These units are all measured in pixels.Code:g.DrawEllipse(myPen, New Rectangle(4, 4, 50, 50))
So you should end up with the following in your button's code:
Run it, click the button! OMG IT'S A CIRCLE! hehehe. YAAAY!Code:Dim g As Graphics = PictureBox1.CreateGraphics
Dim myPen As New Pen(Color.Red)
myPen.Width = 2
g.DrawEllipse(myPen, New Rectangle(4, 4, 50, 50))
Okay, so you want a Line instead/as well...
Just add or replace the last line we have with:
And that will draw a 2px-thick red line from (4, 4) to (50, 50).Code:g.DrawLine(myPen, 4, 4, 50, 50)
We can also do:
Reply if you're still having probs :DCode:g.DrawBezier(myPen, 15, 15, 30, 30, 45, 30, 87, 20)
g.DrawPolygon(myPen, New PointF() {New PointF(1, 1), _
New PointF (20, 10), New PointF(5, 4), New PointF(100, 2), _
New PointF(200, 50), New PointF(39, 45)})
Great stuff. Thanks AdiF.
How do you fill the circle or rectangle with colour? I can't find any parameter that allows you to make the object solid.
myPen.PenType = PenType.SolidColor
Name 'PenType' is not declared :(
Am i missing an import statement? :confused:
I added Imports System.Drawing.Drawing2D and it says Property 'PenType' is 'ReadOnly'.
A Google search on PenType is not helpful, and i searched a bunch of other .NET sites with no luck. :(
I think maybe that .PenType is not the answer.
I found the answer myself. It's .FillRectangle. :)