入力数値を限定するには?

解決


ニトロ  2007-05-15 13:06:44  No: 26134  IP: 192.*.*.*

Edit内に入力する数値を1〜15に限定するにはどうすればいいですか?

編集 削除
Ru  2007-05-15 14:01:10  No: 26135  IP: 192.*.*.*

OnKeyPressイベントで数値のみ入力できるように制御。
これだけだと16以上の数値も入るので多少工夫いります。

編集 削除
cocomo  2007-05-15 14:52:55  No: 26136  IP: 192.*.*.*

めんどくさいからTComboBoxで逃げるって言うのは?

var
  idx : Integer;
begin
  with ComboBox1 do
  begin
    Items.Clear;
    for idx := 1 to 15 do
      Items.Add(IntToStr(idx));
    DropDownCount := 15;
    Style := csDropDownList;
    ItemIndex := 0;
  end;
end;

編集 削除
uuu  2007-05-15 15:15:40  No: 26137  IP: 192.*.*.*

EditのOnChangeで、範囲チェックするのが簡単そう。
貼り付けされても反応するんで。

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Text:= '1';
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
  i: integer;
begin
  i:= StrToIntDef(Edit1.Text, 0);
  if not (i in [1..15]) then
  begin
    ShowMessage('NG');
    Edit1.Text:= '1';
  end;
end;

編集 削除
ニトロ  2007-05-15 15:19:23  No: 26138  IP: 192.*.*.*

返信ありがとうございます。

結局メッセージを表示させるかたちで、
  if (StrToInt(Edit1.Text) < 1)
  or (StrToInt(Edit1.Text) > 15) then
  begin
      ShowMessage('1〜15の間で設定して下さい');
      Edit1.SetFocus;
      Exit;
  end;
こんな感じで書いてみました。

編集 削除
cocomo  2007-05-15 16:52:11  No: 26139  IP: 192.*.*.*

>  if (StrToInt(Edit1.Text) < 1)
>  or (StrToInt(Edit1.Text) > 15) then
if (StrToIntDef(Edit1.Text,0) < 1)
or (StrToIntDef((Edit1.Text,0) > 15) then
の方がいいんじゃない?

編集 削除
ニトロ  2007-05-15 19:56:52  No: 26140  IP: 192.*.*.*

みなさんアドバイスありがとうございます。
勉強になりました。またお願いします。

編集 削除