Wednesday, January 19, 2005
SelectNextControl method
Recently I had a need to switch focus to the next control on a form when the Enter key was pressed, under certain conditions. It seemed easy enough - just call SelectNextControl to have .Net read the tab order and select the next one in order, right? Yes, it was that easy, but it took a few attempts before I finally figured out how the SelectNextControl method worked!
SelectNextControl is a Control instance method, however, it also takes a Control as its first parameter. Initially I attempted to run it on the control that currently had focus, but couldn't figure out what to pass as the first parameter! Finally I realized that the method was intended to be run on the parent control, and the first parameter was the Control after which to select the next one. In my case, I needed to call the method on the Form, and pass it the control that had current focus.
To summarize, SelectNextControl is called on a parent control, such as a form. The first parameter is the control that you want to move away from, such as the currenntly selected control.
SelectNextControl is a Control instance method, however, it also takes a Control as its first parameter. Initially I attempted to run it on the control that currently had focus, but couldn't figure out what to pass as the first parameter! Finally I realized that the method was intended to be run on the parent control, and the first parameter was the Control after which to select the next one. In my case, I needed to call the method on the Form, and pass it the control that had current focus.
To summarize, SelectNextControl is called on a parent control, such as a form. The first parameter is the control that you want to move away from, such as the currenntly selected control.