わかりました、私は本当にここで立ち往生しています:/。正規表現を使用したい (カスタム!) オンスクリーン キーボードがあります。
通常のキーボードのみを許可0-9
する正規表現を正常に作成しました。comma
もちろん、これは私のスクリーンキーボードでは機能しません。
動作するコードスニペットがあります...
しかし、スペース ボタン、バックスペース ボタン、およびキャレットを移動するための 2 つのボタンもあります。
以下の正規表現コードを使用して、スペース、バックスペース、またはキャレット ボタンをクリックすると、次のエラーが発生します。
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;
}
}