The second parameter to TryParse will have the result of parsing, if the
function returns true.
If I understand your question, you are drawing an image in your paint
event handler. If the image is the same image every time, then no, you
just keep it at the class level and redraw it using the graphics object
of the PaintEventArgs, calling graphics.DrawImage. If you always want
an image drawn as large as possible, then you could just add the image
as the backgroundimage of the form, and set the backgroundimageformat
property as appropriate.
I am having difficulties understanding VB.net terminology. The code I
have written is appended below. It works, but is probably quite
inefficient, and is not in accordance with good programming
practices.
I will try to improve on it to learn how to do these things before
doing serious work, so I will appreciate if you and others could point
out how several things can be improved, including clearing the screen,
putting the plotting part in a subroutine with Form as an argument,
etc. My questions and comments are in the code below.
Public Class Form3
Dim frmImage As Bitmap = New Bitmap(1000, 800) ' screen size?
Private Sub Form3_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim rect As New Rectangle(100, 50, 200, 120)
e.Graphics.DrawImage(frmImage, 0, 0)
e.Graphics.DrawRectangle(Pens.Green, rect) ' the plot area
End Sub
Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdClear.Click
frmImage = New Bitmap(1000, 800) ' Bad practice?
Me.Invalidate()
End Sub
Private Sub cmdPlot_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdPlot.Click
Dim g As Graphics = Graphics.FromImage(frmImage)
Dim XX(10) As Single, Y1(10) As Single
Dim i As Short, xxx1 As Single, yyy1 As Single
Dim k As Short, xxx2 As Single, yyy2 As Single
For i = 1 To 10
XX(i) = CSng(i) * 0.4 ' In future, Y1 will come from
complicated calculations
Y1(i) = XX(i) * XX(i) * Val(Form2.TextBox1.Text)
Next i
For i = 1 To 9
xxx1 = XX(i) * 200 / (4.0 - 0.0) + 100 ' In future,
x_scale and y_scale will
yyy1 = Y1(i) * 120 / (10.0 - 0.0) + 50 ' be used
xxx2 = XX(i + 1) * 200 / (4.0 - 0.0) + 100 ' A better way
would be to use
yyy2 = Y1(i + 1) * 120 / (10.0 - 0.0) + 50 ' DrawLines but
I don't know how..
g.DrawLine(Pens.Firebrick, xxx1, yyy1, xxx2, yyy2)
Next i
k = 1
Me.Invalidate() : g.Dispose() ' Seems really necessary
End Sub
End Class