Question

This may be a simple question. I'm new to C# (and most programming) and I am trying to make a program that consists of two text boxes. The information in these textboxes will frequently be deleted and new information will need entered, so it needs to be quick. For convenience I'm trying to make the backspace key refocus on the previous textbox rather than using Shift+Space or clicking. Here is what I have. The program runs, but the code below doesn't seem to do what I intend it to do.

if (e.KeyCode == Keys.Back && textBox2.TextLength == 0)
  textBox1.Focus();

So, when textbox2 has 0 characters and backspace is subsequently keyed, I would like it to move back to textbox1. Thanks for any help.

Was it helpful?

Solution 2

I did get it to work finally. What I did wasn't too different than what I was trying before, but here is how I did it.

  1. I created two text boxes from the .cs [Design] view.
  2. I selected each box and clicked the "Events" lightning bolt icon under Properties. This is something I left out previously when this didn't work.
  3. I set both of them to KeyPress and TextChanged (i.e. textBox1_KeyPress & textBox1_TextChanged (Did the same thing with textBox2). I don't know if this is part of the reason why it worked. I'm just documenting my actions.
  4. I double clicked each textBox, which created an EventArgs for each. This is where I stored my regular code.
  5. In addition to the EventArgs I manually created a KeyEventArgs (see below) where I put the function for the Backspace. Here is the code:
// Here is the KeyEventArgs I created using KeyPress (Public).

public void textBox2_KeyPress(object sender, KeyEventArgs e)

       {

           if (e.KeyCode == Keys.Back && textBox2.Text.Length == 0)
               textBox1.Focus();
       }


// Here is where the rest of my code (Private).

private void textBox2_TextChanged(object sender, EventArgs e)

           {
           if (textBox2.Text == "A")
               richTextBox3.Text = "January";

           if (textBox2.Text == "B")
               richTextBox3.Text = "February";

           if (textBox2.Text == "C")
               richTextBox3.Text = "March";

// Code Continues...

Sorry if this isn't clear. I just wanted to document what I did in case it helps someone later. Thanks for the help everyone.

OTHER TIPS

So to make this work you're going to need to make sure you run this code on KeyUp, but you also do not need multiple KeyUp handlers to do this. Consider this KeyUp handler:

private void textBox_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
    var textBox = sender as TextBox;
    if (textBox == null) { return; }

    if (e.KeyCode == Keys.Back && textBox.Text.Length == 0)
    {
        // this here of course being the Form
        // Select causes the form to select the previous control in the tab order
        this.Select(true, false);
    }
}

Now just attach this handler to all text boxes that you want to behave this way and they'll all work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top