3

I am working in windows form application using c#. I have two fields in a form "Username" and "password" and two buttons "Login" and "Cancel". On form loading cursor is placed in Username textbox which is ok.

But when I press TAB from my keyboard instead of going to Password textbox its going to Login button. How can I set this?

Also the shortcut CTRL+A to select all text is not working in text fields. Any help?

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
SidraM
  • 305
  • 2
  • 4
  • 15
  • 1
    You should set TabIndex right. It gives you the sequence of which tab is next when Tab button on keyboard is pressed. (Note*: It is in the comments cause it does not completely answer OPS whole question) – P. Pat Mar 10 '17 at 07:45
  • You need to adjust tab order for your controls: https://msdn.microsoft.com/en-us/library/csz6b8x8.aspx – Dmitry Egorov Mar 10 '17 at 07:47
  • ok i set TabIndex to 2.... its working fine now :) Thanks – SidraM Mar 10 '17 at 07:48

4 Answers4

6

Every control on WinForms has a TabIndex. This you can find in the properties of the control. You need to adjust this value to 2 in the Password textbox if you want to have the focus switch there after tab

Select your control in the designer, go into the properties:

enter image description here

and set the TabIndex property:

enter image description here

The shortcut Cntr + A will only work if you have the focus on the TextBox! Also make sure that the ShortCutsEnabled property is set to true:

enter image description here

For reference here is the original answer for the shortcut problem. It says however that:

The TextBox control does not support the CTRL+A shortcut key when the Multiline property value is true.

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • yes, it resolved this.. thanks and what about keyboard shortcuts like CTRL+A – SidraM Mar 10 '17 at 07:50
  • @user3837889 not enough information about what you did so that this short cut does not work anymore. – Mong Zhu Mar 10 '17 at 07:51
  • @user3837889 it works only if you have the focus on the textbox. Did you override any of the key press or down events?# – Mong Zhu Mar 10 '17 at 07:54
  • @user3837889 please check the `ShortCutsEnabled` property of the textbox. I made an edit – Mong Zhu Mar 10 '17 at 07:58
  • 1
    yeah I just checked that and found that due to setting the multiline property to true CTRL+A is not working. – SidraM Mar 10 '17 at 07:59
2

Change the tabindex property of your password textbox 1 higher to the tabindex of your username textbox. (i.e. Username tabindex = 1, password tebaindex = 2)

imprezzeb
  • 706
  • 1
  • 7
  • 18
2

The order of the 'tabs' is set by setting 'TabIndex' eg.

controlName.TabIndex = 0;
EpicKip
  • 4,015
  • 1
  • 20
  • 37
2

Here is the answer to your second question regarding using Ctrl + A to select text in a TextBox.

To do this, you'll need to hook into the KeyDown event of the TextBox. This is how you'd select the text:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.A) // Select All
    {
        ((TextBox)sender).SelectAll();
        e.SuppressKeyPress = true;
        e.Handled = true;
    }
}
Overdrive77
  • 115
  • 2
  • 9