0

I am using the following code for goto next and previous controls in windows form

if (e.KeyCode == Keys.Enter)//Next
{
    this.SelectNextControl(this.ActiveControl, true, true, true, true);
}
if (e.KeyCode == Keys.Back)//Previous
{
    this.SelectNextControl(this.ActiveControl, false, true, true, true);
}

The problem in

if (e.KeyCode == Keys.Back)

If current control is a TextBox and the user tries to type and delete one word using backspace, that time this will work and set the focus to the next control.

How to prevent this event when textbox have some values? And it should work for ComboBox and other controls

Suresh
  • 116
  • 9

3 Answers3

1

Another update, probably not as good, but works. I would recommend creating a method:

public void checkKey(object sender)
{
    KeyEventArgs a = new KeyEventArgs(Keys.Back);
    if (sender.GetType().Name == "TextBox")
    {
        TextBox tb = (TextBox)sender;
        if ((a.KeyCode == Keys.Back && String.IsNullOrWhiteSpace(tb.Text)))
        {
            this.SelectNextControl(this.ActiveControl, false, true, true, true);
        }
    }
}

And then on all the textboxes TextChanged events call checkKey(sender);

If you need to add controls, just add it in the if condition.

The event that you are using checks if a key on the Form is pressed and not on the textbox.

LjCode
  • 292
  • 2
  • 12
  • I doubt it, needs some modifying to do that as well. – LjCode Jul 25 '13 at 07:48
  • but how? @Luame Ludik – Suresh Jul 25 '13 at 07:59
  • using the `object sender` will determine what type of control is sending the event. textbox in this case. will work anywhere in the form **including** groupboxes. – LjCode Jul 25 '13 at 08:26
  • `if (e.KeyCode == Keys.Back) { if (sender.GetType().Name == "TextBox") { TextBox tb = (TextBox)sender; if ((e.KeyCode == Keys.Back && String.IsNullOrEmpty(tb.Text))) { this.SelectNextControl(this.ActiveControl, false, true, true, true); } } else this.SelectNextControl(this.ActiveControl, false, true, true, true); }` in this code not working(textbox and combobox are there) – Suresh Jul 25 '13 at 08:33
  • Ok, made another change, probably not as fast, but it works. (tested) – LjCode Jul 25 '13 at 09:08
0

Most people use reverse tab for back in win form . but if you still want to do it by back button .

This cod will help you . and you never loss your lest char .

if(e.KeyCode==Keys.Back)
            {
                if(textBox1.Text.Trim().Length==0){
                this.SelectNextControl(this.ActiveControl, false, true, true, true);
                }
            }

it my pleasure to help you . welcome for your thanks

sourabh devpura
  • 605
  • 8
  • 25
0

ok then first create a new function

public TextBoxBackEvent(TextBox tb,Form form){
               if(tb.Text.Trim().Length==0){
                form.SelectNextControl(form, false, true, true, true);
}

now use this function in any where in your application

like

  TextBoxBackEvent(textbox,this);

and it will work exactly witch you want

sourabh devpura
  • 605
  • 8
  • 25