Recent

Author Topic: [SOLVED] Changing TEdit to an integer  (Read 41046 times)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #15 on: November 18, 2013, 09:56:15 pm »
My mistake.  I should have said to add LclType to the uses statement near the top of the unit.

Goodkeys is already declared.  Once you add LclType it *should* be OK.
Lazarus Trunk / fpc 2.6.2 / Win32

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Changing TEdit to an integer
« Reply #16 on: November 19, 2013, 01:47:05 am »
Thanks, Avishai!

I know you put *should* be ok...but it froze again... 

I added lclType in the uses block; I put the entire procedure line in the class: procedure TClients.CountryCodeChange(Sender: TObject; var Key: Word; Shift: TShiftState); the only thing I modified in your routine was the procedure name:  I changed TForm1.Edit1KeyDown to TClients.CountryCodeChange.  TClients.CountryCodeChange was what the IDE created.

I didn't even try a number, I typed in "h".  It froze... 

Went back into Lazarus and changed the OnKeyDown event to what it provided: CountryCodeKeyDown.  Attempted to type in 1, 2, 3 but it took the 1 and froze...  Task Manager won't even kill it!  I have to reboot. :(

Is there another Property or Event I need to bring into the game?

Thanks!!


Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

ausdigi

  • Jr. Member
  • **
  • Posts: 52
  • Programmer
    • RNR
Re: Changing TEdit to an integer
« Reply #17 on: November 19, 2013, 02:02:02 am »
Avishai's code MUST be in the control's OnKeyDown event - its too late once its reached OnChange. What the code does is only allow certain keys to be processed by and within the control. This is a way to try and restrict input (but not foolproof). Because the TSpinEdit already does the numeric checking it was recommended (and I recommend it too) but if you don't like arrows beside numeric inputs then you have to remember to still validate your input before processing (eg. with TryStrToInt).

TClients.CountryCodeChange was what the IDE created.
Is that when you double-clicked on the control? Most controls have default events for the IDE (like this for TEdit) - you need to switch to the Events tab to implements others.
Win10/64: CT 5.7 32&64

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #18 on: November 19, 2013, 02:02:22 am »
Hmmm.  Are you sure that it is hanging in that routine?  Try commenting out the line "if not(Key in GoodKeys) then Key:= 0;" and see if it still hangs.  If it does then it's hanging somewhere else.  If it does not hang then that is the problem although I don't see how.  I'll look in to some more in the morning.  It's 3AM in Tel Aviv and I need some rest.
Lazarus Trunk / fpc 2.6.2 / Win32

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Changing TEdit to an integer
« Reply #19 on: November 19, 2013, 05:05:04 am »
Thanks for your input!

Avishai - Yep.  It still hung with the line commented out.

ausdigi - Don't know what you mean "be in the control's OnKeyDown event"...  Therefore, I went to that event and tried to enter the code somehow.  However, when I double-clicked the event (for some unknown reason), it created:  procedure TClients.CountryCodeKeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState); which works fine! All I need to do now is incorporate a routine so when [RETURN] is pressed, it will go to the next TabStop.

When you say "its too late once its reached OnChange.", what do you mean?  Since all of these procedures/routines are created by the IDE, how will I know how the flow of the program goes except for the GUI screen inputs that I originally create?  Is there a graphic Nassi-Shneiderman chart or some other flow diagram that the IDE generates?  (whew!)

Thanks!


 
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

ausdigi

  • Jr. Member
  • **
  • Posts: 52
  • Programmer
    • RNR
Re: Changing TEdit to an integer
« Reply #20 on: November 19, 2013, 06:04:26 am »
However, when I double-clicked the event (for some unknown reason), it created:  procedure TClients.CountryCodeKeyDown
That's exactly how the IDE creates event stubs for you.

All I need to do now is incorporate a routine so when [RETURN] is pressed, it will go to the next TabStop.
In the KeyDown event you could check and if its a RETURN you could replace it with a TAB.
Code: [Select]
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_RETURN then
    Key := VK_TAB;
end;

When you say "its too late once its reached OnChange.", what do you mean?  Since all of these procedures/routines are created by the IDE, how will I know how the flow of the program goes except for the GUI screen inputs that I originally create?  Is there a graphic Nassi-Shneiderman chart or some other flow diagram that the IDE generates?  (whew!)
It is "too late" because the OnChange event happens after the OnKeyDown event. The fact that the OnKeyDown event has a var parameter indicates that it can be changed whilst the Change event seems rather final. You could always check the source to see which event fires first but these particular ones are quite logical: OnKeyDown -> OnChange (i.e. a change can't happen before the key press is processed). The IDE only helps you create the code but has nothing to do with flow as such - this is controlled by the components and how they are coded.
Win10/64: CT 5.7 32&64

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #21 on: November 19, 2013, 07:42:15 am »
I don't like TSpinEdit/TFloatSpinEdit either.  Firstly, they're graphically ugly.  Secondly, they're too slow and clumsy to be useful for large ranges (I.E. 1900 to 3000 and yes I know you can type a number but then what's the point of the spinner?).  By the way, in Delphi you have the option to hide the spinner.

But my biggest problem is the design.  In my opinion there only needs to be one control, based on TFloatSpinEdit.  It already has property DecimalPlaces.  When DecimalPlaces:= 0, it displays an Integer although it's a Float.  It also has property Value.  Instead it *should* have property FloatValue and property IntValue.
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #22 on: November 19, 2013, 07:58:55 am »
WickedDum, you can set 'Break Points' in your code by clicking in the gray area to the left of your code where the line numbers are.  Then when you run your program in the IDE, it will stop when that line is about to be executed and switch to the code editor and show you that it is about to execute that line.  You have F7, F8 and F9 to proceed through your code.  You should read the Wiki about the Debugger.  It is one of the best teaching tools you will ever find.

As for the Keyboard Events.  It's OnKeyDown - OnKeyPress - OnChange - OnKeyUp.

I hope this is more helpful than confusing.
Lazarus Trunk / fpc 2.6.2 / Win32

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Changing TEdit to an integer
« Reply #23 on: November 19, 2013, 08:03:33 pm »
Thanks for the input!

Avishai - When we run into situations like these "...Instead it *should* have property FloatValue and property IntValue...", are we supposed to send it to the developers?  I ran into a bunch of discrepancies between the current IDE and the wiki help for the control tabs.


If I understand what I am doing, then...if I want the [ENTER] key to function as the [TAB] key, then I need to go to every TEdit and add the OnKeyDown event.  Correct?

Then, as ausdigi said: "OnKeyDown -> OnChange (i.e. a change can't happen before the key press is processed)", I should/would reorder all of the procedures to ensure the flow of the source was logical and compartmentalized.  Am I on the right track?

If correct, then the IDE is creating a heqquva lot of procedures for a specific logical/active  function.  Could I put that "event" in a global declaration and do it once?  OOP is supposed to be all about reusable code isn't it?

Just sayin'...

Do these questions belong in a different area of the forum?

Thank you all!!
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #24 on: November 19, 2013, 08:07:37 pm »
Quote
If I understand what I am doing, then...if I want the [ENTER] key to function as the [TAB] key, then I need to go to every TEdit and add the OnKeyDown event.  Correct?

Not exactly.  But how do I explain.  You can create 1 OnKeyDown event for a control and then:
  Select another control.
  Go to the Object Inspector/Events Tab.
  Select the OnKeyDown and a DropDown will appear showing Events
  that already exist.  One of them should be the OnKeyDown for the
  earlier Control.
  Select that event and it will be use by both control.

But it has to be written in a generic way.  Give me a few minutes and I will give you some example code.
« Last Edit: November 19, 2013, 08:14:24 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #25 on: November 19, 2013, 08:23:22 pm »
Here is a quick and very simple example.

Code: [Select]
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
  if (Sender is TEdit) then
    with (Sender as TEdit) do
      if Key = VK_Return then Key:= VK_Tab;
end;

But writing it this way means that it can only be used by TEdits.  If you leave out the "if(Sender..." and "With(Sender..." then it will work for some other types of controls as well.  Keep in mind that this is only to demonstrate one possible way to write it generically, using Sender instead of Edit1. 
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #26 on: November 19, 2013, 08:34:01 pm »
Quote
Avishai - When we run into situations like these "...Instead it *should* have property FloatValue and property IntValue...", are we supposed to send it to the developers?  I ran into a bunch of discrepancies between the current IDE and the wiki help for the control tabs.

Personally I prefer to bring things up in the Forum and get some input before sending something to the developers.  There aren't that many of them and they get overwhelmed by all the reports.  And Often the reports are a result of not fully understanding.

And the specific topic of FloatValue and IntValue will not happen and it shouldn't.  You can do that for New Controls, but changing the design of Existing Controls would break any programs that used them before the change.  That's a really bad practice.  My "review" of those controls was purely academic, although someone could write one and maybe submit it for the LazControls Tab.  I doubt it would get excepted though.
Lazarus Trunk / fpc 2.6.2 / Win32

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Changing TEdit to an integer
« Reply #27 on: November 19, 2013, 09:04:39 pm »
if you change KeyPreview in your form to True, then you can use OnKeyDown event of the form itself before any child. Sender would be the form itself, so you have to use ActiveControl instead to find out if the control who has the focus is TEdit then change the key.

Code: [Select]
procedure TYourForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key=VK_RETURN) and (ActiveControl<>nil) and (ActiveControl is TEdit) then
     Key:=VK_TAB;
begin

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #28 on: November 19, 2013, 09:09:14 pm »
Yes. KeyPreview is a good approach and sometimes the only realistic one.  Some Controls don't have OnKeyDown/OnKey... events.
Lazarus Trunk / fpc 2.6.2 / Win32

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Changing TEdit to an integer
« Reply #29 on: November 20, 2013, 03:12:01 am »
Thank you!  Thank you!!

Avishai - I like your code; simple; self-explanatory.  (IS and AS are keywords?  Who knew??)  Thanks.  OK...so the routines that are in a particular event are available to other events?  (Only if written generically.)  I would I put this "generic routine" in one control Event OnKeyDown and then select it through other Event OnKeyDown options that I might want the same action?

engkin - Thanks for the thought.  I'm getting a better understanding of how it is all tied together with "clicking" instead of actually writing code... :D  From what I see...if I wanted the [RETURN] key to function normally in some other code snippet, your routine would not allow it, correct?

Thank you ... programmers (non-sexist? ;)).  You've been a big BIG help!



Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

 

TinyPortal © 2005-2018