简体   繁体   中英

label inside a panel in winform

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;
        }

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM