[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I draw a grid in Visual Basic 2008 Express Edition and plot line with points?

Posted on 2009-04-26
5
Medium Priority
?
1,326 Views
Last Modified: 2013-11-27
I would like to be able to draw a sqaure grid that I could plot points of a line using a mouse.

The line will be drawn from the leftmost part of the grid to the rightmost point of the grid at anytime there is 2 or more points. If there is more than 2 points drawn the line will follow straight paths to each point from left to right until it connects to the rightmost point.

I would like to limit the points to be able to be placed ONLY on the intercection of the gridlines AND one point PER VERTICAL COLUMN (If a point is attempted to be created on the same column the old point will disapper and the new one will take its place on the column).


I will award 500 points for the best answer that works.

I have included a sample of a grid I would like to be able to match (except I would like the gridlines to be spaced equally - vertically as well as horizontally - so that the grid is squared)

sample-grid.png
0
Comment
Question by:NerdsOfTech
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24237651
Here is the "core" of it...you can use Labels or seperate PictureBoxes for the X/Y axis labels.
Imports System.Drawing.Drawing2D
Public Class Form1
 
    Private GridDist As Integer = 25
    Private Points As New SortedList(Of Integer, Integer)
 
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Dim x As Integer = Math.Round(e.X / GridDist) * GridDist
        Points(x) = Math.Round(e.Y / GridDist) * GridDist
        PictureBox1.Refresh()
    End Sub
 
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim gp As New GraphicsPath
        Dim pts As New List(Of Point)
        For Each pair In Points
            Dim pt As Point = New Point(pair.Key, pair.Value)
            pts.Add(pt)
            Dim rc As New Rectangle(pt.X, pt.Y, 1, 1)
            rc.Inflate(3, 3)
            gp.AddRectangle(rc)
        Next
 
        For x As Integer = GridDist To PictureBox1.Width Step GridDist
            e.Graphics.DrawLine(Pens.Black, x, 0, x, PictureBox1.Height)
        Next
        For y As Integer = GridDist To PictureBox1.Height Step GridDist
            e.Graphics.DrawLine(Pens.Black, 0, y, PictureBox1.Width, y)
        Next
 
        If Points.Count >= 2 Then
            e.Graphics.DrawLines(Pens.Red, pts.ToArray)
        End If
        e.Graphics.FillPath(Brushes.Red, gp)
        e.Graphics.DrawPath(Pens.Black, gp)
    End Sub
 
End Class

Open in new window

GridPlotDemo.jpg
0
 
LVL 20

Author Closing Comment

by:NerdsOfTech
ID: 31574629
Absolutely brilliant! Thank you so much. Full points award.

I will post another question and CONNECT it to here -- on how to make the grid confined to a certain amount of x-axis and y-axis range as well as output the VALUES of Y for the points.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24247196
I wonder if it would be better to generate the GraphicsPath once, and then re-use, instead of generating on every Paint event.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24247214
There is certainly lots of room for improvement Bob!  In his follow-on questions, other experts have already suggested using a Class to hold the data so he can specify different types of "markers", colors, labels, data, etc.

This solution was really limited but I think it demonstrated some new concepts to the author...
0
 
LVL 20

Author Comment

by:NerdsOfTech
ID: 24247380
Thanks guys. I am learning and would love to see how a class works instead in the future. Maybe another question can answer that.

=NerdsOfTech
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question