Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

I have form with two TEXTBOX: txtb1 and txtb2.
I want to create a shortcut for the both textbox like this:
when I press ALT+1 the txtb1 TEXTBOX get the focus and when I press ALT+2 the
focus go to txtb2 TEXTBOX.

any suggestion?
Posted

AccesKey property of UIControl class can help to resolve your problem. Sample code is:

VB
<asp:TextBox id="TextBox1"
  AccessKey="Y"
  Text="Press Alt-Y to get focus here"
  Columns="45"
  runat="server"/>
 
Share this answer
 
Override ProcessCmdKey..

C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
      {
          if (keyData == (Keys.Alt | Keys.D1))
          {
              textBox1.Focus();
          }
          else if (keyData == (Keys.Alt | Keys.D2))
          {
              textBox2.Focus();
          }

          return base.ProcessCmdKey(ref msg, keyData);
      }
 
Share this answer
 
I succeed to solve it in my self:

You first need to set the keypreview of the form to true.

VB
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If Control.ModifierKeys = Keys.Alt Then
            If e.KeyCode = Keys.D1 Then
                TextBox1.Focus()
            ElseIf e.KeyCode = Keys.D2 Then
                TextBox2.Focus()
            End If
        End If
    End Sub
 
Share this answer
 
v3
Write the following codes
--------------------
C#
label1.Text = "lable&1";
label1.TabIndex = 0;
textBox1.TabIndex = 1;


At run time when you press Alt+1, it will focus on TextBox.

If there is any Button control then write as
button1.Text = "button&2";

If you press Alt+2 at run time, it is selected.
 
Share this answer
 
How I implement that in form application?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900