Solved

Visual Basic 2008. Copy a line graph - between two picture boxes

Posted on 2009-05-06
10
724 Views
Last Modified: 2013-11-27
Please include all code for entire project and also include a screenshot of the solution.

If a picturebox had a set of segments that create one line (create a grid, set points so that each y value can only have one x value, plot line), how could I translate (copy exactly) that line to picturebox2?
0
Comment
Question by:NerdsOfTech
  • 5
  • 5
10 Comments
 
LVL 19

Author Comment

by:NerdsOfTech
Comment Utility
Oops each x can only have one y value (one point per column)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Is the line in the second PictureBox supposed to be editable by the user?  ...or the user creates on the first one, hits copy, then the second one is "static"?  Also, do you want a LITERAL IMAGE of the first?  ...or do you want to retain the DATA (a copy) from the first and simply display it in the second?
0
 
LVL 19

Author Comment

by:NerdsOfTech
Comment Utility
Excellent question! Yes the copy is active and with its own data. Can it also be a different color as well? Thanks!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Ok...so basically you need the SAME behaviour as the original PictureBox, but with the ability to specify a color and an initial data set?

It sounds like you should be considering writing a custom UserControl so that you can duplicate the plot easily and customize it.  Is this something you want to pursue?...  (it's really not that hard)
0
 
LVL 19

Author Comment

by:NerdsOfTech
Comment Utility
Yes, definately! However, Can I post that as a separate question?

1. I would like to see the code on 2 pictureboxs already on the form
2. The left box would have a blue line (segmented like the previous projects)
3. The right box would have a red line (")
4. Each picturebox is independent and you can plot on either one with the mouse.
5. Here is the twist.... 2 buttons somewhere on the form: 1 Red and 1 Blue button.
6. If you click on the blue button the blue line's points are copied as points on the red line (the red line is cleared before the copy happens)
7. If you click on the red button the red line's points are copied as points on the blue line (the blue line is cleared before the copy happens)

This one is all yours!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
This should do it...
(PictureBox1, PictureBox2, Button1, Button2)
Imports System.Drawing.Drawing2D

Public Class Form1
 

    Private GridDist As Integer = 25

    Private LeftPoints As New SortedList(Of Integer, Integer)

    Private RightPoints As New SortedList(Of Integer, Integer)
 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Button1.Text = ">>>"

        Button1.ForeColor = Color.Blue

        Button2.Text = "<<<"

        Button2.ForeColor = Color.Red

    End Sub
 

    Private Sub PB_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown, PictureBox2.MouseDown

        Dim x As Integer = Math.Round(e.X / GridDist) * GridDist

        Dim y As Integer = Math.Round(e.Y / GridDist) * GridDist
 

        If e.Button = Windows.Forms.MouseButtons.Left Then

            If sender Is PictureBox1 Then

                LeftPoints(x) = y

                PictureBox1.Refresh()

            Else

                RightPoints(x) = y

                PictureBox2.Refresh()

            End If

        End If

    End Sub
 

    Private Sub PB_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint, PictureBox2.Paint

        Dim gp As New GraphicsPath

        Dim pts As New List(Of Point)

        Dim PB As PictureBox = CType(sender, PictureBox)

        Dim source As SortedList(Of Integer, Integer) = IIf(PB Is PictureBox1, LeftPoints, RightPoints)
 

        For Each pair In source

            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 PB.Width Step GridDist

            e.Graphics.DrawLine(Pens.Black, x, 0, x, PB.Height)

        Next

        For y As Integer = GridDist To PB.Height Step GridDist

            e.Graphics.DrawLine(Pens.Black, 0, y, PB.Width, y)

        Next
 

        Dim C As Color = IIf(PB Is PictureBox1, Color.Blue, Color.Red)

        If pts.Count >= 2 Then

            Using P As New Pen(C, 3)

                e.Graphics.DrawLines(P, pts.ToArray)

            End Using

        End If

        Using B As New SolidBrush(C)

            e.Graphics.FillPath(B, gp)

        End Using

        e.Graphics.DrawPath(Pens.Black, gp)

    End Sub
 

    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click

        If sender Is Button1 Then

            RightPoints.Clear()

            For Each pair In LeftPoints

                RightPoints.Add(pair.Key, pair.Value)

            Next

            PictureBox2.Refresh()

        Else

            LeftPoints.Clear()

            For Each pair In RightPoints

                LeftPoints.Add(pair.Key, pair.Value)

            Next

            PictureBox1.Refresh()

        End If

    End Sub
 

End Class

Open in new window

GridPlotClone.jpg
0
 
LVL 19

Author Closing Comment

by:NerdsOfTech
Comment Utility
Idle_Mind, simply genius! Another amazing job! Thank you again.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
So...see how the code is EXACTLY the same for both PictureBoxes and I simply change the Color and SortedList being used based on the "sender" variable?  This is an indicator that a UserControl should be created to encapsulate the functionality.  This way you don't have to maintain seperate SortedList variables and manually change the Color.  Each UserControl can have its own data and attributes neatly seperated from every other UserControl.  The code in the Form would only deal with telling one UserControl to copy its data to another...
0
 
LVL 19

Author Comment

by:NerdsOfTech
Comment Utility
That is awesome. Hypothetically speaking, if there were 4 buttons and 4 graphs then these subs could handle all 4 graphs and button functions with small changes and Case selects -- without duplication of code?... Wow...enlightening!

Thank you so much!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Exactly!...   (the light bulb shines a little brighter)

This is the concept behind OOP (Object Oriented Programming) and it's great to take a real world example from the ground up and show how a proof of concept idea works...but would become cumbersome when applied to many objects.  By encapsulating (cornerstone of OOP) the functionality  into a UserControl you get instant SCALABILITY of an already proven concept.

Object Oriented Programming: http://en.wikipedia.org/wiki/Object_oriented_programming
Encapsulation: http://en.wikipedia.org/wiki/Encapsulation_(computer_science)
Scalability: http://en.wikipedia.org/wiki/Scalability
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now