How do I know where I am when traversing form content??

Using the <RET> key to traverse through a form with a number of fields what question can I ask to determine WHICH field I've just pressed the <RET> key on?

Have tried using the TabIndex property like:

if self::TabIndex = 0
    invoke self::processEmployeeNumber()
    exit method
end-if

for asking if  I'm on the first field on the form but it doesn't seem to work as it appears that this code is processed even WHEN I'm on the 10th field (tabindex property = 9).

So I know I've pressed the <RET> key so how do I now know on WHICH field I've pressed it on?

 

  • Verified Answer

    The following code should work for you.
    I have created a new method called GetFocusedControl which is called in the Keydown method.

    This will return null if no control has focus or will contain the control that has focus.

    This control can then be tested with an evaluate statement.

          method-id Form1_KeyDown final private.
          01 mycontrol type Control.
          procedure division using by value sender as object e as type System.Windows.Forms.KeyEventArgs.
                 set mycontrol to self::GetFocusedControl(self::Controls)
                 evaluate mycontrol
                    when self::textBox1
                       invoke type MessageBox::Show("textBox1 active")
                    when self::textBox2
                       invoke type MessageBox::Show("textBox2 active")
                    when self::dataGridView1
                       invoke type MessageBox::Show("datagrid active")
                    when null
                       invoke type MessageBox::Show("no control is focused")
                 end-evaluate
                 set e::Handled to true
          end method.

          method-id GetFocusedControl public.
          01 container type ContainerControl.
          procedure division using controlsin as type Control ControlCollection
                             returning mycontrol as type System.Windows.Forms.Control.
              perform varying clsControl as type System.Windows.Forms.Control thru self::Controls
                 if clsControl::Focused
                    set mycontrol to clsControl
                    goback
                 else
                    if clsControl::ContainsFocus
                       if clsControl::Controls::Count = 0
                          set mycontrol to clsControl
                          goback
                       else
                          set mycontrol to self::GetFocusedControl(clsControl::Controls)
                          goback
                       end-if
                    end-if
                 end-if
              end-perform
              set mycontrol to null
              goback.
          end method.