Visual Basic 2008: How to update a graph from another sub routine

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
from the following sub routine the user can add up to 10 cuts in a graph:
Private Sub btnNextCut_Click(sender As System.Object, e As System.EventArgs) Handles btnNextCut.Click<br/>
<br/>
Dim graphicsCuts As System.Drawing.Graphics<br/>
Dim PenColor As New Pen(Color.Black)<br/>
graphicsCuts = ptbDraw.CreateGraphics<br/>
ptbDraw.Refresh()<br/>
<br/>
If LblCutvalue.Text < 10 Then Next cut is allowed<br/>
<br/>
If one of the X,Y values is disabled it means that no cut has been entered yet<br/>
so the first time only the fields need to be enabled<br/>
<br/>
If TxtboxXstartvalue.Enabled = False Then<br/>
<br/>
When clicked enable all entry fields<br/>
TxtboxXstartvalue.Enabled = True<br/>
TxtboxYstartvalue.Enabled = True<br/>
TxtboxXendvalue.Enabled = True<br/>
TxtboxYendvalue.Enabled = True<br/>
Else<br/>
First check if coordinates or valid<br/>
<br/>
If (Xstart >= "0" And Xstart <= "2000" And TxtboxXstartvalue.Text <> String.Empty) Then<br/>
Try<br/>
Xstart = CInt(TxtboxXstartvalue.Text) / 5<br/>
Catch ex As IndexOutOfRangeException<br/>
Console.WriteLine("IndexOutOfRangeException caught")<br/>
End Try<br/>
Else<br/>
MsgBox("X start coordinate value is invalid", MsgBoxStyle.Exclamation)<br/>
Exit Sub<br/>
End If<br/>
<br/>
If (Ystart >= "0" And Ystart <= "1750" And TxtboxYstartvalue.Text <> String.Empty) Then<br/>
Try<br/>
Ystart = CInt(TxtboxYstartvalue.Text) / 4.375<br/>
Catch ex As IndexOutOfRangeException<br/>
Console.WriteLine("IndexOutOfRangeException caught")<br/>
End Try<br/>
Else<br/>
MsgBox("Y start coordinate value is invalid", MsgBoxStyle.Exclamation)<br/>
Exit Sub<br/>
End If<br/>
<br/>
If (Xend >= "0" And Xend <= "2000" And TxtboxXendvalue.Text <> String.Empty) Then<br/>
Try<br/>
Xend = CInt(TxtboxXendvalue.Text) / 5<br/>
Catch ex As IndexOutOfRangeException<br/>
Console.WriteLine("IndexOutOfRangeException caught")<br/>
End Try<br/>
Else<br/>
MsgBox("X end coordinate value is invalid", MsgBoxStyle.Exclamation)<br/>
Exit Sub<br/>
End If<br/>
<br/>
If (Yend >= "0" And Yend <= "1750" And TxtboxYendvalue.Text <> String.Empty) Then<br/>
Try<br/>
Yend = CInt(TxtboxYendvalue.Text) / 4.375<br/>
Catch ex As IndexOutOfRangeException<br/>
Console.WriteLine("IndexOutOfRangeException caught")<br/>
End Try<br/>
Else<br/>
MsgBox("Y end coordinate value is invalid", MsgBoxStyle.Exclamation)<br/>
Exit Sub<br/>
End If<br/>
<br/>
Save the value of Xstart, Ystart, Xend and Yend in an array for later usage<br/>
Cutvalues(LblCutvalue.Text, 0) = Xstart<br/>
Cutvalues(LblCutvalue.Text, 1) = Ystart<br/>
Cutvalues(LblCutvalue.Text, 2) = Xend<br/>
Cutvalues(LblCutvalue.Text, 3) = Yend<br/>
<br/>
LblCutvalue.Text = LblCutvalue.Text + 1<br/>
Refresh()<br/>
<br/>
Empty values in entry fields for next entry<br/>
TxtboxXstartvalue.Text = ""<br/>
TxtboxYstartvalue.Text = ""<br/>
TxtboxXendvalue.Text = ""<br/>
TxtboxYendvalue.Text = ""<br/>
<br/>
End If<br/>
Else<br/>
MsgBox("Sorry, only 10 cuts allowed", MsgBoxStyle.Exclamation)<br/>
End If<br/>
End Sub

Then later on from another sub routine that drawing shall be reused (say e.g. with 2 cuts)
I am trying that with this sub routine:
Private Sub CuttingWood_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load<br/>
Dim CutOrder() As String<br/>
Try open file and trap any errors using handler<br/>
FileOpen(1, "orders.txt", OpenMode.Input)<br/>
Do Until EOF(1) read lines from file<br/>
LineOfText = LineInput(1)<br/>
Orderline = Split(LineOfText, ";")<br/>
If InStr(LineOfText, "(") Then There is at least one cut<br/>
MsgBox("Line of text is now: " & LineOfText)<br/>
MsgBox("There was at least one cut") Do nothing as there were no previous cuts<br/>
If InStr(LineOfText, "#") Then<br/>
divide the string by the "#" character and read the cuts one by one<br/>
Else : InStr(LineOfText, "#") there was only one cut<br/>
LblCutvalue.Text = 1<br/>
Orderline(4) = Orderline(4).Replace("(", "") Remove "("<br/>
Orderline(4) = Orderline(4).Replace(")", "") Remove ")"<br/>
CutOrder = Split(Orderline(4), ",")<br/>
MsgBox("Cutorder is: " & CutOrder(0))<br/>
Xstart = CInt(CutOrder(0))<br/>
Xend = CInt(CutOrder(2))<br/>
Ystart = CInt(CutOrder(1))<br/>
Yend = CInt(CutOrder(3))<br/>
Cutvalues(LblCutvalue.Text, 0) = Xstart<br/>
Cutvalues(LblCutvalue.Text, 1) = Ystart<br/>
Cutvalues(LblCutvalue.Text, 2) = Xend<br/>
Cutvalues(LblCutvalue.Text, 3) = Yend<br/>
graphicsCuts = ptbDraw.CreateGraphics<br/>
<br/>
Me.Refresh()<br/>
End If<br/>
Else<br/>
do nothing as there was no cut<br/>
End If<br/>
Loop<br/>
Catch ex As IndexOutOfRangeException<br/>
Console.WriteLine("IndexOutOfRangeException caught")<br/>
Catch Ex As Exception<br/>
MsgBox(Ex.ToString())<br/>
Finally<br/>
FileClose(1) Close file<br/>
End Try<br/>
End Sub

I see the cut but then when the new cut is being added the old ones disappear and I only see the new one. Probably because I am using this command:
graphicsCuts = ptbDraw.CreateGraphics
in the second routine... But what command to use to use the current graph and add a line to it?

The paint sub routine looks like this:
Private Sub CuttingWood_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint<br/>
Dim graphicsCuts As System.Drawing.Graphics<br/>
Dim PenColor As New Pen(Color.Black)<br/>
graphicsCuts = ptbDraw.CreateGraphics<br/>
Dim x As Integer<br/>
For x = 0 To LblCutvalue.Text<br/>
graphicsCuts.DrawLine(PenColor, Cutvalues(x, 0), Cutvalues(x, 1), Cutvalues(x, 2), Cutvalues(x, 3))<br/>
Next<br/>
End Sub

Regards, Lars


View the full article
 
Back
Top