Cassio Posted March 30, 2003 Posted March 30, 2003 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! Quote Stream of Consciousness (My blog)
Moderators Robby Posted March 30, 2003 Moderators Posted March 30, 2003 Maybe use Left + Width + 10 (of the first control) to set the location of the second control. Quote Visit...Bassic Software
Cassio Posted March 30, 2003 Author Posted March 30, 2003 But how can I set the size of each checkbox according to the length of its text? Is there any AutoSize property? Quote Stream of Consciousness (My blog)
*Experts* Volte Posted March 30, 2003 *Experts* Posted March 30, 2003 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 FunctionIf 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. Quote
Cassio Posted March 30, 2003 Author Posted March 30, 2003 Thanks. Ill try that. Quote Stream of Consciousness (My blog)
*Gurus* divil Posted March 30, 2003 *Gurus* Posted March 30, 2003 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). Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Volte Posted March 30, 2003 *Experts* Posted March 30, 2003 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. Quote
*Gurus* divil Posted March 30, 2003 *Gurus* Posted March 30, 2003 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. :) Quote 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 Robby Posted March 30, 2003 Moderators Posted March 30, 2003 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. Quote Visit...Bassic Software
Moderators Robby Posted March 30, 2003 Moderators Posted March 30, 2003 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 Quote Visit...Bassic Software
*Experts* Volte Posted March 30, 2003 *Experts* Posted March 30, 2003 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] Quote
Moderators Robby Posted March 30, 2003 Moderators Posted March 30, 2003 Actualy what I had in mind was to place each checkbox relative to the previous (checkbox's) position. Quote Visit...Bassic Software
*Experts* Volte Posted March 30, 2003 *Experts* Posted March 30, 2003 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). Quote
Ariez Posted March 30, 2003 Posted March 30, 2003 Objects end life after they leave scope so the New Control().Font is disposed. Am I wrong? Quote Auto-suggestion: "I have a life" Uncontroled thinking: "So what the.."
*Gurus* divil Posted March 30, 2003 *Gurus* Posted March 30, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Ariez Posted March 30, 2003 Posted March 30, 2003 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???? Quote Auto-suggestion: "I have a life" Uncontroled thinking: "So what the.."
Ariez Posted March 30, 2003 Posted March 30, 2003 oh ok... Do u often use a destructor for your own objects too? Quote Auto-suggestion: "I have a life" Uncontroled thinking: "So what the.."
Cassio Posted March 30, 2003 Author Posted March 30, 2003 (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 March 30, 2003 by Cassio Quote Stream of Consciousness (My blog)
Cassio Posted April 1, 2003 Author Posted April 1, 2003 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. Quote Stream of Consciousness (My blog)
Guest mutant Posted April 1, 2003 Posted April 1, 2003 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. Quote
Cassio Posted April 1, 2003 Author Posted April 1, 2003 Thanks it works fine. Quote Stream of Consciousness (My blog)
*Gurus* divil Posted April 1, 2003 *Gurus* Posted April 1, 2003 That's a nasty way of doing it. You should create the graphics object before calling your function however many times you need to (passing your graphics object to it) and then properly dispose it afterwards. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.