2012-03-20 55 views
0

我想获得这样的效果:当我点击文本框 ,光标移动到索引0 例如“你好”(|你好) 现在,当我写的东西,我希望文字出现之前“你好” 例如‘hello2hello’CaretIndex里面文本框

我已经使用的功能,例如:

.Select(0,0); 

.CurrentIndex=0; 

.SelectionStart=0 SelectionLenght=0; 

这些功能只在事件KeyDownTextChanged事件的影响。 不幸的是,这些行为并没有产生预期的效果。我得到了我输入内容的镜像。示例(而不是你好,我得到olleh) 如何改变插入文本框内的脱字符的索引?

回答

0

试试这个

public string TBName = ""; 
private void txtResult_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (TBName != ((TextBox)sender).Name) 
    { 
     ((TextBox)sender).SelectionStart = 0; 
     ((TextBox)sender).SelectionLength = 0; 
     TBName = ((TextBox)sender).Name; 
    } 
} 

你可以将它链接到多个文本框,然后还有

+0

这显然会更好,以及确保'sender'实际上是在一个TextBox你做铸造前。 – Jaques 2012-03-20 11:53:50