文本框:文本换行到多行 [英] TextBox : Wrapping text to multiple lines

查看:233
本文介绍了文本框:文本换行到多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框在我的形式,并试图将内容包装成多行,这样它不会砍掉。

I have a TextBox in my form and trying to wrap the content into multiple lines so that it doesn't get chopped off.

到目前为止,我这样做,

So far i did this,

TextBox myTextBox = new TextBox();
myTextBox.Multiline = true;
myTextBox.WordWrap = true;



我试过以上各种组合,由图灵多行为false,然后自动换行设置为false。

I tried various combinations of above, by turing Multiline to false and then WordWrap to false.

但似乎没有任何工作。

我发现有一个TextWrapping财产太多,但出于某种原因,这不是供我的文本框,可能是美元,因为不同的.NET版本b $ b

I found that there's a TextWrapping property too, but for some reason that's not available for my TextBox, probably because of a different .Net version.

有没有办法到文本框的内容包装成多行?

Is there a way to wrap the content of a TextBox into multiple lines?

推荐答案

你是什么内容?它是一个人类可读的字符串(如正常的单词和句子),或者是一个十六进制字符串等。

What is your content? Is it a "human readable" string (e.g. 'normal' words and sentences), or is it a hexadecimal string etc.?

根据你的内容,你有多个选项:

Depending on your content, you have multiple options:


  1. 插入 \\\
    经过一定数目的字符

  2. 根据您的框架(WPF中,的WinForms等),如果您正在使用WPF使用不同的属性/实现.....


    ,尝试 AcceptReturn =真TextWrapping =总结


    如果你正在使用的WinForms,请尝试将一些 \\ \
    -characters,当 .Multiline:= TRUE .WordWrap:= TRUE

  1. Insert a \n after a certain amount of characters
  2. Depending on your framework (WPF, WinForms etc.) use different properties / implementations.....
    If you are using WPF, try AcceptReturn="true" TextWrapping="Wrap"
    If you are using WinForms, try inserting some \n-characters , when .Multiline := true and .WordWrap := true

附录:如果您要插入 \\\
每个 X 人物,我对你下面的代码片段(我很喜欢)后:


ADDENDUM: If you want to insert a \n after every x characters, I have the following snippet for you (which I quite like):

using System.Text.RegularExpressions;

...

string mystr = "this is my very long text";

mystr = Regex.Replace("(.{20})", "$1\n");

TextBox1.Text = mystr; // or: TextBox1.Content = mystr;



20 的正则表达式里面( {20})你的角色的ammount的是,在这之后 \\\
将被插入。的(意思是,每20个字符后你的字符串将有一个新行)

Where the 20 inside the Regex "(.{20})" your ammount of characters is, after which a \n will be inserted. (meaning, that your string will have a new line after every 20 characters)

这篇关于文本框:文本换行到多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆