开发者

Multiline TextBox with custom word wrapping

I am beginning to program in .Net and C# and currently I am stuck. I have a very similar problem as the posting on this question at stackoverflow : C#: Multiline TextBox with TextBox.WordWrap Displaying Long Base64 String.

The response to that question was this block of code:

public IEnumerable<string> SimpleWrap(string line, int length) 
{ 
   var s = line; 
   while (s.Length > length) 
   { 
     var result = s.Substring(0, length); 
     s = s.Substring(length); 
     yield return result; 
   } 
   yield return s; 
} 

I dont know how to make use of that piece of code. CAn someone please provide me with a code snippet that uses this particular method to write text that automatically also inserts a new line. My code currently looks like this:

var length = GetMaximumCharacters(txtBxResults);
var txtWrap = SimpleWrap(stringValue, length);
foreach (string s in txtWrap)
{
    txtBxResults.AppendText(s);
}

If I use AppendText method, it simple writes all the text in one single line which I d开发者_如何学Goo not want. Any replies will be greatly appreciated.

Thanks, KK


You almost have it right, you just need to insert the newline character as well. Try

foreach (string s in txtWrap)
{
  txtBxResults.AppendText(s + Environment.NewLine);
}


Well I can't give you the exact code right now (I'll come back and post it later) but in general, what you should do is identify the index of the next comma and, if characters on the current line + that index > length of the line then append a new line before that compound. If you do that in a bucle, when it's done it should be formatted correctly, also take into account the last compound won't have (I think) a comma at the end.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜