José Perez
asked on
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:
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
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.
ASKER
Tried both recommendations... still not working, my Textbox1 control still not focused.
Reading carefully your code, it looks as if you inverted the logic in String.IsNullOrEmpty(txtPr ecioCompet encia1.Tex t) = False
This means that it will trigger when the control is not NullOrEmpty. I understand that you want the reverse. Simply remove = False.
This means that it will trigger when the control is not NullOrEmpty. I understand that you want the reverse. Simply remove = False.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.