Textbox_KeyPress Event using vb.net or c#

后端 未结 3 539
逝去的感伤
逝去的感伤 2021-01-29 05:55

My requirement is i am trying to develop a text editor for my mother tongue language. That is i am trying to develop tamil text editor using unicode characters. When i am pres

相关标签:
3条回答
  • 2021-01-29 06:38

    this TextBox, adds "jj" when the user presses k key. The cursor position is corrcted.

    public class MyTextBox : TextBox
    {
    
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.K)
            {
                int pos = this.SelectionStart;
                this.Text = this.Text.Substring(0, this.SelectionStart) + "jj" 
                + this.Text.Substring(this.SelectionStart);
                this.SelectionStart = pos + 2;
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
    
    0 讨论(0)
  • 2021-01-29 06:41

    You need to update the SelectionStart Property and increase its length with the size of the new text inserted. Something like:

    int curPos = txtEditor.SelectionStart;
    if (e.KeyChar == 'k')
    {
        txtEditor.Text=txtEditor.Text.Insert(txtEditor.SelectionStart, "jj");
        txtEditor.SelectionLength = 0;
    }
    txtEditor.SelectionStart = curPos + 2; //or whatever the length of text u inserted    
    
    0 讨论(0)
  • 2021-01-29 06:52

    You need to stop the textbox from rendering, while you update the cursor position. I had done something similar before with a console application. But can't find the code to reference it here.

    0 讨论(0)
提交回复
热议问题