我的Windows窗体应用程序中有一个列表框,显示了很长的文本。由于文本太长,用户必须使用水平滑块检查文本的其余部分。
所以,我想限制每行的列表框字符。对于每50个字符,它应该进入下一行,因此用户不必使用滑翔机。
我不能把“新行”放在文本源是Sql Database。
我的代码基本上是这样的:
listbox1.items.add(dbRead["LongText"]); // dbRead = SqlDataReader
所以我必须编辑列表框本身。我已经检查了一下,但没有设法找到。我还试图找到一个事件,例如文本被更改时,每50个字符listbox.items.add("")
等等。我仍然不熟悉语法。
有什么建议吗?
答案 0 :(得分:3)
您可以编写一个扩展方法( 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 强>
在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;
}
}
我的结果与你的不同: 11毫秒。与3384毫秒。:)
答案 1 :(得分:1)
重新编写代码以考虑空格。对于可变长度的线条,一些长度超过50个字符,并且线条间隔调整为空格,我发现性能非常接近相同。它们在1000个字符串上都在15到25毫秒之间。尽管正则表达式确实表现得更快。这是我使用的代码:
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;
}
测试是这样的:
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());