[英]How to detect position of last pressed key in a string in C#?
I'm entering data in a datagridview. 我正在datagridview中输入数据。
Say I've entered 678.56
in a cell. 说我在一个单元格中输入了
678.56
。 Now if I again press digit 6 after 678, 现在,如果我在678之后再次按数字6,
I want to know the last pressed key's position in the string. 我想知道最后按下的键在字符串中的位置。 Text.IndexOf(e.KeyChar) is giving me position 0 (as there is also a 6).
Text.IndexOf(e.KeyChar)给我位置0(因为还有6)。
But I need to know the currently pressed key's position. 但我需要知道当前按下的键的位置。 I'm using
KeyPress
event. 我正在使用
KeyPress
事件。
Edit: I'm using string value taken from a cell of datagridview. 编辑:我正在使用从datagridview的单元格中获取的字符串值。 It's not a textbox.
它不是文本框。 I'm creating a eventhandler as KeyPress.
我正在创建一个作为KeyPress的事件处理程序。
查看TextBox.SelectionStart之前的字符。
You could use this in your KeyPress
event: 您可以在
KeyPress
事件中使用它:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
int position = textBox1.SelectionStart;
}
尝试使用KeyRelease事件,即当e.KeyChar将返回按下的键的值(在这种情况下释放)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.