Reputation: 1
I made a quiz that stores the questions in a SQL database
and display the question in a Rich Text Box
and the answers in Check Boxes
.
But sometimes the answer is too long and I need more space to show it. I thought that a multiline
would help me, but I can't find a way to do it.
Thanks!
Upvotes: 0
Views: 3210
Reputation: 82534
Winforms checkboxes have an AutoSize
property that is set to true
by default.*
If you set it to false
, you can change the height of the checkbox and it's text would wrap automatically.
However, if the AutoSize
property is set to false
, you must to set the width and height of checkbox yourself.
*As TaW mentioned in his comment, the AutoSize
property's default is true
only when adding the checkbox in the designer. For dynamically created checkboxes the default is false
.
Upvotes: 1
Reputation: 1946
If you set the width of the checkbox you will automatically get multiline. I made a simple example of how to set the width of the control (if you want the width set hard) and then just adapting the height. Its better to set the height correct or the text might be clipped.
Try this:
private void CreateCheckBox(){
CheckBox cb = new CheckBox();
cb.Location = new Point(10, 10);
cb.Width = 150;
Controls.Add(cb);
cb.Text = "This is a long text that needs to be broken down into multiple lines.";
cb.Height = GetHeight(cb);
}
private static int GetHeight(CheckBox cb) {
using (Graphics g = cb.CreateGraphics()) {
var size = g.MeasureString(cb.Text, cb.Font);
int rows = (int)Math.Ceiling(((double) size.Width/cb.Width));
return (int)(rows*size.Height);
}
}
Upvotes: 0