textbox 光标位置

宇哥_ 2014-04-24 09:59:49
代码如下,直接搬过去就行,求大神帮忙看下如何让光标显示在文本最后???求代码位置

private void button1_Click(object sender, EventArgs e)
{
#region
string[] strList = textBox1.Text.Replace("\r\n ", "").Split(new string[] { "\r\n" }, StringSplitOptions.None);
StringBuilder sbd = new StringBuilder();
bool flag = true;
for (int i = 0; i < strList.Length; i++)
{
flag = true;
string strL = strList[i];
if (strL != "")
{
while (flag)
{
if (Encoding.Default.GetByteCount(strL) > 14)
{
int lenByte = 0;
string str = "";
for (int j = 0; j < strL.Length; j++)
{
string s = strL.Substring(i, 1);
if (Encoding.Default.GetByteCount(s) == 2)
{
lenByte += 2;
str += s;
}
else
{
lenByte++;
str += s;
}
if (lenByte >= 14)
{
break;
}
}
sbd.Append(strL.Substring(0, str.Length));
sbd.Append("\r\n");
strL = " " + strL.Substring(str.Length, strL.Length - str.Length);
}
else
{
flag = false;
sbd.Append(strL);
sbd.Append("\r\n");
}
}
}
}
textBox1.Text = sbd.ToString();
#endregion
}
...全文
335 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tshsktm 2014-04-27
  • 打赏
  • 举报
回复
引用 4 楼 u013789247 的回复:
事件发错了,这段代码是在textBox1_TextChanged事件中 [quote=引用 2 楼 caozhy 的回复:] textBox1.Text = sbd.ToString(); 后加上 textBox1.SelectStart = textBox1.Text.Length;
我试过了,不行。加上后输入一个字符就会换一行[/quote] 换行是因为你的代码中加入的\r\n 把总数量减1试试,应该在回车符号之前了。 textBox1.SelectStart = textBox1.Text.Length-1
宇哥_ 2014-04-24
  • 打赏
  • 举报
回复
事件发错了,这段代码是在textBox1_TextChanged事件中
引用 2 楼 caozhy 的回复:
textBox1.Text = sbd.ToString(); 后加上 textBox1.SelectStart = textBox1.Text.Length;
我试过了,不行。加上后输入一个字符就会换一行
ailsa512 2014-04-24
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
textBox1.Text = sbd.ToString(); 后加上 textBox1.SelectStart = textBox1.Text.Length;
这样不会显示光标 貌似还要加个textbox1.Focus()
threenewbee 2014-04-24
  • 打赏
  • 举报
回复
textBox1.Text = sbd.ToString(); 后加上 textBox1.SelectStart = textBox1.Text.Length;
ailsa512 2014-04-24
  • 打赏
  • 举报
回复
// 创建光标 [System.Runtime.InteropServices.DllImport("User32.dll")] private static extern bool CreateCaret(IntPtr hWnd, int hBitmap, int nWidth, int nHeight); // 设置光标位置 [System.Runtime.InteropServices.DllImport("User32.dll")] private static extern bool SetCaretPos(int x, int y); // 显示光标 [System.Runtime.InteropServices.DllImport("User32.dll")] private static extern bool ShowCaret(IntPtr hWnd); private void button1_Click(object sender, EventArgs e) { #region string[] strList = textBox1.Text.Replace("\r\n ", "").Split(new string[] { "\r\n" }, StringSplitOptions.None); StringBuilder sbd = new StringBuilder(); bool flag = true; for (int i = 0; i < strList.Length; i++) { flag = true; string strL = strList[i]; if (strL != "") { while (flag) { if (Encoding.Default.GetByteCount(strL) > 14) { int lenByte = 0; string str = ""; for (int j = 0; j < strL.Length; j++) { string s = strL.Substring(i, 1); if (Encoding.Default.GetByteCount(s) == 2) { lenByte += 2; str += s; } else { lenByte++; str += s; } if (lenByte >= 14) { break; } } sbd.Append(strL.Substring(0, str.Length)); sbd.Append("\r\n"); strL = " " + strL.Substring(str.Length, strL.Length - str.Length); } else { flag = false; sbd.Append(strL); sbd.Append("\r\n"); } } } } textBox1.Text = sbd.ToString(); Graphics g = textBox1.CreateGraphics(); Point p = textBox1.GetPositionFromCharIndex(textBox1.Text.LastIndexOf(textBox1.Text[textBox1.Text.Length - 1])); //int height=(int)Math.Ceiling(g.MeasureString("满",textBox1.Font).Height); int height = (int)Math.Ceiling(textBox1.Font.GetHeight()); CreateCaret(textBox1.Handle,0,1,height); SetCaretPos(p.X, p.Y); ShowCaret(textBox1.Handle); #endregion } 楼主试试
於黾 2014-04-24
  • 打赏
  • 举报
回复
引用 4 楼 u013789247 的回复:
事件发错了,这段代码是在textBox1_TextChanged事件中 [quote=引用 2 楼 caozhy 的回复:] textBox1.Text = sbd.ToString(); 后加上 textBox1.SelectStart = textBox1.Text.Length;
我试过了,不行。加上后输入一个字符就会换一行[/quote] 你的textbox设置为允许换行显示了,所以把光标移到最后一个字符的后面,就变成跑到\n后面去了 你可以写成 if(textBox1.length>0) { textBox1.SelectStart = textBox1.Text.Length-1; }
黄大仙儿 2014-04-24
  • 打赏
  • 举报
回复
引用 4 楼 u013789247 的回复:
事件发错了,这段代码是在textBox1_TextChanged事件中 [quote=引用 2 楼 caozhy 的回复:] textBox1.Text = sbd.ToString(); 后加上 textBox1.SelectStart = textBox1.Text.Length;
我试过了,不行。加上后输入一个字符就会换一行[/quote]

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Focus();
            textBox1.Text = "test!!!!!!";
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox1.SelectionStart = textBox1.Text.Length;
        }
测试过没有问题

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧