简体   繁体   English

每行的列表框字符数限制

[英]Listbox character limit per line

I have a listbox in my windows form application that shows quite long texts. 我的Windows窗体应用程序中有一个列表框,显示了很长的文本。 Since texts are so long, user have to use the horizontal slider for check the rest of text. 由于文本太长,用户必须使用水平滑块来检查其余文本。

So, I want to limit listbox character per line. 因此,我想限制每行的列表框字符。 For every 50 char it should go to next row, so user won't have to use glider. 每50个字符应转到下一行,因此用户不必使用滑翔机。

I can't put "new line" since text source is Sql Database. 因为文本源是Sql数据库,所以我不能放“换行”。

My code is basically this: 我的代码基本上是这样的:

listbox1.items.add(dbRead["LongText"]); // dbRead = SqlDataReader

So I have to edit listbox itself. 所以我必须编辑列表框本身。 I've checked it a bit, but didn't manage to find. 我检查了一下,但没有找到。 I've also tried to find an event like when text is changed, for every 50 char listbox.items.add("") etc. I'm still alien to syntax. 我还尝试为每个50个字符的listbox.items.add("")等查找一个事件,如更改文本时。我仍然与语法无关。

Any suggestions ? 有什么建议么 ?

You can write an extension method( SplitByLength ) like below 您可以编写一个扩展方法( SplitByLength ),如下所示

var input = "I have a listbox in my windows form application that shows quite long texts. Since texts are so long, user have to use the horizontal slider for check the rest of text.\nSo, I want to limit listbox character per line. For every 50 char it should go to next row, so user won't have to use glider.";
var lines = input.SplitByLength(50).ToArray();
listBox1.Items.AddRange(lines);

public static partial class MyExtensions
{
    public static  IEnumerable<string> SplitByLength(this string input, int maxLen)
    {
        return Regex.Split(input, @"(.{1," + maxLen + @"})(?:\s|$)")
                    .Where(x => x.Length > 0)
                    .Select(x => x.Trim());
    }
}

----------EDIT---------- - - - - - 编辑 - - - - -

After tinstaafl's comment, an edit seems to be a must 在tinstaafl发表评论后,似乎必须进行编辑

var input = "I have a listbox in my windows form application that shows quite long texts. Since texts are so long, user have to use the horizontal slider for check the rest of text.\nSo, I want to limit listbox character per line. For every 50 char it should go to next row, so user won't have to use glider.";
input = String.Join(" ", Enumerable.Repeat(input, 100));

var t1 = Measure(10, () =>
{
    var lines = input.SplitByLength_LB(50).ToArray();
});

var t2 = Measure(10, ()=>
{
    var lines = input.SplitByLength_tinstaafl(50).ToArray();
});

long Measure(int n,Action action)
{
    action(); //JIT???
    var sw = Stopwatch.StartNew();
    for (int i = 0; i < n; i++)
    {
        action();
    }
    return sw.ElapsedMilliseconds;
}

public static partial class MyExtensions
{
    public static  IEnumerable<string> SplitByLength_LB(this string input, int maxLen)
    {
        return Regex.Split(input, @"(.{1," + maxLen + @"})(?:\s|$)")
                    .Where(x => x.Length > 0)
                    .Select(x => x.Trim());
    }

    public static IEnumerable<string> SplitByLength_tinstaafl(this string input, int maxLen)
    {
        List<string> output = new List<string>();
        while (input.Length > 0)
        {
            output.Add(new string(input.Take(maxLen).ToArray()));
            input = new string(input.Skip(maxLen).ToArray());
        }
        return output;
    }
}

And my results are different than yours: 11 ms. 我的结果与您的结果有所不同: 11毫秒。 vs. 3384 ms. 与3384毫秒。 :) :)

Redid my code to take into account spaces. 重命名我的代码以考虑空间。 With variable length lines some shorter some longer than 50 characters and the line breaks adjusted for spaces, I found that the performance is very close to the same. 对于可变长度的行,一些短于50个字符,并且换行符针对空格进行了调整,我发现性能非常接近。 They're both between 15 and 25 milliseconds on 1000 strings. 它们在1000个字符串上都在15到25毫秒之间。 Though regex does perform moderately faster. 尽管regex的确确实表现得更快。 Here's the code I used: 这是我使用的代码:

public static partial class MyExtensions
{
    public static IEnumerable<string> SplitByLength_LB(this string input, int maxLen)
    {
        return Regex.Split(input, @"(.{1," + maxLen + @"})(?:\s|$)")
                    .Where(x => x.Length > 0)
                    .Select(x => x.Trim());
    }
    public static IEnumerable<string> SplitByLength_tinstaafl(this string input, int maxLen)
    {
        List<string> output = new List<string>{""};

        string[] temp = input.Split("\n ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        for(int i = 0; i < temp.Count(); i++)
        {
            if((output.Last() + " " + temp[i]).Length > 50)
            {
                output.Add(temp[i]);
            }
            else
                output[output.Count() - 1] += " " + temp[i];
        }
        return output;
    }
        return output;
    }

The test is like this: 测试是这样的:

        Stopwatch s1 = new Stopwatch();
        List<string> source = new List<string>();
        Random rnd = new Random();
        for(int i = 0; i < 1000; i++)
        {
            var input = "I have a listbox in my windows form application that shows quite long texts. Since texts are so long, user have to use the horizontal slider for check the rest of text. So, I want to limit listbox character per line.";
            int nextbreak = rnd.Next(20, input.Length);
            source.Add(new string(input.TakeWhile((x, y) => input.IndexOf(' ', y) <= nextbreak).ToArray()));
        }
        s1.Start();
        List<string> output = new List<string>(from s in source
                                               from p in s.SplitByLength_LB(50)
                                               select p);
        s1.Stop();
        Console.WriteLine("SplitByLength_LB\t" + s1.ElapsedMilliseconds.ToString());
        s1.Reset();
        s1.Start();
        List<string> output2 = new List<string>(from s in source
                                                from p in s.SplitByLength_tinstaafl(50)
                                                select p);
        s1.Stop();
        Console.WriteLine("SplitByLength_tinstaafl\t" + s1.ElapsedMilliseconds.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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