0

I'm working in a winform where I created a label in a panel. As I append text words, how do I cause the label to go to the next line when the text fills the width of the panel?

I tried the following code but it doesn't look right

        int lbl= 150;
        if (Lbl_full_list.Width > o)
        {
            my_Lbl.Text += "\n" + comboBox1.Text;
            o += 150;
        }
        else
        {
             my_Lbl.Text.Text += " , " + comboBox1.Text;
        }
InBetween
  • 32,319
  • 3
  • 50
  • 90
naf-naf
  • 103
  • 1
  • 4
  • 8
  • 1
    Here is a similar thread. http://stackoverflow.com/questions/1204804/word-wrap-for-label-in-winforms – CharithJ Jul 27 '11 at 11:05
  • I saw this before and was not successful when I tried it. Could you please show me an example? Thank you! – naf-naf Jul 27 '11 at 11:25
  • @naf-naf just checked the thread (posted above) scroll a bit down there. look at the answer with most votes. `MaximumSize` property is a way to do it – Nika G. Jul 27 '11 at 11:33

2 Answers2

2

The easiest solution is to:

  1. Set my_Lbl.AutoSize to false.
  2. Set the docking mode of my_Lbl in the containing panel to Fill.

Now my_Lbl will automatically start a new row when a line of text exceeds the label's width. Plus you get dynamic layout when the containing Panel resizes.

Note that you should create a dedicated Panel for this purpose. If you have more Controls inside the containing Panel you are currently using just create a new one only for this purpose (containing my_Lbl).

InBetween
  • 32,319
  • 3
  • 50
  • 90
0

There is MaximumSize (Width, Height) property. set it according to the needs. Also leave the autosize to be true.

Nika G.
  • 2,374
  • 1
  • 17
  • 18