[英]C# TextBox Override Arrow Key Select Function
Basically, continuing from this question , when the user presses shift
+ either left or right I need it to select the entire character group. 基本上,从这个问题继续,当用户按下
shift
+向左或向右时,我需要它来选择整个字符组。
The current code I have (under the PreviewKeyDown Event): 我拥有的当前代码(在PreviewKeyDown事件下):
string s;
int caretLocation = textBox1.SelectionStart;
string txt = textBox1.Text;
if (e.Shift)
{
switch (e.KeyCode)
{
#region Right
case Keys.Right:
{
s = txt.Substring(caretLocation);
foreach (string combo in charCombinations)
{
if (s.StartsWith(combo))
{
textBox1.SelectionLength = combo.Length - 1;
break;
}
}
break;
}
#endregion
#region Left
case Keys.Left:
{
s = txt.Substring(0, caretLocation);
foreach (string combo in charCombinations)
{
if (s.EndsWith(combo))
{
textBox1.SelectionStart = caretLocation - combo.Length + 1;
textBox1.SelectionLength = combo.Length + 1
break;
}
}
break;
}
#endregion
}
}
The first issue lies with the right key, where if there are two of such character groups in a row, the caret does not move right any further from the position shown below: 第一个问题在于右键,如果连续出现两个这样的字符组,则插入符号不会从下面显示的位置进一步向右移动:
The second issue lies with the left key, where the left key selection just doesn't select the entire character: 第二个问题与左键有关,其中左键选择只是不能选择整个字符:
For this case, the entire word sin(
should be selected. 对于这种情况,应选择整个单词
sin(
。
Thanks in advance! 提前致谢!
charCombinations
has been defined as: charCombinations
已定义为:
private static readonly string[] charCombinations = new string[] { "asinh(", "acosh", "atanh(",
"sinh(", "cosh(", "tanh(", "asin(", "acos(", "atan(", "sin(", "cos(", "tan(",
"log(", "ln(", "PI", "e", "Phi"};
You must add next text length to previous length. 您必须将下一个文本长度添加到上一个长度。
I fixed your codes this model: 我已将此模型固定为您的代码:
case Keys.Right:
{
s = txt.Substring(caretLocation);
foreach (string combo in charCombinations)
{
if (s.StartsWith(combo))
{
textBox1.SelectionLength += combo.Length - 1;
break;
}
}
break;
}
The important line is: textBox1.SelectionLength += combo.Length - 1;
重要的行是:
textBox1.SelectionLength += combo.Length - 1;
in that I use +=
instead =
因为我用
+=
代替=
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.