hi,

In my c # Sharp application,

i want to use the arrow key as a short cut,

if i press left arrow means the cursor has to move to the left side text box , if i press the right arrow key means the cursor has to focus the right side text box.

in which event i have to right and can u help me ?

Recommended Answers

All 8 Replies

This only works when text is in the textbox.

First create a KeyDown event handler:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
      {
         if (e.KeyCode.Equals(Keys.Right))
         {
            e.Handled = true;
            SendKeys.Send("{END}");
         }

         if (e.KeyCode.Equals(Keys.Left))
         {
            e.Handled = true;
            SendKeys.Send("{HOME}");
         }
      }

Then add the handler to the "Designer.cs" code:

this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);

no....
this is not working....
i want the code to move from one text box to another text box when i m clicking the left arrow.?
is there any way

That's a little different.
Let's say you have two text boxes -- with the IDs of textBoxLeft and textBoxRight
I would try something like this:

private void textBoxLeft_KeyDown(object sender, KeyEventArgs e)
      {
         if (e.KeyCode.Equals(Keys.Right))
         {
            e.Handled = true;
            SendKeys.Send("{TAB}");
         }
      }

      private void textBoxRight_KeyDown(object sender, KeyEventArgs e)
      {
         if (e.KeyCode.Equals(Keys.Left))
         {
            e.Handled = true;
            SendKeys.Send("+{TAB}");
         }
      }

and the handler stubs like this:

this.textBoxLeft.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBoxLeft_KeyDown);
//...
this.textBoxRight.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBoxRight_KeyDown);

Kavi4u, you do know this functionality is provided by tab (forward) and shift-tab (back) right? How are going to handle a user wanting to move the cursor back inside the current text box to correct a typo?

Use your form's key down event instead of the text box. Then tell which text box to focus when a different key is pressed.

//pseudo not acutal code;
private void this_KeyDown(object sender, KeyEventArgs e)
{
 if (e.KeyCode == Keys.Left)
   leftBox.Focused = true;

 else if (e.KeyCode == Keys.Right)
   rightBox.Focused = true;
}

Something of that nature; then to up your game make it so that the application remembers the user's last position in the text boxes.

@lxXTaCoXxl: the .Focused property of the text box is read-only, so that will not work.

However, this will:

private void textBoxLeft_KeyDown(object sender, KeyEventArgs e)
      {
         if (e.KeyCode.Equals(Keys.Right))
         {
            e.Handled = true;
            //SendKeys.Send("{TAB}");
            textBoxRight.Focus();
         }
      }

      private void textBoxRight_KeyDown(object sender, KeyEventArgs e)
      {
         if (e.KeyCode.Equals(Keys.Left))
         {
            e.Handled = true;
            //SendKeys.Send("+{TAB}");
            textBoxLeft.Focus();
         }
      }

I also assumed this was to work ONLY when one of the text boxes already has focus.

I don't mess with focus lol. I don't really care for making apps that deal with it. I said it was pseudo code. That doesn't mean that you will be using that property. We can't give everything out can we? lol

you must derive a new class that is based on the class of the control that you want, and you override the ProcessCmdKey().

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    //handle your keys here
}

Source : C# Arrow Key Press

Vayne

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.