How can i update cursor position? [duplicate]

你。 提交于 2019-12-20 04:12:38

问题


Possible Duplicate:
Set cursor in RichTextBox

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    string text = richTextBox1.Text.ToString();          
    text = text.Replace("a", "අ");
    text = text.Replace("අඅ", "ආ");
    richTextBox1.Text = text.ToString();
}

this is my code. in here after getting result cursor position is come in-front of the letter so how i update cursor position after the letter? please help........


回答1:


richTextBox1.Focus();
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();

TextBox.SelectionStart Property: Gets or sets the starting position of the text selected in the text box. MSDN

Edited: Here is another option ... if you use richTextBox1.AppendText instead of richTextBox1.Text, it should automatically put the cursor at the end.




回答2:


Do the following:

richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.Focus();

So your event should be:

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
   string text = richTextBox1.Text;
   text = text.Replace("a", "අ");
   text = text.Replace("අඅ", "ආ");
   text = text.Replace("i", "ඉ");
   richTextBox1.Text = text.ToString();
   richTextBox1.SelectionStart = richTextBox1.Text.Length;
   richTextBox1.Focus();
}



回答3:


You can set the SelectionStart value to a number larger than the length of your text. This will automatically place the cursor after the last character. e.g. this.rechTextBox1.SelectionStart = int.MaxValue;




回答4:


Try this:

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    string text = richTextBox1.Text;
    text = text.Replace("a", "අ");
    text = text.Replace("අඅ", "ආ");
    text = text.Replace("i", "ඉ");
    this.richTextBox1.Text = text.ToString();

    TextPointer caret = this.richTextBox1.CaretPosition;
    caret = caretPos.DocumentEnd;
    this.richTextBox1.CaretPosition = caret;
}

Hope this helps!



来源:https://stackoverflow.com/questions/11304542/how-can-i-update-cursor-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!