特定文字の入力のみを許可するには

文書番号 : 33551     文書種別 : 使用方法     登録日 : 2012/11/14     最終更新日 : 2012/11/14
文書を印刷する
対象製品
SPREAD for Windows Forms 7.0J
詳細
テキスト型セルやコンボボックス型セルに用意されているCharacterSetプロパティを使用することで、対象セルに許可する文字セットを設定できます。CharacterSetプロパティに設定可能な列挙体が用意されており、それぞれ以下の内容に従って動作します。

メンバ解説
Ascii任意の文字の入力を許可します。
Alpha英字のみを許可します。
AlphaNumeric英数字のみを許可します。
Numeric数字のみを許可します。(マイナス符号や小数点は入力可)
Hiraganaひらがなのみを許可します。
KatakanaHalf半角カタカナのみを許可します。
Katakana全角カタカナのみを許可します。
KanjiOnly全角文字のみを許可します。(半角スペースは入力可)
KanjiOnlyIME全角文字のみを許可します。(IMEオン)
AllIMEすべての文字を許可します。(IMEオン)


◎サンプルコード(VB)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  ' 任意の文字を許可
  FpSpread1.ActiveSheet.Columns(0).Label = "Ascii"
  Dim tcell As New FarPoint.Win.Spread.CellType.TextCellType()
  tcell.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Ascii
  FpSpread1.ActiveSheet.Columns(0).CellType = tcell

  ' 英字のみを許可
  FpSpread1.ActiveSheet.Columns(1).Label = "Alpha"
  Dim tAlpha As New FarPoint.Win.Spread.CellType.TextCellType()
  tAlpha.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Alpha
  FpSpread1.ActiveSheet.Columns(1).CellType = tAlpha

  ' 英数字のみを許可
  FpSpread1.ActiveSheet.Columns(2).Label = "AlphaNumeric"
  Dim tAlphaNumeric As New FarPoint.Win.Spread.CellType.TextCellType()
  tAlphaNumeric.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.AlphaNumeric
  FpSpread1.ActiveSheet.Columns(2).CellType = tAlphaNumeric

  ' 数字のみを許可
  FpSpread1.ActiveSheet.Columns(3).Label = "Numeric"
  Dim tNumeric As New FarPoint.Win.Spread.CellType.TextCellType()
  tNumeric.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Numeric
  FpSpread1.ActiveSheet.Columns(3).CellType = tNumeric

  ' ひらがなのみを許可
  FpSpread1.ActiveSheet.Columns(4).Label = "Hiragana"
  Dim tHiragana As New FarPoint.Win.Spread.CellType.TextCellType()
  tHiragana.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Hiragana
  FpSpread1.ActiveSheet.Columns(4).CellType = tHiragana

  ' 半角カタカナのみを許可
  FpSpread1.ActiveSheet.Columns(5).Label = "KatakanaHalf"
  Dim tKatakanaHalf As New FarPoint.Win.Spread.CellType.TextCellType()
  tKatakanaHalf.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.KatakanaHalf
  FpSpread1.ActiveSheet.Columns(5).CellType = tKatakanaHalf

  ' 全角カタカナのみを許可
  FpSpread1.ActiveSheet.Columns(6).Label = "Katakana"
  Dim tKatakana As New FarPoint.Win.Spread.CellType.TextCellType()
  tKatakana.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Katakana
  FpSpread1.ActiveSheet.Columns(6).CellType = tKatakana

  ' 全角文字のみを許可
  FpSpread1.ActiveSheet.Columns(7).Label = "KanjiOnly"
  Dim tKanjiOnly As New FarPoint.Win.Spread.CellType.TextCellType()
  tKanjiOnly.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.KanjiOnly
  FpSpread1.ActiveSheet.Columns(7).CellType = tKanjiOnly

  ' 全角文字のみを許可(IMEオン)
  FpSpread1.ActiveSheet.Columns(8).Label = "KanjiOnlyIME"
  Dim tKanjiOnlyIME As New FarPoint.Win.Spread.CellType.TextCellType()
  tKanjiOnlyIME.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.KanjiOnlyIME
  FpSpread1.ActiveSheet.Columns(8).CellType = tKanjiOnlyIME

  ' すべての文字を許可(IMEオン)
  FpSpread1.ActiveSheet.Columns(9).Label = "AllIME"
  Dim tAllIME As New FarPoint.Win.Spread.CellType.TextCellType()
  tAllIME.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.AllIME
  FpSpread1.ActiveSheet.Columns(9).CellType = tAllIME
End Sub

◎サンプルコード(C#)
private void Form1_Load(object sender, EventArgs e)
{
  // 任意の文字を許可
  fpSpread1.ActiveSheet.Columns[0].Label = "Ascii";
  FarPoint.Win.Spread.CellType.TextCellType tcell = new FarPoint.Win.Spread.CellType.TextCellType();
  tcell.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Ascii;
  fpSpread1.ActiveSheet.Columns[0].CellType = tcell;

  // 英字のみを許可
  fpSpread1.ActiveSheet.Columns[1].Label = "Alpha";
  FarPoint.Win.Spread.CellType.TextCellType tAlpha = new FarPoint.Win.Spread.CellType.TextCellType();
  tAlpha.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Alpha;
  fpSpread1.ActiveSheet.Columns[1].CellType = tAlpha;

  // 英数字のみを許可
  fpSpread1.ActiveSheet.Columns[2].Label = "AlphaNumeric";
  FarPoint.Win.Spread.CellType.TextCellType tAlphaNumeric = new FarPoint.Win.Spread.CellType.TextCellType();
  tAlphaNumeric.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.AlphaNumeric;
  fpSpread1.ActiveSheet.Columns[2].CellType = tAlphaNumeric;

  // 数字のみを許可
  fpSpread1.ActiveSheet.Columns[3].Label = "Numeric";
  FarPoint.Win.Spread.CellType.TextCellType tNumeric = new FarPoint.Win.Spread.CellType.TextCellType();
  tNumeric.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Numeric;
  fpSpread1.ActiveSheet.Columns[3].CellType = tNumeric;

  // ひらがなのみを許可
  fpSpread1.ActiveSheet.Columns[4].Label = "Hiragana";
  FarPoint.Win.Spread.CellType.TextCellType tHiragana = new FarPoint.Win.Spread.CellType.TextCellType();
  tHiragana.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Hiragana;
  fpSpread1.ActiveSheet.Columns[4].CellType = tHiragana;

  // 半角カタカナのみを許可
  fpSpread1.ActiveSheet.Columns[5].Label = "KatakanaHalf";
  FarPoint.Win.Spread.CellType.TextCellType tKatakanaHalf = new FarPoint.Win.Spread.CellType.TextCellType();
  tKatakanaHalf.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.KatakanaHalf;
  fpSpread1.ActiveSheet.Columns[5].CellType = tKatakanaHalf;

  // 全角カタカナのみを許可
  fpSpread1.ActiveSheet.Columns[6].Label = "Katakana";
  FarPoint.Win.Spread.CellType.TextCellType tKatakana = new FarPoint.Win.Spread.CellType.TextCellType();
  tKatakana.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Katakana;
  fpSpread1.ActiveSheet.Columns[6].CellType = tKatakana;

  // 全角文字のみを許可
  fpSpread1.ActiveSheet.Columns[7].Label = "KanjiOnly";
  FarPoint.Win.Spread.CellType.TextCellType tKanjiOnly = new FarPoint.Win.Spread.CellType.TextCellType();
  tKanjiOnly.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.KanjiOnly;
  fpSpread1.ActiveSheet.Columns[7].CellType = tKanjiOnly;

  // 全角文字のみを許可(IMEオン)
  fpSpread1.ActiveSheet.Columns[8].Label = "KanjiOnlyIME";
  FarPoint.Win.Spread.CellType.TextCellType tKanjiOnlyIME = new FarPoint.Win.Spread.CellType.TextCellType();
  tKanjiOnlyIME.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.KanjiOnlyIME;
  fpSpread1.ActiveSheet.Columns[8].CellType = tKanjiOnlyIME;

  // すべての文字を許可(IMEオン)
  fpSpread1.ActiveSheet.Columns[9].Label = "AllIME";
  FarPoint.Win.Spread.CellType.TextCellType tAllIME = new FarPoint.Win.Spread.CellType.TextCellType();
  tAllIME.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.AllIME;
  fpSpread1.ActiveSheet.Columns[9].CellType = tAllIME;
}

この列挙体に存在しない、より細かい制御を行う場合には、セルの編集用コントロールのKeyPressイベントを取得し特定文字を制限するコードを記述します。この方法は、.NET Framework標準のTextBoxコントロールで特定文字を制限する場合と基本的には同じになります。KeyPressイベントを用いた簡単なサンプルを記載しますが、KeyPressイベント内の実装方法についてはMSDNなどを参照してください。

◎サンプルコード(VB)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim tcell As New FarPoint.Win.Spread.CellType.TextCellType()
  tcell.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Ascii
  FpSpread1.ActiveSheet.Columns(0).CellType = tcell
End Sub

Private Sub FpSpread1_EditModeOn(ByVal sender As Object, ByVal e As System.EventArgs) Handles FpSpread1.EditModeOn
  '編集中セルでのKeyPressイベントを発生させるため、イベントをハンドルします
  Dim KeyPressHandler As KeyPressEventHandler = AddressOf FpSpread1_KeyPress
  AddHandler FpSpread1.EditingControl.KeyPress, KeyPressHandler
End Sub

Private Sub FpSpread1_EditModeOff(ByVal sender As Object, ByVal e As System.EventArgs) Handles FpSpread1.EditModeOff
  'イベントのハンドルを解除します
  Dim KeyPressHandler As KeyPressEventHandler = AddressOf FpSpread1_KeyPress
  RemoveHandler FpSpread1.EditingControl.KeyPress, KeyPressHandler
End Sub

Private Sub FpSpread1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles FpSpread1.KeyPress
  ' アルファベット[a]の入力を禁止するコード
  Select Case e.KeyChar
    Case "a"
      e.Handled = True
  End Select

  '' 全角文字の入力を禁止するコード
  'Dim sjis As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift-JIS")
  'If sjis.GetByteCount(e.KeyChar) = 2 Then
  '  e.Handled = True
  'End If
End Sub

◎サンプルコード(C#)
private void Form1_Load(object sender, EventArgs e)
{
  FarPoint.Win.Spread.CellType.TextCellType tcell = new FarPoint.Win.Spread.CellType.TextCellType();
  tcell.CharacterSet = FarPoint.Win.Spread.CellType.CharacterSet.Ascii;
  fpSpread1.ActiveSheet.Columns[0].CellType = tcell;
}

private void fpSpread1_EditModeOn(object sender, EventArgs e)
{
  //編集中セルでのKeyPressイベントを発生させるため、イベントをハンドルします
  fpSpread1.EditingControl.KeyPress += new KeyPressEventHandler(this.fpSpread1_KeyPress);
}

private void fpSpread1_EditModeOff(object sender, EventArgs e)
{
  //イベントのハンドルを解除します
  fpSpread1.EditingControl.KeyPress -= new KeyPressEventHandler(this.fpSpread1_KeyPress);
}

private void fpSpread1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
  // アルファベット[a]の入力を禁止するコード
  switch (e.KeyChar.ToString())
  {
    case "a":
      //アルファベット"a"が入力された場合はイベントをキャンセル
      e.Handled = true;
      break;
  }

  //// 全角文字の入力を禁止するコード
  //System.Text.Encoding sjis = System.Text.Encoding.GetEncoding("Shift-JIS");
  //if (sjis.GetByteCount(e.KeyChar.ToString()) == 2)
  //{
  //  e.Handled = true;
  //}
}
関連情報
キーワード
「入力制限」