Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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!

  • *Experts*
Posted

You can use the Graphics.MeasureString function to get the size of

text.... something like this:

    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.

  • *Experts*
Posted

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.

  • *Gurus*
Posted
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. :)

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • Moderators
Posted

Unless I missunderstood the question entirely, wouldn't this work...

CheckBox1.Text = "This is the text inside checkbox1 and it's quite long"
CheckBox2.Top = CheckBox1.Top
CheckBox2.Left = CheckBox1.Left + CheckBox1.Width + 10

Visit...Bassic Software
  • *Experts*
Posted

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:

[size=1][ ] Check1         [ ] Another Check   [ ] Yet another    [ ] More[/size]

rather than:

[size=1][ ] Check 1 [ ] Another Check [ ] Yet another [ ] More[/size]

  • *Experts*
Posted

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).

Posted

Objects end life after they leave scope so the New Control().Font is disposed.

Am I wrong?

Auto-suggestion: "I have a life"

Uncontroled thinking: "So what the.."

  • *Gurus*
Posted
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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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????

Auto-suggestion: "I have a life"

Uncontroled thinking: "So what the.."

Posted (edited)
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? Edited by Cassio
Posted
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.

Guest mutant
Posted

Im not sure what you need but try this:

 

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...