请问C#中如何设置TEXTBOX自动换行,比如我想输入10个字节之后让他自动换行,
MUTIPLYLINE已经设置为TRUE,有这个算法的例子吗?
谢谢

解决方案 »

  1.   

    label.text = "asdasdasa,<br>sdsds";label换行
    textbox.text = "sdsadasds\\n asdas";
    textbox换行
      

  2.   

    有个 wordwrap 的属性..设置 true 就行了 到边缘自动换行
      

  3.   

    label.text = "asdasdasa,<br>sdsds";label换行
    textbox.text = "sdsadasds\r\n asdas";
    textbox换行
      

  4.   

    10个字节啊。应该把框自己拖成10个字节那么宽。然后MUTIPLYLINE已经设置为TRUE吧。
      

  5.   

    要用到p/invoke。以下为代码。
    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));
            }
      

  6.   

    写个函数,判断每隔10字符时,就加入\n。
    最后再返回值,传给textbox.Text即可。
      

  7.   

    一个比较歪的方法,功能可以实现,你可以参考一下:        int len = 1;        
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (len != 10)
                {
                    len += 1;
                }
                else
                { 
                    SendKeys.Send("{Enter}");
                    len = 0;
                }
            }
      

  8.   

    这个如果我在里面删除什么 也要len也要+1了 好像行不通
      

  9.   

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    if (textBox1.Lines[textBox1.Lines.Length - 1].Length == 10)
    {
    textBox1.AppendText(Environment.NewLine);
    }
    }试试这个哈~,看能否达到你要求~[align=center]********************************************************
    本内容用 CSDN小秘书 回复
    每天回帖即可获得10分可用分!
    ********************************************************
    [/align]
      

  10.   

    其实有两种方式可以实现:
    一种是不改变文本内容:设置换行属性
    textBox1.Multiline = true;//多行显示
    textBox1.WordWrap = true; //文字断行显示
    textBox1.Width = 15;//这里设置显示10个文字的宽度像素
    第二种是,在相应的文本中加入换行的文字转义符如在第10个字符后面加入"\r\n"之类的
      

  11.   

    那可以手动输入换行么??[align=center]********************************************************
    本内容用 CSDN小秘书 回复
    每天回帖即可获得10分可用分!
    ********************************************************
    [/align]
      

  12.   


            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行 我要在第二行加一个字)操作的时候光标自动移动到最后的处理望高人指点
      

  13.   

    有个 wordwrap 的属性..设置 true 就行了 到边缘自动换行然后把textbox拉到指定大小
      

  14.   

    \n是没法自动换行的,只能给Lines一个一个赋值,Lines是个string[]
      

  15.   

           private void textBox2_TextChanged(object sender, EventArgs e)
            {
                if (textBox2.Text.Length % 10==0)
                {
                    textBox2.Multiline = true;
                    textBox2.WordWrap = true;
                    textBox2.AppendText(Environment.NewLine);
                }
            }
      

  16.   

    C# TextBox换行运行在Windows上。Windows能够显示的换行必须由两个字符组成:carriage return & line feed,也就是必须是"\r\n"。如果只是"\n"在Windows中不能显示为换行的,这与Linux/Unix等其他的操作系统不一样。所以上边如果把"\n"替换成"\r\n"就可以了。
    其实问题仍然没有很好的解决,因为用"\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名称空间下,提供对当前程序的运行环境和平台的相关信息的访问。它提供了很多有用的静态属性和方法。
      

  17.   

    style="word-wrap:break-word;word-break:keep-all;"加这么一句,然后调整宽度就可以一到边缘就自动换行了