vb.net vs2013 Setfocus not working

Hi, I am having issues to set focus on a textbox. What happens is that when the textbox is empty or null I would like to set the focus on the textbox so the user can add the text. Currently it is not doing it.
The code is the following:

 If (IsNumeric(txtPrecioCompetencia1.Text) = False) Then
            MessageBox.Show("Favor ingrese precio de Competencia 1")
            txtPrecioCompetencia1.Focus()

        ElseIf (String.IsNullOrEmpty(txtPrecioCompetencia1.Text) = False) Then
            MessageBox.Show("Favor ingrese precio de Competencia 1")
            txtPrecioCompetencia1.Focus()
        End If

Open in new window

LVL 2
José PerezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
You should not use Focus in .NET. According to the documentation: Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
0
José PerezAuthor Commented:
Tried both recommendations... still not working, my Textbox1 control still not focused.
0
Jacques Bourgeois (James Burger)PresidentCommented:
Reading carefully your code, it looks as if you inverted the logic in String.IsNullOrEmpty(txtPrecioCompetencia1.Text) = False

This means that it will trigger when the control is not NullOrEmpty. I understand that you want the reverse. Simply remove = False.
0
José PerezAuthor Commented:
ok it (more or less) works. Now it gives the 2 textbox messages instead of giving the first textbox message and focus on this textbox.

        If (IsNumeric(txtPrecioCompetencia1.Text)) Then
            MessageBox.Show("Favor ingrese precio de Competencia 1")
            txtPrecioCompetencia1.Select()

        ElseIf (String.IsNullOrEmpty(txtPrecioCompetencia1.Text)) Then
            MessageBox.Show("Favor ingrese precio de Competencia 1")
            txtPrecioCompetencia1.Select()
        End If

        If (IsNumeric(txtPrecioCompetencia2.Text)) Then
            MessageBox.Show("Favor ingrese precio de Competencia 2")
            txtPrecioCompetencia2.Select()
        ElseIf (String.IsNullOrEmpty(txtPrecioCompetencia2.Text)) Then
            MessageBox.Show("Favor ingrese precio de Competencia 2")
            txtPrecioCompetencia2.Select()
        End If

Open in new window

0
Jacques Bourgeois (James Burger)PresidentCommented:
Put an Exit Sub (or Exit Function) after the Select statements in the first block. It will skip the second one if both needs to trigger.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.