Problem:
Customer is using the Enter key to traverse through a Windows Form with a number of controls on it.
When the Enter key is pressed, the application needs to find out which control on the form the user was in.
They have tried using the TabIndex property like:
if self::TabIndex = 0
invoke self::processEmployeeNumber()
exit method
end-if
But it always seems to return the same value.
How can this be done?
Resolution:
The following code should work for you. A new method called GetFocusedControl has been created 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.