Understanding Cartesian Coordinates vs. Window Forms in VB.Net
Graphs are an essential tool in mathematics and programming. In this tutorial, you'll learn how to draw graphs in VB.Net using the built-in System.Drawing library. We'll walk through the steps for creating a coordinate plane, plotting functions, and building your own simple Visual Basic graph control.
The Cartesian Plane is the usual graph surface we are taught in school. It consists of the x and y axes.
In Visual Basic, the Window's Form would represent something like a graph book;
and this graph book
is plotted or drawn on using code.
However, there is a subtle difference between the way we use graphs and the Visual Basic form:
The y-axis of the Visual Basic form is measured from the top and increases as you move downwards.
Setting Up the Coordinate Plane in VB.Net
Visual Basic .NET is a beginner-friendly language that still provides powerful tools for graphics programming. With just a few lines of code, you can:
- Draw Cartesian graphs with x and y axes.
- Plot mathematical functions (e.g., 'y = x^2').
- Create simple charts and graphs for projects.
- Work with the Paint event to refresh and update drawings.
Drawing on the Visual Basic Form
For drawing in Visual Basic.Net, we'll use the Windows Forms environment and the 'System.Drawing' namespace.
This allows you to draw lines, shapes, and text directly on a form.
With Visual Basic.Net, drawing is usually done in the paint method
of a window form or picture box.
Create a new Visual Basic Windows Forms Application
project
;
call it Dymetric_VB.
Create a new Visual Basic class file;
call it Facet.
Type out the adjoining VB.Net code to see what the windows form looks like.
Note: This code creates the x and y axes - the basis for any VB.Net graph. We'll extend this code to build other graphing projects in VB.Net!
What You've Learnt on Graphs and VB.Net Windows Forms
In this tutorial, we've shown how to draw graphs in VB.Net using System.Drawing. Visual Basic makes it possible to create a coordinate plane on a Windows Form and plot functions, lines, and points. This Visual Basic graph tutorial serves as a step-by-step guide through the process.
By using the 'Paint' event, you can plot functions in VB.Net and create simple charts and graphs
for school projects or professional applications. This example demonstrates how to implement a graph control in Visual Basic
with code you can adapt.
Understanding how to work with graphs - both as mathematical models and as graphics on a
VB.Net windows form-is essential for senior secondary students and aspiring developers.
Enhancing Your VB.Net Graphs
To make graphs more useful, you can:
- Add gridlines for readability.
- Label axes with numbers.
- Create reusable graph drawing functions.
- Extend to basic VB.Net charting controls if you need bar or line charts.
Key Takeaways on Graphs and VB.Net Windows Forms
With a little code, you can build custom graphs in Visual Basic. Whether you're plotting math functions, drawing coordinate planes, or experimenting with graphics, VB.Net makes it easy to get started.
This Visual Basic graph tutorial introduced you to:
- Drawing axes with System.Drawing
- Plotting functions like 'y = x^2'
- Enhancing visuals with gridlines and labels
VB.Net Graph Code for Form Class
Private cycle As New EllipticalRegion
Private do_simulation As Boolean = False
' decide what course of action to take
Public Sub decideAction(sender As Object, g As Graphics, click_check As Boolean)
If do_simulation And click_check Then
' do animation
cycle.play(sender, g)
do_simulation = False
Else
' Put ball on screen
cycle.prep(sender, g)
do_simulation = True
End If
End Sub
End Class
VB.Net Graph Code for Facet Class
Dim screen_rect As Rectangle
Dim response_btn As New Button()
Public Sub formFeatures(control As Object)
'Set window position, width and height
screen_rect = Screen.PrimaryScreen.Bounds
control.SetDesktopBounds(0, 0, screen_rect.Width, screen_rect.Height)
' Set a display text
sender.Text = "usingMaths.com"
' Set a background colour
sender.BackColor = System.Drawing.Color.Orange
' Set an icon image
Dim path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
path = New Uri(path).LocalPath
Try
sender.Icon = New Icon(path & "\usingMaths.ico")
Catch ex As Exception
' Well, just go on and use default pic
End Try
End Sub
End Class
Important:
Get the logo image(.ico) used in the code here
and save it to the same folder(directory) as your code files.
For Visual Studio, this folder should be
C:\Users\user_name\Documents\Visual Studio 20**\Projects\Dymetric_VB\Dymetric_VB\bin\Debug.
VB.Net Drawing Tutorial Reference
For a thorough explanation of drawing graphics in Visual Basic, please see the following links:
Drawing Graphics in Visual Basic - Step by step guide on using Visual Studio for Visual Basic graphics.
Graphics for Visual Basic 6.0 Users - largely apt for Visual Basic beginners; Knock your head out!!