C # Label wrap method in WinForm Development

Source: Internet
Author: User

    In WinForm development, the content to be displayed by the Label is too long, but the line feed is not allowed. This article will provide you with three solutions for your selection.

 

     

    Many of my friends will encounter the problem that the Label content to be displayed is too long but cannot be changed in WinForm development. Here I have summarized several methods for your reference.

    First, set the Label AutoSize attribute to False and manually modify the Label size. the advantage is that the content will be automatically wrapped due to the length of the content, but when the content length exceeds the set size, the extra content will not be displayed. therefore, this method is suitable for determining the content length.

    The second method is to set the Dock of the Label to FILL and set the AutoSize attribute to False. This method can correct the shortcomings mentioned above, but the Label will occupy the positions of other controls at the same time, affects layout. therefore, when using this method, it is best to add a Panel or GroupBox control to the Label.

    The third is to dynamically set the Label size by judging the content length. The specific program is as follows (the Label Control name is Label1 and the string of the displayed content is str ):

    Int LblNum = str. Length; // Label content Length int RowNum = 10; // Number of words displayed per line

     

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

     

    Int ColNum = (LblNum-(LblNum/RowNum) * RowNum) = 0? (LblNum/RowNum) :( LblNum/RowNum) + 1; // Number of columns label1.AutoSize = false; // set AutoSizelabel1.Width = (int) (FontWidth * 10.0 ); // set the display width label1.Height = RowHeight * ColNum; // set the display height

    This method can control the number of words displayed in each line of the Label and dynamically generate the corresponding number of lines, which is of great benefit to typographical layout, but if there are too many content displayed, this method will make the extra content unavailable. therefore, when the display content is too long, you can add a Panel or GroupBox on the outer layer and set AutoScroll to true. This will not affect the layout of other controls because the Label height is too long, it can also better display the complete content.

    All three methods have their advantages and disadvantages. If you don't know who is the best or the best, you can try it.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.