MESCIUS SPREAD for Windows Forms 15.0J サンプルコード集 > キーボード操作(入力マップ) > 編集中セルでのキー入力を検知する |
編集中セルにおいて、各Keyイベント(FpSpreadクラスのKeyDown/KeyPress/KeyUpイベント:いずれもSystem.Windows.Forms.Controlから継承)は基本的に発生しません。/p>
このような場合は、編集中に利用されるエディターコントロール(FpSpread1.EditingControl)にハンドルすることで、各Keyイベントを発生させることができます。
但し、各Keyイベントの発生条件は入力マップの定義などにも大きく依存しますので、実装の際にはあらかじめ十分な動作確認を行ってください。
|
private void fpSpread1_EditModeOn(object sender, System.EventArgs e) { //セルの編集開始のタイミングで各Keyイベントをハンドルします fpSpread1.EditingControl.KeyDown += new KeyEventHandler(this.fpSpread1_KeyDown); fpSpread1.EditingControl.KeyPress += new KeyPressEventHandler(this.fpSpread1_KeyPress); fpSpread1.EditingControl.KeyUp += new KeyEventHandler(this.fpSpread1_KeyUp); } private void fpSpread1_EditModeOff(object sender, System.EventArgs e) { //セルの編集終了のタイミングで各Keyイベントのハンドルを解除します fpSpread1.EditingControl.KeyDown -= new KeyEventHandler(this.fpSpread1_KeyDown); fpSpread1.EditingControl.KeyPress -= new KeyPressEventHandler(this.fpSpread1_KeyPress); fpSpread1.EditingControl.KeyUp -= new KeyEventHandler(this.fpSpread1_KeyUp); } private void fpSpread1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { Console.WriteLine("KeyDownイベント:" + e.KeyCode.ToString()); } private void fpSpread1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { Console.WriteLine("KeyPressイベント:" + e.KeyChar.ToString()); } private void fpSpread1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { Console.WriteLine("KeyUpイベント:" + e.KeyCode.ToString()); }
Private Sub FpSpread1_EditModeOn(ByVal sender As Object, ByVal e As System.EventArgs) Handles FpSpread1.EditModeOn 'セルの編集開始のタイミングで各Keyイベントをハンドルします Dim KeyDownHandler As KeyEventHandler = AddressOf FpSpread1_KeyDown AddHandler FpSpread1.EditingControl.KeyDown, KeyDownHandler Dim KeyPressHandler As KeyPressEventHandler = AddressOf FpSpread1_KeyPress AddHandler FpSpread1.EditingControl.KeyPress, KeyPressHandler Dim KeyUpHandler As KeyEventHandler = AddressOf FpSpread1_KeyUp AddHandler FpSpread1.EditingControl.KeyUp, KeyUpHandler End Sub Private Sub FpSpread1_EditModeOff(ByVal sender As Object, ByVal e As System.EventArgs) Handles FpSpread1.EditModeOff 'セルの編集終了のタイミングで各Keyイベントのハンドルを解除します Dim KeyDownHandler As KeyEventHandler = AddressOf FpSpread1_KeyDown RemoveHandler FpSpread1.EditingControl.KeyDown, KeyDownHandler Dim KeyPressHandler As KeyPressEventHandler = AddressOf FpSpread1_KeyPress RemoveHandler FpSpread1.EditingControl.KeyPress, KeyPressHandler Dim KeyUpHandler As KeyEventHandler = AddressOf FpSpread1_KeyUp RemoveHandler FpSpread1.EditingControl.KeyUp, KeyUpHandler End Sub Private Sub FpSpread1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles FpSpread1.KeyDown Console.WriteLine("KeyDownイベント:" + e.KeyCode.ToString) End Sub Private Sub FpSpread1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles FpSpread1.KeyPress Console.WriteLine("KeyPressイベント:" + e.KeyChar.ToString) End Sub Private Sub FpSpread1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles FpSpread1.KeyUp Console.WriteLine("KeyUpイベント:" + e.KeyCode.ToString) End Sub