Ask the Expert

In C#, how do I set focus on first field and then, after barcode input, automatically focus this set

In C#, how do I set focus on first field (which is limited to 8 characters read by bar code) and then, after barcode input is done, automatically focus this set to field 2? Thanks your help.
You can't make it quite automatic, but if you can catch the event when barcode data is available, you set the control to have focus in your code using the ActiveControl property of the Form class:

class BarcodeForm : Form {
  ...
  void barCode1_DataAvailable(object sender, EventArgs e) {
    // Check that active control is a TextBox
    TextBox textBox = this.ActiveControl as TextBox;
    if( textBox == null ) return;

    // Get data from the barcode reader
    textBox.Text = GetBarcodeData();

    // Set the next active control
    if( textBox == textBox1 ) this.ActiveControl = textBox2;
  }
}

The ActiveControl property is the control that currently has focus, and changing it changes the control with focus.

This was first published in December 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: