How do we set field focus

dominico

Well-known member
Joined
Mar 9, 2005
Messages
57
Programming Experience
3-5
Hi all,

I have a combobox and I would like to set input focus to it in form load. However, according to VB, in order for a control to receive input focus, the control must have a handle assigned to it, and the Visible and Enabled properties must both be set to true.

All I need is to have a handle assigned to it but I couldn't do it.

Please help.

Thanks.

dominico
 
Last edited:
There are two ways. You can rearrange your tab order so that the control you want to be focused has the lowest value, or you can use the Activated event like this:
VB.NET:
	Private firstActivation As Boolean = True

	Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
		If Me.firstActivation Then
			Me.TextBox1.Focus()
			Me.firstActivation = False
		End If
	End Sub
 
If you don't use the firstActivation variable then the TextBox will be focused every time the form is activated. That would mean that every time you switched to another application or another form within the same application and then back again, that TextBox would receive focus, which is probably not desirable.
 
jmcilhinney said:
There are two ways. You can rearrange your tab order so that the control you want to be focused has the lowest value, or you can use the Activated event like this:
VB.NET:
	Private firstActivation As Boolean = True
 
	Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
		If Me.firstActivation Then
			Me.TextBox1.Focus()
			Me.firstActivation = False
		End If
	End Sub

Thank you jmcilhinney sir!

you're the best! :)

dominico
 
Back
Top