Developed in C # WinForm Label wrap method

Many of my friends will encounter in the development of WinForm Label content to be displayed is too long, but it can not issue a new line. Here I summarize several methods for your reference.

The first is to Label the AutoSize property set to False, manually modify Label size. The advantage is due to the length of the content of the wrap, but when the content length exceeds the set size, the extra content will will not be displayed. Thus, this method is suitable for use in determining the content of the basic length of time.

The second is to set the Label Dock FILL, while AutoSize property set to False, this method can be corrected the above mentioned drawbacks, but at the same position occupied Label other controls will, affect the layout. Thus, the use of this method is best to add a Label Panel or GroupBox control.

The third is determined by the length of the content, dynamically set the size of the following specific procedures Label (Label control named Label1, content is displayed as a string str).:

 

int LblNum = str.Length; // Label content length
int RowNum = 10; // number of words per line is

 

float FontWidth = label1.Width / label1.Text.Length; // width of each character
int RowHeight = 15; // height of each row

 

int ColNum=(LblNum-(LblNum/RowNum)*RowNum)==0?(LblNum/RowNum):(LblNum / RowNum)+1;   //列数
label1.AutoSize = false;    //设置AutoSize
label1.Width = (int) (FontWidth * 10.0); // set the width of the display
label1.Height = RowHeight * ColNum; // Set the elevation

 

The number of lines corresponding to each line can make Label displayed by this method to obtain the control word, but also dynamically generated, this layout has the great advantage if it is too large for the display, which will make for a multi- out of the content can not be displayed. Therefore, the display time is too long, you can add Panel or GroupBox in the outer layer, AutoScroll set to true, so that will not be too long and lead to the expansion of Label height and affect the layout of other controls, while you can better display the full content.

Three methods has its advantages and disadvantages, did not say who is superior, only the most suitable, friends may wish to try

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/08/17/2643807.html

Guess you like

Origin blog.csdn.net/weixin_34149796/article/details/93495177