Question I don't know when Paint event

solfinker

Well-known member
Joined
Aug 6, 2013
Messages
71
Programming Experience
Beginner
I have two labels in form4 associated with two variables in form1:
Public Class Form1
.....

Form4.Label1.Text = FormatNumber(COR8(0, 0), 4)
Form4.Label2.Text = FormatNumber(COR8(4, 0), 4)
.....
End Class



and I also have another two labels in the same form4 associated with the same variables the following way:


Public Class Form4


Dim COR8f1(,) As Double = Form1.COR8 'which is Public Shared

Private Sub Form4_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim corx, cory As Single
corx = COR8f1(0, 0) : cory = COR8f1(4, 0)

Label3.Text = corx
Label4.Text = cory


End Sub

It happens that Label1.text and Label2.text are immediately updated, but the same does not happen to Label3.text and Label4.text which sometimes update and sometimes don't. I mean, sometimes all labels update at the same time and sometimes it takes 3 or 4 times for labels 3 and 4 to update.

It is obvious that I'm doing sth wrong.
I use Private Sub Form4_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint because I'm drawing a graph that doesn't update unless I reload the form.

Thank you very much for your patience.
 
Last edited:
Why are you handling Paint at all when you're not drawing anything? That's what the Paint event is for, not assigning data to Text properties. Put some thought into when you actually want those Labels to display those values and handle the appropriate event. If it's when the form loads then handle the Load event of the form. If it's when a Button is clicked then handle the Click event of the Button. Etc.
 
Yes, I'm painting, and I don't need the labels. What I need is to update the variables.

Private Sub Form4_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim x, y, axlong, corx, cory As Single 'axlong: axis longitude

corx = COR8f1(0, 0) : cory = COR8f1(4, 0)
axlong = CInt(Math.Max(Math.Abs(corx), Math.Abs(cory)))

With e.Graphics
.SmoothingMode = SmoothingMode.AntiAlias
.ScaleTransform(50, 50)
.TranslateTransform(5, 5)

' Draw axes.

Using p As New Pen(Color.Gray, 0)
.DrawLine(p, -axlong, 0, axlong, 0)
For x = -axlong To axlong Step 0.5
.DrawLine(p, x, -0.1F, x, 0.1F)
Next x
.DrawLine(p, 0, -axlong, 0, axlong)
For y = -axlong To axlong Step 0.5
.DrawLine(p, -0.1F, y, 0.1F, y)
Next y

' Draw the line

p.Color = Color.Red
p.Width = 0.05

.DrawLine(p, 0, 0, corx, cory)

End Using

End With
End Sub

Thank you very much for your advice
 
What I want is a Cartesian graph showing at Mousehover.
It happens that ToolTip cannot show a graph, and I do not know if a graph can be embed in a picturebox, so I suppose I have to show a form with the Cartesian graph within.
Unless you do suggest an easiest way of doing it.

Thank you very much for your help.
 
Back
Top