|
分類:[.NET 全般]
C# 2010 WPF環境です。
テキストボックの数字のみ 入力クラスを作成したのですが。 IMEのALT+全角を押すとIMEが半角から全角に変更されます。 変更しない様にする方法がありましたら教えて
/*-------------------------------------------------------------- * 数字 テキスト 000010 DSP 0->00000 *--------------------------------------------------------------*/
public class OtsTextNumber : TextBox {
static OtsTextNumber() { }
// 項目の正常(真) 異常(偽) private bool m_Momal = true;
public bool Momal { get { return m_Momal; } set { m_Momal = value; } }
/* * */
protected override void OnGotFocus(System.Windows.RoutedEventArgs e) { base.OnGotFocus(e);
// this.Text = this.Text.Replace(",", "");
InputMethod.SetPreferredImeState(this, InputMethodState.DoNotCare); InputMethod.SetPreferredImeConversionMode(this, ImeConversionModeValues.Alphanumeric); InputMethod.SetPreferredImeSentenceMode(this, ImeSentenceModeValues.Automatic);
this.Background = Brushes.LightBlue;
if (this.Text == "") { return; }
try {
if (int.Parse(this.Text) < 0) { this.Text = string.Format("{0:" + new String('0',this.MaxLength) + "}", int.Parse(this.Text)); } } catch { this.Text = ""; };
}
protected override void OnLostFocus(System.Windows.RoutedEventArgs e) {
base.OnLostFocus(e); // Change the TextBox color if (this.Momal == true) { this.Background = Brushes.White; } else { this.Background = Brushes.Pink; }
if (this.Text == "") { return; }
try { this.Text = string.Format("{0:" + new String('0', this.MaxLength) + "}", int.Parse(this.Text)); } catch { this.Text = ""; };
}
/* * */ protected override void OnPreviewKeyDown(KeyEventArgs e) {
base.OnPreviewKeyDown(e);
switch (e.Key) {
case Key.D1: case Key.D2: case Key.D3: case Key.D4: case Key.D5: case Key.D6: case Key.D7: case Key.D8: case Key.D9:
case Key.NumPad1: case Key.NumPad2: case Key.NumPad3: case Key.NumPad4: case Key.NumPad5: case Key.NumPad6: case Key.NumPad7: case Key.NumPad8: case Key.NumPad9: case Key.D0: case Key.NumPad0: break; case Key.Back: case Key.Insert: case Key.Delete: case Key.Tab: case Key.OemBackTab: // case Key.Up: // case Key.Down: case Key.Right: case Key.Left: break; default: e.Handled = true; break; } }
/* * */ protected override void OnPreviewKeyUp(KeyEventArgs e) { base.OnPreviewKeyUp(e);
/* * Enter で次のキー */ if ((Keyboard.Modifiers == ModifierKeys.None) && (e.Key == Key.Enter) && this.IsTabStop == true) { UIElement element = this as UIElement; element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); }
} }
|