Dynamic positioning

Cassio

Junior Contributor
Joined
Nov 30, 2002
Messages
276
Location
Rio de Janeiro
Hi! I have some checkboxes that get their text from a xml file, it works good. But the problem is that sometimes the text is small and sometimes its long, so i want to change the position so that one checkbox start where the previews one end. That could be easy to do if checkboxes had an AutoSize property but it doesnt have.
Is there an easy way out or ill have to check the text length and then code the checkbox size and position?

Thanks!
 
You can use the Graphics.MeasureString function to get the size of
text.... something like this:
Visual Basic:
    Private Function TextWidth(ByVal text As String) As Integer
        Dim g As Graphics

        Return g.MeasureString(text, New Control().Font).Width
    End Function
If your checkboxes use a non-standard font, replace the bit
New Control().Font with myCheckbox.Font or whatever.
Otherwise it just uses the default control font.
 
I would use Me.Font instead, otherwise you might end up creating a new font every time that function is run (and not disposing it either).
 
That's true, but if you wanted to place the function in a class, rather
than a form (I prefer to stay away from putting functions inside
forms, unless they are form-specific), then you would need to create
a new Control and get the font. Afterwards you can destory the
Control and all is well again.

Still, your way would work better within a form.
 
I assume as he's adding checkboxes that he's going to be executing the code within a form. Also he'll need a graphics handle before executing that function (I noticed you didn't instantiate g) using the form's CreateGraphics method. :)
 
Did you guys consider if the text was so long in length the text wraps to a second line? The width of the caption would no longer be relative.
 
Unless I missunderstood the question entirely, wouldn't this work...
Visual Basic:
CheckBox1.Text = "This is the text inside checkbox1 and it's quite long"
CheckBox2.Top = CheckBox1.Top
CheckBox2.Left = CheckBox1.Left + CheckBox1.Width + 10
 
Yes, but if you want all the checkboxes' text to be on one line (without
word-wrapping), then you need to use the MeasureString way to
actually set the widths of the checkboxes.

With Robby's way, the controls might be laid out like:
Code:
[size=1][ ] Check1         [ ] Another Check   [ ] Yet another    [ ] More[/size]
rather than:
Code:
[size=1][ ] Check 1 [ ] Another Check [ ] Yet another [ ] More[/size]
 
Yes, but the width of the previous checkbox might not necessarily
be equal to the width of the text within.

In fact, now that I think about it more, your way is indeed
what is needed no matter what method you use, but you need to
set the Width of each Checkbox beforehand using the MeasureString, so the width of the Checkbox is the same as the
width of the text (plus the size of the check-box itself).
 
Eventually it will be disposed by the garbage collector, but Font objects use win32 GDI handles so it's best to call their Dispose methods manually when you're done with them, same with brushes and any other class which uses win32 resources.
 
I usaually use the Object=nothing without being sure.

But I just check the ms-help on the 'nothing' keyword and it states

"When you assign Nothing to an object variable, it no longer refers to any object instance. If the variable had previously referred to an instance, setting it to Nothing does not terminate the instance itself. The instance is terminated, and the memory and system resources associated with it are released, only after the garbage collector detects there are no active references remaining.

Its pretty confusing. How do I get rid of an object for good????
 
Hey, thanks for the reply. I guess I didnt make my self very clear. I want the checkboxes to be displayed vertically, but some texts will need more than one line so i guess its the same idea but ill use the height property. Right?
 
Last edited:
Private Function TextWidth(ByVal text As String) As Integer
Dim g As Graphics

Return g.MeasureString(text, New Control().Font).Width
End Function

Im trying to do that but i keep recieving the Reference Null Error. I guess its because of the 'g' object.
 
Im not sure what you need but try this:

Visual Basic:
Dim size As System.Drawing.SizeF
size = Me.CreateGraphics.MeasureString("ggggg", New Control().Font)
MsgBox(size.toSize.ToString())

This will show you the width and height of of the text.
 
Back
Top