C# 在屏幕键盘上使用regex
好吧,我真的被困在这里了,伙计们:/。我有一个(自定义!)屏幕键盘,我想使用正则表达式 我成功地创建了一个正则表达式,只允许常规键盘使用C# 在屏幕键盘上使用regex,c#,.net,regex,C#,.net,Regex,好吧,我真的被困在这里了,伙计们:/。我有一个(自定义!)屏幕键盘,我想使用正则表达式 我成功地创建了一个正则表达式,只允许常规键盘使用0-9和逗号 当然,这对我的屏幕键盘不起作用 我有一个代码片段可以工作 但我也有一个空格按钮,一个退格按钮,和两个按钮来移动插入符号 当我使用下面的正则表达式代码并单击空格、退格或插入符号按钮时,会出现以下错误: Object reference not set to an instance of an object. 我做错了什么 // the re
0-9
和逗号
当然,这对我的屏幕键盘不起作用
我有一个代码片段可以工作
但我也有一个空格按钮,一个退格按钮,和两个按钮来移动插入符号
当我使用下面的正则表达式代码并单击空格、退格或插入符号按钮时,会出现以下错误:
Object reference not set to an instance of an object.
我做错了什么
// the regex code
private Control keyb = null;
private void EditProdPriceEx_GotFocus(object sender, RoutedEventArgs e)
{
string AllowedChars = "[^0-9,]";
if (Regex.IsMatch(EditProdPriceEx.Text, AllowedChars))
{
keyb = (Control)sender;
}
}
空格键:
private void KnopSpatie_Click(object sender, RoutedEventArgs e)
{
TextBox tb = keyb as TextBox;
int selStart = tb.SelectionStart; // here is where i get the error
string before = tb.Text.Substring(0, selStart);
string after = tb.Text.Substring(before.Length);
tb.Text = string.Concat(before, " ", after);
tb.SelectionStart = before.Length + 1;
}
退格按钮:
private void KnopWissen_Click(object sender, RoutedEventArgs e)
{
TextBox tb = keyb as TextBox;
int cursorPosition = tb.SelectionStart; // here is where i get the error
if (cursorPosition > 0)
{
tb.Text = tb.Text.Substring(0, cursorPosition -1) + tb.Text.Substring(cursorPosition);
tb.SelectionStart = cursorPosition - 1;
}
}
向左移动插入符号按钮:
private void KnopLinks_Click(object sender, RoutedEventArgs e)
{
TextBox tb = keyb as TextBox;
if (tb.SelectionStart > 0) // here is where i get the error
{
int selStart = tb.SelectionStart;
string before = tb.Text.Substring(0, selStart);
string after = tb.Text.Substring(before.Length);
tb.SelectionStart = before.Length - 1;
}
}
向右移动插入符号按钮:
private void KnopRechts_Click(object sender, RoutedEventArgs e)
{
TextBox tb = keyb as TextBox;
if (tb.SelectionStart >= 0) // here is where i get the error
{
int selStart = tb.SelectionStart;
string before = tb.Text.Substring(0, selStart);
string after = tb.Text.Substring(before.Length);
tb.SelectionStart = before.Length + 1;
}
}
我想你一定要试试这个。在第一个代码中
// the regex code
private Control keyb = null;
private void EditProdPriceEx_GotFocus(object sender, RoutedEventArgs e)
{
string AllowedChars = "[^0-9,]";
if (Regex.IsMatch(EditProdPriceEx.Text, AllowedChars))
{
keyb = (Control)sender;
}
}
您将keyb设置为null,并且没有被您提到的任何键(空格和其他键)设置,我认为这会产生引用未设置错误。你能检查一下吗?哇……这与交易无关……真奇怪@对不起,我编辑了错误,这些错误是荷兰语的,所以我把它翻译错了!你犯了什么错误?您没有检查null
。是的,您的if
s将阻止向tb赋值。然后在后面的其他方法中得到异常,因为它是null…thekeyb=null我的屏幕键盘需要code>,它会向文本框发送类似A
或B
等内容。所以我想我找错地方了。是的,我知道,但是对于正则表达式,例如,如果你按空格键,保持“keyb=null”,因为你没有设置它,引用仍然是null。啊哈!是的,的确如此,这很有道理!我会看一看,谢谢:)我只是简单地使用了KnopA,B,C等。。。IshittesVisible=假代码>多一点工作,但也可以工作