Forum > General

Can I simplify checking multiple objects with a similar name??

(1/2) > >>

wpflum:
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?

Marc:
and why don't you want to use the sender passed ?
That is the easiest. Why do you persist in using findcomponent()

wpflum:
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:

--- Quote from: wpflum on September 20, 2010, 06:04:47 pm ---Why the problem with findcomponent??? 

--- End quote ---

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

wpflum:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version