Click here to Skip to main content
15,031,550 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have this event which perform some computations inside it (the "for" ones)
The caret inside textBox1(for each letter I write), is redirected to the beginning of text after those computations, and thats why I inserted the last line in code provided, to rectify that bug: textBox1.SelectionStart = textBox1.Text.Length;
[I know that "SelectionStart" is in fact the caret position, but because of the computation, it start as I explained]
Now my problem is: When I want to insert a NEW text in the interior of the text, for each letter I write, the caret goes to the end of the text(because of the last line mentioned). How to write something but caret maintain its position?
Thanks.
Here is the code:
C#
//Here are 4 arrays with chars.
     private void textBox1_TextChanged(object sender, EventArgs e) 
     {
            for (int i = 0; i < literaRom.Length; i++)
            {
                if (textBox1.Text.Contains(literaRom[i]))//lower
                    textBox1.Text = textBox1.Text.Replace(literaRom[i], literaRus[i]);
                if (textBox1.Text.Contains(literaRom[i].ToUpper()))//upper
                    textBox1.Text = textBox1.Text.Replace(literaRom[i].ToUpper(), literaRus[i].ToUpper());
            }

            for (int i = 0; i < literaKeyComp.Length; i++)
            {
                if (textBox1.Text.Contains(literaKeyComp[i]))//lower
                    textBox1.Text = textBox1.Text.Replace(literaKeyComp[i], literaRusComp[i]);
                if (textBox1.Text.Contains(literaKeyComp[i].ToUpper()))//upper
                    textBox1.Text = textBox1.Text.Replace(literaKeyComp[i].ToUpper(), literaRusComp[i].ToUpper());
            }


             textBox1.SelectionStart = textBox1.Text.Length;//redirected cursor (put carret at end of text)
     }
Posted
Updated 26-Nov-11 1:22am
v3

1 solution

Read the SelectionStart before you do the operation, and restore it at the end instead of setting it to the length. You may want to check that your changes have not reduced the length of the text, or inserted before the caret - in which case you need to calculate the new caret position to maintain it's relationship with the user text.
   
Comments
_Q12_ 26-Nov-11 7:01am
   
+5 for you!
private void textBox1_TextChanged(object sender, EventArgs e)
{
int caretPos = textBox1.SelectionStart;//<<<< here
for (int i = 0; i < literaRom.Length; i++)
{
//blabla
}

for (int i = 0; i < literaKeyComp.Length; i++)
{
//blabla
}
textBox1.SelectionStart = caretPos;//redirected cursor__WORKING PERFECTLY

}
many THANKS OriginalGriff
_Q12_ 26-Nov-11 7:09am
   
At what are you referring when you said this: "You may want to check that your changes have not reduced the length of the text, or inserted before the caret" ?
You mean when I insert some string from code?
OriginalGriff 26-Nov-11 7:25am
   
Since you are replacing chunks of text, it is possible that the total length of the text in the text box is going to change. If that happens, and the change is before the carat position, then the carat should move to the left or right to allow for it - otherwise the user types a character in position three, you change it and the caret ends up is the middle of a changed selection instead of at the end. (it's difficult to explain without pictures). If the user doesn't notice and carries on typing, the text box content could get very muddled before he looks at it and starts swearing at you!
_Q12_ 26-Nov-11 7:43am
   
right now is doing pretty well... When I will encounter what you say, I will return to this post and continue from there...until then I'm very happy with it.
Thanks again.
PS-See if you can answer the other post I made it (before this one).(the hard one)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900