Created On:  31 May 2012

Problem:

Is it possible to explicitly cause the focus to move to a desired TextBox on a Windows Form depending on a condition that is met in the program?

Resolution:

Yes, this can be done by using the Focus method of the Control class.  There is an example program attached to this article.  The description of this program is as follows:

                         TESTFOCUS

This example program demonstrates how to explicitly set focus to a specific control on a form depending on some contition in the program by using the Focus method of the control class. 

This form has on it three TextBox controls and a radiobutton group.
The focus will be set to one of the three TextBox controls depending on which radio button is selected.

The CheckChanged event is fired on a radio button whenever that state of the button is changed, on to off or off to on.
This is why we must use the Checked property to determine if the radio button was actually selected.


class-id
testfocus.Form1 is partial
    inherits type System.Windows.Forms.Form.

working-storage
section.
method-id NEW.
procedure division.

   invoke
self::InitializeComponent
   goback.

end
method.

method-id radioButton1_CheckedChanged final private.
procedure division using by value sender as object e as type System.EventArgs.

   if
radioButton1::Checked
      invoke textBox1::Focus
   end-if

end method.

method-id radioButton2_CheckedChanged final private.
procedure division using by value sender as object e as type System.EventArgs.

   if radioButton2::Checked
      invoke textBox2::Focus
   end-if

end
method.

method-id radioButton3_CheckedChanged final private.
procedure division using by value sender as object e as type System.EventArgs.

   if
radioButton3::Checked
      invoke textBox3::Focus
   end-if

end
method.
end class.