Recent

Author Topic: Controlling user input? [SOLVED]  (Read 6933 times)

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Controlling user input? [SOLVED]
« on: September 18, 2014, 07:56:40 pm »
I want to have the user input a month as a number (1-12). But, obviously the user could input any number, meaning the program won't work. I know I could use an IF statement, but is there a better way to control user input?
« Last Edit: September 19, 2014, 07:03:52 pm by MJ Haste »
Programming is my middle name! Wait, no it is not...

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: Controlling user input?
« Reply #1 on: September 18, 2014, 07:59:54 pm »
The easiest way is to use TSpinEdit.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Controlling user input?
« Reply #2 on: September 18, 2014, 08:05:04 pm »
Personally I'd just use a ComboBox with 12 items (configured as style = csDropDownList & DropDownCount=12).  You could even populate the ComboBox with month names, and just read the ItemIndex+1.    For some reason I just find TSpinEdits hard to use (not as a developer, I'm talking now about the end user experience - way too many mouse clicks to get to just the right number....)
« Last Edit: September 18, 2014, 08:07:42 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Controlling user input?
« Reply #3 on: September 18, 2014, 08:10:51 pm »
Thanks for the suggestions, I'm sure that I'll use these at some point.
But, any other way to do it in a simple program, not an application?
Programming is my middle name! Wait, no it is not...

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Controlling user input?
« Reply #4 on: September 18, 2014, 08:11:57 pm »
this will do just fine

Code: [Select]
if not (Key in ['1'..'12'] )then Key:=#0;
Edit: put it on key press event where key is a char
« Last Edit: September 18, 2014, 08:14:25 pm by Never »
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Controlling user input?
« Reply #5 on: September 18, 2014, 08:13:26 pm »
Thanks Never.
Programming is my middle name! Wait, no it is not...

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Controlling user input?
« Reply #6 on: September 18, 2014, 08:16:35 pm »
your are wellcome @MJ Haste
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Controlling user input?
« Reply #7 on: September 18, 2014, 08:18:37 pm »
Code: [Select]
if not (Key in ['1'..'12'] )then Key:=#0;
Edit: put it on key press event where key is a char

That bends my head :-)  '12' is not a char, does this really work?

But, any other way to do it in a simple program, not an application?

You mean a console application?   Sorry, I've no idea...
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Controlling user input?
« Reply #8 on: September 18, 2014, 09:07:05 pm »
no it will not @Mike.Cornflake thank you
i was in a harry
 this one is fine  on key press
Code: [Select]
if not (Key in ['0'..'9'] )then Key:=#0;
and this on key up


Code: [Select]
var loc_Num:Integer;
    var loc_Str:String;
begin

       loc_Str:=Edit1.Text;
  loc_Num:=StrToInt(loc_Str);
  if not (loc_Num in [1..12]) then ShowMessage('error');
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Controlling user input?
« Reply #9 on: September 18, 2014, 09:43:06 pm »
You mean a console application?   Sorry, I've no idea...

Yep, thanks anyway. I'll want to do this in a graphical application at some point. :)

Thanks Never, don't really know what you mean by key up, but I understand the code. :)
Programming is my middle name! Wait, no it is not...

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Controlling user input?
« Reply #10 on: September 18, 2014, 10:01:24 pm »
You mean a console application?   Sorry, I've no idea...
Thanks Never, don't really know what you mean by key up, but I understand the code. :)

every a key on your keyboard is pressed the lcl framework creates 3 events onkeydown when you push it down onkeyUp when you release it and onkeypress when both keydown and up state have been satisfied (very generic explanation) they onkeydown/up take the virtual key code as input the onkeypress take the character that was pressed as input.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Controlling user input?
« Reply #11 on: September 18, 2014, 10:13:56 pm »
Adding to @taazz
Code: [Select]
Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
Code: [Select]
EditKeyPress(Sender: TObject; var Key: char);TEdit provides among other events [OnKeyUp] and [OnKeyPress] to control user input
you can locate all the events of each control you add in a form
from the events tab on lazarus [object inspector]
press F11 if you do not see the [object inspector] or go view/object inspector,
to use an object's event double click to the corresponding event on the object inspector's list and lazarus will set you up
to start writting your code
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

Rails

  • Guest
Re: Controlling user input?
« Reply #12 on: September 19, 2014, 02:41:12 am »
See if this simple console program helps.

Code: [Select]
program project1;

uses
  SysUtils;

var
  month: string = '';

begin
  WriteLn('Enter month as a number from 1 to 12 to start.');
  WriteLn('Enter Q or quit to stop.');
  while True do                                               //  One way to do a continuous Loop command
  begin
    ReadLn(month);                                         //  Reads one line of input from the console
    if LeftStr(LowerCase(month), 1) = 'q' then    //  Test for user wanting to quit
    begin
      WriteLn('Done');                                      //  Write 'Done' and exit the program
      Exit;
    end;
    case month of                                               // Test for the months as numbers and write the
      '1':                                                           //   appropriate name to the console
        WriteLn('January');
      '2':
        WriteLn('February');
      '3':
        WriteLn('March');
      '4':
        WriteLn('April');
      '5':
        WriteLn('May');                                     //
      '6':
        WriteLn('June');
      '7':
        WriteLn('July');
      '8':
        WriteLn('August');
      '9':
        WriteLn('September');
      '10':
        WriteLn('October');
      '11':
        WriteLn('November');
      '12':
        WriteLn('December');
      else
        WriteLn('Error - try again.');                   //  If no valid month found, must be error                           
    end;
  end;
end.       

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Controlling user input?
« Reply #13 on: September 19, 2014, 08:44:42 am »
I also think the solution of Mike.Cornflake is the best idea. But if you still want to use a simple edit, do masking or use this:
Code: [Select]
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
var KeyLength : byte;
begin
  KeyLength := length((Sender as TEdit).Text);
  case KeyLength of
   0 : if not (key in ['0','1',#8]) then key := #0;
   1 : if not (key in ['0'..'9',#8]) then key := #0;
   else if not (key = #8) then key := #0;
  end;
end;                     
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

MJ Haste

  • New Member
  • *
  • Posts: 34
  • An aspiring programmer!
    • MJHaste's YouTube
Re: Controlling user input?
« Reply #14 on: September 19, 2014, 07:03:34 pm »
Thanks for all these different ways, and explanations! I really appreciate all the help. Rails and Never were closest to giving what I needed, but I'll be sure to use everyone else's suggestions at some point!
Programming is my middle name! Wait, no it is not...

 

TinyPortal © 2005-2018