Knowing how to make a paint program in Microsoft Visual Basic.NET (VB.NET) can get you started using graphics in programming. VB.NET is an object-oriented programming language used to create desktop, Web and mobile applications. VB.NET offers a graphics class that can be used for drawing objects such as rectangles, circles and lines. You can use the ColorDialog control to display all available colours to the user and use the colour selected to draw your objects.
- Skill level:
Things you need
- Microsoft Visual Basic Express
Start Microsoft Visual Basic Express, click "New Project..." on the left pane of your screen, then select "Windows Forms Application." Click "OK."
Double-click "Button" on the "Toolbox" to add a new button. Double-click "Check Box" to add a new text box. Add two more check boxes to the form. Double-click "ColorDialog" to add the control to the program. Double-click "Button1" to open the "Form1.vb"module.
Copy and paste the following code inside the "Button1_Click" procedure to let the user choose a colour:
If Me.ColorDialog1.ShowDialog = DialogResult.OK Then clr = ColorDialog1.Color End If
Copy and paste the following under "Public Class Form1" to create two global variables used to create graphics and colour:
Private gfx As Graphics Private clr As Color
Click the drop arrow from the upper-left combo box and select "(Form1 Events)." Select "Load" to create the Form event and copy and paste the following inside the event:
gfx = Me.CreateGraphics()
Click the "FormClosed" event to create the event and copy and paste the following to dispose the graphics object:
Click the "MouseMove" event to create the event, and copy and paste the code below to draw objects when the user moves the mouse:
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub Dim rect As Rectangle Dim my Pen As Pen my Pen = New Pen(clr, 2) If Me.CheckBox1.Checked Then With rect .X = e.X - 1 .Y = e.Y - 1 .Width = 15 .Height = 15 End With gfx.DrawRectangle(my Pen, rect) End If If Me.CheckBox2.Checked Then With rect .Width = 15 .Height = 15 End With gfx.DrawLine(my Pen, e.X, e.Y, e.X - 1, e.Y - 1) End If If Me.CheckBox3.Checked Then my Pen = New Pen(clr, 2) With rect .X = e.X - 1 .Y = e.Y - 1 .Width = 15 .Height = 15 End With gfx.DrawEllipse(my Pen, rect) End If
Press "F5" to run your program, then click "Button1" to choose a colour for your objects. Check the first check box to draw a series of rectangles. Check the second box to draw lines or check the third check box to draw a series of circles.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for