请问C#中如何设置TEXTBOX自动换行,比如我想输入10个字节之后让他自动换行,
MUTIPLYLINE已经设置为TRUE,有这个算法的例子吗?
谢谢
MUTIPLYLINE已经设置为TRUE,有这个算法的例子吗?
谢谢
解决方案 »
- 求一个分析"类" 的正则.顺便散分.升四角了
- udpclient.send()和udpclient.client.sendto()有什么区别
- 如何在下拉菜单上右击,然后出现一个上下文菜单?
- 我做的winform软件无故刷屏很厉害,是因为控件用得太多?
- 验证码识别:500元可以求到高人帮忙提供下面验证码源码或指导一下吗?
- 动态生成LinkButton事件的执行,请大侠帮忙。
- Winform下的html编辑器
- calendar周的算法
- 技巧问题,怎么在RichTextBox中显示出和MSN传输文件时的进度条的那种效果
- 各路高手请进!
- XElement.Value 把 "\" 变 "\\",怎么解决?
- 谁有实现adsl宽带自动拨号断开的C#源码?
textbox.text = "sdsadasds\\n asdas";
textbox换行
textbox.text = "sdsadasds\r\n asdas";
textbox换行
this.GetWordWrappedText(this.textBox)会把自动折行转成硬回车。using System.Runtime.InteropServices; [DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", CharSet=CharSet.Unicode)]
static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, StringBuilder lParam);
const int EM_GETLINE = 0xc4;
const int EM_GETLINECOUNT = 0xba; string GetWordWrappedText(TextBoxBase textBox)
{
int count = SendMessage(textBox.Handle, EM_GETLINECOUNT, 0, 0);
string[] lines = new string[count];
for (int i = 0; i < count; ++i)
lines[i] = this.GetLine(textBox, i);
return string.Join("\r\n", lines);
} string GetLine(TextBoxBase textBox, int index)
{
StringBuilder buffer = new StringBuilder("\uffff".PadRight(0xffff));
int count = SendMessage(textBox.Handle, EM_GETLINE, index, buffer);
return buffer.ToString(0, count);
} private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.GetWordWrappedText(this.textBox1));
}
最后再返回值,传给textbox.Text即可。
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (len != 10)
{
len += 1;
}
else
{
SendKeys.Send("{Enter}");
len = 0;
}
}
{
if (textBox1.Lines[textBox1.Lines.Length - 1].Length == 10)
{
textBox1.AppendText(Environment.NewLine);
}
}试试这个哈~,看能否达到你要求~[align=center]********************************************************
本内容用 CSDN小秘书 回复
每天回帖即可获得10分可用分!
********************************************************
[/align]
一种是不改变文本内容:设置换行属性
textBox1.Multiline = true;//多行显示
textBox1.WordWrap = true; //文字断行显示
textBox1.Width = 15;//这里设置显示10个文字的宽度像素
第二种是,在相应的文本中加入换行的文字转义符如在第10个字符后面加入"\r\n"之类的
本内容用 CSDN小秘书 回复
每天回帖即可获得10分可用分!
********************************************************
[/align]
private void textBox1_TextChanged(object sender, EventArgs e)
{
int intChar = 3;
string strNewTextBox = "";
string strTextBox = this.textBox1.Text;
int abc = this.textBox1.SelectionStart;
if (strTextBox != "")
{
if (strTextBox.Contains("\r\n"))
{
strTextBox = strTextBox.Replace("\r\n", "");
}
if (strTextBox.Length > intChar)
{
int intLine = Convert.ToInt32(Math.Ceiling((decimal)strTextBox.Length / (decimal)intChar));
for (int i = 0; i < intLine - 1; i++)
{
strNewTextBox += strTextBox.Substring(i * intChar, intChar) + "\r\n";
}
strTextBox = strNewTextBox + strTextBox.Substring((intLine - 1) * intChar);
}
}
this.textBox1.Text = strTextBox; if (abc == textBox1.Text.Length || abc == 0)
{
textBox1.Focus();
textBox1.Select(textBox1.TextLength, 0);
}
}
我写了这么个方法,默认为3个字节的长度换行 现在有2个问题
1。无法使用回车
2。在字符串中间部分(比如我输入了5行 我要在第二行加一个字)操作的时候光标自动移动到最后的处理望高人指点
{
if (textBox2.Text.Length % 10==0)
{
textBox2.Multiline = true;
textBox2.WordWrap = true;
textBox2.AppendText(Environment.NewLine);
}
}
其实问题仍然没有很好的解决,因为用"\r\n"能够满足Windows的要求了,但是如果是其他平台怎么办?为了要确保让换行效果在各种平台上都能够正常的显示,请用Environment.NewLine。它可以确保在不同的平台下都能够返回正确的换行字符,在Windows下是\r\n,在Linux(Mono)下就应该是\n了。 所以上面的代码应该写成:
aTextBox.Text = "First Line" + Environment.NewLine + "Second Line" + Environment.NewLine + "Third Line";
另外C# TextBox换行你也可以用verbatim string literal
(用@开始的字符串)的形式来输入换行符:
aTextBox.Text = @"First Line Second Line Third Line";
这种形式在代码[dai ma]中看起来很直观,但是如果代码编辑器是在Windows下运行的,仍然相当于输入了\r\n。Environment是一个静态类,位于System名称空间下,提供对当前程序的运行环境和平台的相关信息的访问。它提供了很多有用的静态属性和方法。