4

In windows forms I have checkbox list that contains long text, the form is re-sizable..

can I make the text wraps automatically in run-time according to the form width ?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Emad Khalil
  • 803
  • 1
  • 8
  • 14
  • If you are not afraid of making custom control, then sure you can do everything. One possibility, however, can be easy: measure text and put new lines yourself (idea stolen from [this](http://stackoverflow.com/q/5937637/1997232) answer). – Sinatr Oct 27 '14 at 13:25
  • @Sinatr I can go with custom control, but most of the solutions I found wraps the text at the beginning while initializing the control, then got fixed forever even when i change the form width. – Emad Khalil Oct 27 '14 at 13:36
  • Thanks for your help, but the text comes dynamically from database, I can't modify it @Sinatr – Emad Khalil Oct 27 '14 at 13:40
  • _I have checkbox list_. Because you mentioned that text of checkboxes coming from database, maybe `DataGridView` with `CheckBoxColumn` and `TextBoxColumn` will be better approach. `DataGridViewCellStyle` have a property `WrapMode` – Fabio Oct 30 '14 at 05:06

3 Answers3

9

You can by using:

  1. set 'autosize' to 'false'

  2. change the width of 'MaximumSize' to the label width you want, say '70'

  3. change the height of 'MinimumSize' to the label height you want, say '30'

Joseph Wu
  • 4,786
  • 1
  • 21
  • 19
  • I was using a table layout panel as container, and set the AutoSize property of the checkbox to true. Changing that fixed it. – Adam L. S. Jul 27 '21 at 05:55
2

To make the Text wrap you need to make the AutoSize property false and allow for a larger Height:

checkBox1.AutoSize = false;
checkBox1.Height = checkBox1.Height * 3; // or however many lines you may need

// style the control as you want..
checkBox1.CheckAlign = ContentAlignment.TopLeft;
checkBox1.TextAlign = ContentAlignment.TopLeft;
checkBox1.Anchor = AnchorStyles.Right;

checkBox1.Text = "12321312231232 13189892321 312989893123 ";

You will need to think about the vertical layout..

Maybe a FlowLayoutPanel will help there or maybe you want to measure the size needed by using Graphics.MeasureString or TextRenderer.MeasureText(String, Font, Size)!

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Thanks a lot, but i think this will wrap the text while initializing the control, then got fixed forever, but what i want to do is automatically adjust the wrapping during the run time of the application if the user changes the width of the form – Emad Khalil Oct 27 '14 at 14:44
  • In that case you will also need to anchor the Checkbox, but then it'll adapt the wrapping dynamically.. I have added the necessary line.. – TaW Oct 27 '14 at 18:10
  • You don't actually need to change CheckAlign, TextAlign and Anchor. Those properties only have to do with aesthetics (CheckAlign and TextAlign) and dynamic layout (Anchor). What's more, if you don't anchor to the left, your checkbox won't be resized when the parent's width increases. – Simone May 23 '17 at 08:43
  • You are right but I didn't actually say those are needed; I do mention them as examples of how to style the control. Which kind of anchoring is wanted is always up the the designer.. – TaW May 23 '17 at 09:13
-1

I tried many approaches but at last this one worked after a lot of researches:-

simply I used two events to detect size change of the panel contains the controls then I adjusted the controls accordingly..

first event is LayoutEventHandler, second event for detecting resolution change

in these events:-

1- get the panel width considering the resolution (lower accepted resolution is 1024x768)

  Rectangle resolution = Screen.PrimaryScreen.Bounds;
  int panelWidth *= (int)Math.Floor((double)resolution.Width / 1024);

2- loop on all controls and adjust the control width to fit the panel width (i subtracted 10 pixels for vertical scroll width), then I got the control height from MeasureString function which takes the control text, the font and control width, and return the control size.

(i.e. I multiplied the height with a roughly factor "1.25" to overcome line height and padding)

  foreach (var control in controls)
  {
      if (control is RadioButton || control is CheckBox)
      {
             control.Width = panelWidth - 10;

             Font fontUsed = control.Font;
             using (Graphics g = control.CreateGraphics())
             {
               SizeF size = g.MeasureString(control.Text, fontUsed, control.Width);
              control.Height = (int)Math.Ceiling(size.Height * 1.25);
            }
       }
  }
Community
  • 1
  • 1
Emad Khalil
  • 803
  • 1
  • 8
  • 14
  • 1
    This technique is unnecessarily implementing the dynamic layout "by hand", where there are more proficient ways, like anchors or docking. – Simone May 23 '17 at 08:46