r/visualbasic • • May 19 '20

Unexpected label drifting

I have a grid based map plotting program. A bunch of pictures boxes that can be loaded with stuff on click. When it was small I would just drag out the PBs and labs (a b c, 1 2 3), but it's gotten so big that I'm now programmatically adding pbs and labels

Dim locx as int = 1
Dim locy as int = 1
For I as int = 0 to 24
      Dim pb as new picture box
      With pb
           Pb.location = new point(25 * locx, 25 * locy)
           Pb.height  = 25
           Pb.width = 25
       End with
       Me.gridpan.controls.add(pb)
      If locx > 4 then
          Locx =0
          Locy +=1
     Else if locx <5 then
          Locx += 1
     End if
 Next

This works great. All the squares are flush next to eachother in a 5x5 grid. I add the same thing for the x labels

Locx = 1
Locy = 0

For I as int = 0 to 24
      Dim titlex as new label
      With titlex
           Titlex.location = new point(37.5 * locx, 25 * locy)
           Titlex.height  = 25
           Titlex.width = 25
       End with
       Me.gridpan.controls.add(titlex)
       Locx += 1
 Next

The first few labels look good, but as a whole they keep drifting further and further and further out of alignment with the picture boxes below them

5 Upvotes

6 comments sorted by

1

u/redcorn89 May 19 '20

Do the labels get the same text in them? If not, setting the auto size property of the label to false. It’s true by default and will override the sizes you are assigning.

titlex.AutoSize = False

Also when you use with titlex you do not have to retype titlex in front of each line (On Mobile so hopefully formatting turns out correct)

Locx = 1
Locy = 0

For i as int = 0 to 24

  Dim titlex as new label
  With titlex
      .location = new point(37.5 * locx, 25 * locy)
      .height  = 25
      .width = 25
      .AutoSize = False
   End with
   Me.gridpan.controls.add(titlex)
   Locx += 1
Next

1

u/Thunor_SixHammers May 19 '20

Unfortunately this didn't fix it.

They all have pretty much the same thing in them, a single alphabet charcater.

1

u/redcorn89 May 19 '20

Try changing the 37.5 that you are multiplying the x location by to either 37 or 38. Location coordinates need to be integers so every odd row will end with a .5 that will be rounded. So essentially your x coordinates are spaced by 38, 37, 38, 37....

I also just noticed that your picture boxes are both multiplied by 25 for x and y but the labels are 37.5 for x and 25 for y. Should the labels also be 25 for x?

1

u/Thunor_SixHammers May 19 '20

The discrepancy in x was to try and center the label over the text box. Each box is 25*25, so

The first box would go from 25 to 50, so the mid point would be at 25+12.5(37.5).

I think I have solved it though:

I set the label border style to something visible to see if the edges were aligned. They were, this some how made the text more prone to fall within the square. Then I centered the text on each label and now it works fine

1

u/redcorn89 May 19 '20

Nice

1

u/nice-scores May 24 '20

𝓷𝓲𝓬𝓮 ☜(゚ヮ゚☜)

Nice Leaderboard

1. u/spiro29 at 8653 nices

2. u/RepliesNice at 7890 nices

3. u/Manan175 at 7096 nices

...

240872. u/redcorn89 at 1 nice


I AM A BOT | REPLY !IGNORE AND I WILL STOP REPLYING TO YOUR COMMENTS