Textbox_KeyPress Event using vb.net or c#
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 pressing key on the keyboard(English character like) k that time i want to replace two characters like "&H0b95" "&H0bcd". How to i implement this concept? whether it is possible or not.
sample code when keypress event e.keychar=chrw("&H0b95") & chrw("&H0bcd") 'This code is not Execute Becuase it get Only One Character'
TextBox1.Text=chrw("&H0b95") & chrw("&H0bcd")
I am already finish this concept but the only problem is cursor position is scroll that is when i am assigning the character to textbox that time selection start is zero so the cursor go to first position. after second line i am set the cursor position to the length of the text that is cursor come to end of the text.
so the problem is the cursor move up and down when i am pressing a key at all the times. how to solve this problem. can any body given an idea to me.
Click here! To See the Tamil Unicode Char开发者_开发知识库acters Table.
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.
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
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);
}
}
精彩评论