zoukankan      html  css  js  c++  java
  • 自动换行的两种代码(C#)

    最近有个需求,需要将C# winform中的listBox中的内容自动换行,

    其实在用listBox前,已经用textBox实现了大部分功能,可惜最后还是有个焦点的问题,

    就是textBox中的文字会自动被选中,也就是蓝背景,超难看,老板就说不行,所以改换ListBox来试一试

      如大家所知,listBox的每一个item就是一条记录,而且默认是不会自动换行的,所以listBox才同时有垂直和水平的滚动条

      百度和google一下后,网上给的思路大概是:限定一个长度,字符串超过长度的部分截取下来,添加到下一个item,这样就人为的实现了listBox的自动换行。

           结合到我的需求中 字符串是包含空格的,所以我就想了两种思路

           思路一:

               1.把字符串分割成一个个单独的单词,然后像串珍珠链子一样,一个个的单词组装起来

               2.如果长度没有超过给定长度则继续组装(这里用到join方法),

                  2.1.当超过给定长度时,把最后一个单词从列表中删掉,(并用中间变量保存下来,添加到下一行的开头,中间变量的初始值为空)

                详细看代码:

     1 /*
     2  *字符串换行,考虑到空格,以及完整单词的情况
     3  *@param inputStr 要换行的字符串
     4  *@param textWidth 固定的长度,超过这个长度则进行换行
     5  *
     6  */
     7 public List<string> listBoxWordWrap(string inputStr, int textWidth)
     8 {
     9     List<string> tempList = new List<string>();//临时存放拼接字符串的列表
    10     List<string> lastList = new List<string>();//最终的数据
    11     int strLength = inputStr.Length;//获取要换行字符串的长度
    12     if (strLength > textWidth)
    13     {
    14         string[] listArray = inputStr.Split(' ');//先把字符串分割成一个个单词,后面再重新连接
    15         string joinStr = "";
    16         string theDeleteStr = "";//用来存放因为增加了它才超过固定长度的那个单词。
    17         for (int j = 0; j < listArray.Length; j++)
    18         {
    19             tempList.Add(listArray[j]);//把分割好的单词 一个个的往list里面添加
    20             joinStr = String.Join(" ",tempList.ToArray());//然后转化成字符串
    21             //每添加一个都跟固定长度比较一下,当小的时候,继续添加;如果大于的时候进入判断
    22             if (joinStr.Length > textWidth)
    23             {
    24                 //因为大于了固定长度,所以把最后一个单词删掉,删掉后的字符串为一条完整的记录,
    25                 int lastSpaceIndex = joinStr.LastIndexOf(" ");
    26                 lastList.Add((theDeleteStr+" "+joinStr.Substring(0, lastSpaceIndex)).Trim());
    27                 theDeleteStr = listArray[j];
    28                 //刚好是最后一个的时候
    29                 if (j == listArray.Length - 1)
    30                     lastList.Add(theDeleteStr);
    31 
    32                 tempList.Clear();//清空临时list
    33             }
    34             else if (j == listArray.Length - 1)//当遍历到结尾,剩下的当做最后一行
    35             {
    36                 lastList.Add((theDeleteStr+" "+joinStr).Trim());
    37                 tempList.Clear();
    38             }
    39         }
    40     }
    41     return lastList;
    42 }
    View Code

         思路二:

               1.把字符串分割成一个个单独的单词,然后像串珍珠链子一样,一个个的单词组装起来

               2.如果当前长度小于给定长度,但再多一个单词后,字符串的长度就大于给定长度,这样就能够确定换行处

                详细看代码:

     1 /*
     2  *字符串换行,考虑到空格,以及完整单词的情况
     3  *@param inputStr 要换行的字符串
     4  *@param textWidth 固定的长度,超过这个长度则进行换行
     5  *
     6  */
     7 public List<string> listBoxWordWrap2(string inputStr, int textWidth)
     8 {
     9     List<string> list = new List<string>();
    10     List<string> lastList = new List<string>();
    11     string str = inputStr;
    12     int textWidth = 64;
    13     if (str != "" || str != null)
    14     {
    15         int strLength = str.Length;
    16         if (strLength > textWidth)
    17         {
    18             string[] listArray = str.Split(' ');//先把字符串分割成一个个单词,后面再重新连接
    19             string joinStr = "";//连接起来的字符串
    20             string nextStr = "";//再添加多一个元素的连接字符串
    21             for (int j = 0; j < listArray.Length; j++)
    22             {
    23                 list.Add(listArray[j]);
    24                 joinStr = String.Join(" ", list.ToArray());
    25                 //通过 当前字符串比固定长度小,但下一个字符串比固定长度大来判断换行处
    26                 if(joinStr.Length < textWidth && j < listArray.Length-1){
    27                     list.Add(listArray[j+1]);
    28                     nextStr = String.Join(" ",list.ToArray());
    29                     if (nextStr.Length > textWidth)
    30                     {
    31                         lastList.Add(joinStr);
    32                         list.Clear();
    33                     }
    34                     else
    35                         list.Remove(listArray[j+1]);
    36                 }
    37                 else if (j == listArray.Length - 1)
    38                 {
    39                     lastList.Add(joinStr.Trim());
    40                     list.Clear();
    41                 }
    42             }
    43 
    44         }
    45     }
    46     return lastList;
    47 }
    View Code
  • 相关阅读:
    jsp中${pageContext.request.contextPath}的意思
    Linux系统(centos)同步时间方式
    Tomcat启动报错 Failed to start component [StandardServer[8005]]解决
    有根树
    轻重链剖分/长短链剖分
    CF1389G
    9.12模拟总结
    [POI2014]HOT-Hotels加强版
    可持久/可回退化数据结构
    PA2014 Muzeum
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/4192107.html
Copyright © 2011-2022 走看看