✅ How can I draw a line between labels?
I have to draw lines between city labels. But when the form loads, the labels disappear and there is no line. What can I do?
66 Replies
I think you need to get the image's graphics, not create a new one
i just noticed this error
Those are warnings, not errors!
oh yea warning sorry :when:
either way, im thinking that maybe i have to make the image some kind of grid
so that i can give x,y,a,b some coordinates
but idrk how to do that
a,b ?
for the two points
i was thinking i couldn't use X and Y cuz they are the coordinates for the first point
What do you see now in your program?
in the form?
yes
its just the map
the picture
the labels disappeared
and there's no line
disappeared? so they appeared at some point before they disappeared?
in the form they are there
but once it runs they disappear
you mean the form builder?
its the first time im working with graphics so idk what to do
the designer yea
the image is probably hiding the labels
try calling
.BringToFront()
Method on the labelsI'll try
Graphics also has a method called DrawString
You can draw text directly into the image
also it looks to me like you're resetting the label's location to 0, 0
which is the top left coordinate of the form
assuming x, y, a, b values are 0
ooh yea that makes sense
the warnings do say they assign the value 0
yup they're in the top left corner
so the first problem would be the location on the axis...
alright so i managed to make them appear in the desired positions
just copied their coordinates from the designer
but the line still isn't there
what code are you using to try to draw a line?
i have to make it so the line appears when i press the button
do you know what Form2_Paint does? or when does it get called?
last time i used it it made a drawing when the form loaded
well, put this code in the button1_click then
also according to this code ur trying to draw the line on the form itself not on the image
ooh
well not even with this change it doesnt show
but i dont get any errors or warnings
show your new code
im still thinking it might be sum related to those integers
whats ur pictureBox called, is it
pictureBox1
?yea
you sure you clicked the correct button?
yea
well now i used left and top for the labels
i had used .Location.X and .Location.Y before
i think you need to call
pictureBox1.Invalidate();
to redraw the picturebox
or .Refresh()
yup invalidate worked
but the lines is not where it should be
it should be between these two labels
Yeah the labels are using the coordinates of the form while the line is using the coordinates of the image itself
1 minute
maybe if i make the form background the image?
there is a better way
lets hear it
PictureBox has a method called PointToClient where it translates a coordinate in the image into the form's coordinate
you could try using that
how do i use it :when:
i genuinely never heard of this method
Control.PointToClient(Point) Method (System.Windows.Forms)
Computes the location of the specified screen point into client coordinates.
like this?
use
pictureBox1
instead of this
this refers to the form right?
yes
didnt work
its even worse now
what do u mean?
yeah well i was gonna tell you to actually use the image coordinates for the labels
so...
no more x and y?
or what
Point contains x and y you can actually change x and y into Point to be stored into a single variable
this code might work
Note i can't test, so i have to rely on your tests
ill see what i can do
code updated..
your label positions should be as it was as it was in this image
and use this method to convert the label's position into the position in the image
alright lemme try
IT WORKED
gad dayum
show 😄
Nice
thing is
this saturday im gonna go to some kind of contest
and idrk what subjects imma have
but im gonna have to make an app like this
not necesasrily like this one exactly
but how can I remember all this
$practice
true
either way thank you so so much