Recent

Author Topic: Can I simplify checking multiple objects with a similar name??  (Read 6181 times)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Can I simplify checking multiple objects with a similar name??
« on: September 20, 2010, 05:00:22 pm »
I wasn't sure exactly how to word the subject so here is a better description.

I have a form with 16 fields that are all named 'Item' + an integer, i.e. 'Item1, Item2, item3, etc.  I need to determine if a key press happened in any of those fields from the form key press event, I do NOT want to use the object key press  event.

I can do something like,

if (Key = VK_END) and (form.activecontrol = form.findcomponent('Item1')) then do .....
if (Key = VK_END) and (form.activecontrol = form.findcomponent('Item2')) then do .....
if (Key = VK_END) and (form.activecontrol = form.findcomponent('Item3')) then do .....

or something like

if Key = VK_END then
                     begin
                      if form.activecontrol = form.findcomponent('item1') then do .....
                      if form.activecontrol = form.findcomponent('item2') then do .....
                      if form.activecontrol = form.findcomponent('item3') then do .....
                      end;

Either way will work but I was hoping to simplify the code in someway.

What the code will do is check if the user hit the 'end' key when in a specific tedit object and then also check if that specific tedit object is empty or not, if it is one of the specific tedits and that tedit is empty then I can continue to the next screen if it is not empty but the rest of that line is filled in correctly then I can also go to the next screen. If the tedit is a specific one and the rest of the line is not finished then I give a warning message and keep the focus on that tedit. 

I can do this with a bunch of if statements but I hoped I could shrink it down with some fancy coding  :)

Any suggestions?
« Last Edit: September 20, 2010, 05:08:16 pm by wpflum »

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2673
Re: Can I simplify checking multiple objects with a similar name??
« Reply #1 on: September 20, 2010, 05:55:14 pm »
and why don't you want to use the sender passed ?
That is the easiest. Why do you persist in using findcomponent()
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Can I simplify checking multiple objects with a similar name??
« Reply #2 on: September 20, 2010, 06:04:47 pm »
It's not that I don't want the sender passed it is because I need to use the form keydown event to catch cursor key presses so I don't have an individual object sender like if I used the individual keypress event for an object.  Why the problem with findcomponent??? 

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4673
  • I like bugs.
Re: Can I simplify checking multiple objects with a similar name??
« Reply #3 on: September 20, 2010, 07:45:53 pm »
Why the problem with findcomponent??? 

Because it is slow and because it is not needed.
The method belongs to TForm1 so you don't need to write "form." explicitly, so:

  if Key = VK_END then begin
    if activecontrol = item1 then .....
    if activecontrol = item2 then .....
    if activecontrol = item3 then .....
  end;

Although I don't know why you can't assign key handlers to each control.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Can I simplify checking multiple objects with a similar name??
« Reply #4 on: September 20, 2010, 07:59:42 pm »
Ok, I was trying to avoid restarting the discussion my programming style by partitioning off the code but since you insist. 

I'm handling everything through a keypress function that is not on any form so I send the sending object, the form itself since I'm using the form keydown event, to the function so I have to access form elements using sender.formobject......  and so on.  I didn't want 30 or so key handlers like if I tried to do it per object so I have one big handler that bases what key does what depending on where on the form or page of the form your are at.  My first thought on handling the event was to use a bunch of ifs like I originally posted but I was hoping since the fields were all prefixed with 'Item' I might find a more elegant solution, one I might add, that would allow future expansion by adjusting a few parameters instead of adding a bunch more if statements.

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: Can I simplify checking multiple objects with a similar name??
« Reply #5 on: September 21, 2010, 03:57:22 am »
1: you can have all or a subset of your tedits call the same on key event and keep your app onkey as well.  the tedits onkey call can recorde who "sender" is in a variable that you test in you app level onkey (which one is called first?)

2: You can also create a list of pointers to your tedits, populate this list in the form create, and use a for loop to scan the list. 

2b: this list approch can be made into an object that includes a do something special for flaged tedits. the object can also have a onkey handler that is assigned when you add the tedit to the object's list and so on.

is this closer to what your looking for?
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

 

TinyPortal © 2005-2018