Problem when changing field focus

Asked

Viewed 86 times

4

I’m having a problem changing the input focus between the TextBox of my application.

The problem is actually this: tb_Estados has a AutoCompleteMode = AutoCompleteMode.Append and a AutoCompleteSource = AutoCompleteSource.CustomSource. So far, no problem.

Things start to go wrong on the principle that when trying to change textbox with by pressing enter in the event KeyPress of TextBox, simply "nothing happens".

private void tb_Estados_KeyPress_1(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar == ((char)(Keys.Enter))))
    {
        tb_Cidades.Focus();
    }
}

The focus doesn’t change to the tb_Cidades, but it does remain in the tb_Estados.

What I noticed occurs is that the text tb_Estados is all selected by pressing the Enter key instead of changing the input focus. That is, it does not change the focus, it just selects (if there is something inserted) the text of the tb_Estado. If you have nothing inserted, it simply selects the " " and does not switch to the tb_Cidades.

I tried to use the .Select() instead of .Focus(), but it didn’t help.

I also tried to use the SendKeys to send the command to {"Tab"}, but it didn’t work either.

I appreciate the attention and possible solutions of all!

  • Try putting a e.Cancel = true after tb_Cidades.Focus();

  • You can always try SelectNextControl(tb_Estados, true, true, true, true);. But anyway it seems to me that there is some property in control tb_Cidades that is preventing the focus.

  • Try putting in Keydown if (e.Keycode == Keys.Enter) { Sendkeys.Send("{TAB}"); e.Handled = true; }

2 answers

3


Apparently there is a bug that has not been fixed by microsoft on the AutoCompleteSource.

I did a very simple test and actually using the textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; the KeyPress crashing.

If you comment on the line the event KeyPress comes back to work.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        textBox1.AutoCompleteMode = AutoCompleteMode.Append;
        textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar == ((char)(Keys.Enter))))
        {
            textBox2.Select();
        }
    }
}

I found this related to the problem, and apparently there was a microsoft link where it was reported, but the link no longer exists. I don’t know if this is really a bug, but this property really disables the KeyPress. Anyway, for your case it’s best to use the KeyDown.

Example with Keydown:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        textBox1.AutoCompleteMode = AutoCompleteMode.Append;
        textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            textBox2.Focus();
        }
    }
}

And here you find an interesting reading on the difference between Keydown and Keypress.

1

I really appreciate all your help. As stated in the previous reply, it appears to be a Microsoft bug that has not yet been fixed in relation to AutoCompleteSource.

The solution I used, through all the answers was:

At the event KeyPress:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar == ((char)(Keys.Enter))))
    {
        textBox2.Select();
    }
}

and at the event KeyDown:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        textBox2.Focus();
    }
}

The detail was that I needed to use in both events, not only in one or the other. Anyway, I am grateful to all those who went to help me!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.