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. For every 50 char it should go to next row, so user won't have to use glider.
I can't put "new line" since text source is Sql Database.
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.
Any suggestions ?
You can write an extension method( SplitByLength ) like below
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
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. vs. 3384 ms. :)
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. They're both between 15 and 25 milliseconds on 1000 strings. Though regex does perform moderately faster. 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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.