Solved

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

Posted on 2009-05-06
10
728 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 19

Author Comment

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

Expert Comment

by:Mike Tomlinson
ID: 24321609
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
ID: 24322429
Excellent question! Yes the copy is active and with its own data. Can it also be a different color as well? Thanks!
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24322475
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
ID: 24323305
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24328878
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
ID: 31578819
Idle_Mind, simply genius! Another amazing job! Thank you again.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24330442
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
ID: 24330565
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
ID: 24330653
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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