Two kinds of code for line wrapping (C #)

Source: Internet
Author: User

Recently there is a need to wrap the contents of a ListBox in C # WinForm.

In fact, before using a ListBox, has been using a TextBox to achieve most of the functions, but finally there is a focus on the problem,

is the text in the TextBox will automatically be selected, that is, blue background, super ugly, the boss said no, so change the listbox to try

As you know, each item in the listbox is a record and is not automatically wrapped by default, so the ListBox has both vertical and horizontal scroll bars

Baidu and Google, after the online idea is: limit a length, the string over the length of the part of the interception, add to the next item, so that the human implementation of the ListBox automatic wrapping.

The strings that are incorporated into my requirements contain spaces, so I thought of two ways

Idea One:

1. Divide the string into separate words, and then assemble the words like a chain of pearls

2. If the length does not exceed the given length then the assembly continues ( using the Join method here ),

2.1. When a given length is exceeded, delete the last word from the list (and save it with an intermediate variable, add it to the beginning of the next line, and the initial value of the intermediate variable is empty)

See the code in detail:

1 /*2 * String wrapping, taking into account the space, and the case of the complete word3 * @param inputstr string to wrap4 * @param The fixed length of the textWidth, the line will be wrapped over this length5  *6  */7  Publiclist<string> Listboxwordwrap (stringINPUTSTR,inttextWidth)8 {9list<string> templist =Newlist<string> ();//temporarily store a list of stitching stringsTenlist<string> lastlist =Newlist<string> ();//the final data One     intStrlength = Inputstr.length;//gets the length of the string to wrap. A     if(Strlength >textWidth) -     { -         string[] Listarray = Inputstr.split (' ');//first, divide the string into words, and then reconnect them back. the         stringJoinstr =""; -         stringThedeletestr ="";//It is used to store the word that is more than the fixed length because it is added.  -          for(intj =0; J < Listarray.length; J + +) -         { +Templist.add (Listarray[j]);//Add the divided words to the list -Joinstr = String.Join (" ", Templist.toarray ());//and convert it into a string. +             //each added one is compared with a fixed length, and when it is small, continue adding; A             if(Joinstr.length >textWidth) at             { -                 //because it is larger than the fixed length, the last word is deleted, and the deleted string is a complete record . -                 intLastspaceindex = Joinstr.lastindexof (" "); -Lastlist.add ((thedeletestr+" "+joinstr.substring (0, Lastspaceindex)). Trim ()); -Thedeletestr =Listarray[j]; -                 //It's just the last one. in                 if(j = = Listarray.length-1) - Lastlist.add (THEDELETESTR); to  +Templist.clear ();//Empty the Temp list -             } the             Else if(j = = Listarray.length-1)//when traversing to the end, the rest as the last line *             { $Lastlist.add ((thedeletestr+" "+joinstr). Trim ());Panax Notoginseng templist.clear (); -             } the         } +     } A     returnlastlist; the}
View Code

Idea two:

1. Divide the string into separate words, and then assemble the words like a chain of pearls

2. If the current length is less than the given length, but after one more word, the length of the string is greater than the given length, so that it is possible to determine where the line breaks

See the code in detail:

1 /*2 * String wrapping, taking into account the space, and the case of the complete word3 * @param inputstr string to wrap4 * @param The fixed length of the textWidth, the line will be wrapped over this length5  *6  */7  Publiclist<string> ListBoxWordWrap2 (stringINPUTSTR,inttextWidth)8 {9list<string> list =Newlist<string>();Tenlist<string> lastlist =Newlist<string>(); One     stringstr =Inputstr; A     intTextWidth = -; -     if(str! =""|| str! =NULL) -     { the         intStrlength =Str. Length; -         if(Strlength >textWidth) -         { -             string[] Listarray = str. Split (' ');//first, divide the string into words, and then reconnect them back. +             stringJoinstr ="";//concatenated Strings -             stringNextstr ="";//add more than one element to the connection string +              for(intj =0; J < Listarray.length; J + +) A             { at list. ADD (Listarray[j]); -Joinstr = String.Join (" ", List. ToArray ()); -                 //the current string is smaller than the fixed length, but the next string is larger than the fixed length to determine the line break -                 if(Joinstr.length < TextWidth && J < listarray.length-1){ -List. ADD (listarray[j+1]); -Nextstr = String.Join (" ", List. ToArray ()); in                     if(Nextstr.length >textWidth) -                     { to Lastlist.add (JOINSTR); + list. Clear (); -                     } the                     Else *List. Remove (listarray[j+1]); $                 }Panax Notoginseng                 Else if(j = = Listarray.length-1) -                 { the Lastlist.add (Joinstr.trim ()); + list. Clear (); A                 } the             } +  -         } $     } $     returnlastlist; -}
View Code

Two kinds of code for line wrapping (C #)

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.