si2030
asked on
Forcing focus from a user control to the next control in a form....
Hi Experts,
I have a user control that has a combobox and a button.
This control sits on a main form with other controls and has been created PROGRAMMATICALLY on this form.
When I click on the button a window displays and you can pick a client. Once a client is picked and upon closing the popup window the combobox is populated via the valuemember... This all works great.
Once the combobox populates, the focus sits on the button that was initially clicked on.
However if I use the following code inside the usercontrol:
DirectCast(Me.Parent, frmCreditorInvoicePayment) .dtInvoice Date.Focus ()
it works. it moves to the next control on the parent....
My real problem is that this is Peculiar to the form "frmCreditorInvoicePayment " where as this usercontrol will be used on other forms.......
How can I make this usercontrol generically move to the next control outside???
Kind Regards
Simon
I have a user control that has a combobox and a button.
This control sits on a main form with other controls and has been created PROGRAMMATICALLY on this form.
When I click on the button a window displays and you can pick a client. Once a client is picked and upon closing the popup window the combobox is populated via the valuemember... This all works great.
Once the combobox populates, the focus sits on the button that was initially clicked on.
However if I use the following code inside the usercontrol:
DirectCast(Me.Parent, frmCreditorInvoicePayment)
it works. it moves to the next control on the parent....
My real problem is that this is Peculiar to the form "frmCreditorInvoicePayment
How can I make this usercontrol generically move to the next control outside???
Kind Regards
Simon
'Usercontrol created on the main form.
Dim thisUserControl As New uscClientSelect()
Me.Controls.Add(thisUserControl)
thisUserControl.Location = New Point(115, 15)
thisUserControl.TabIndex = 0
main-form.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi, Simon,
On further thought, I realized that the above assumes that the next control can assume the focus. You need to check for this. Perhaps something like:
Dim frmParent As Form = DirectCast(Me.Parent, Form)
Dim ctlNext As Control = frmParent.GetNextControl(M e, forward:=True)
Do Until (ctlNext Is Nothing OrElse ctlNext.CanFocus OrElse ctlNext Is Me)
ctlNext = frmParent.GetNextControl(c tlNext, forward:=True)
Loop
If (ctlNext Is Nothing) Then
' Leave the focus on the User control, or maybe rotate to
' the first control in the sequence that can take the focus.
Else
ctlNext.Focus()
End If
would be more complete.
Cheers,
Randy
On further thought, I realized that the above assumes that the next control can assume the focus. You need to check for this. Perhaps something like:
Dim frmParent As Form = DirectCast(Me.Parent, Form)
Dim ctlNext As Control = frmParent.GetNextControl(M
Do Until (ctlNext Is Nothing OrElse ctlNext.CanFocus OrElse ctlNext Is Me)
ctlNext = frmParent.GetNextControl(c
Loop
If (ctlNext Is Nothing) Then
' Leave the focus on the User control, or maybe rotate to
' the first control in the sequence that can take the focus.
Else
ctlNext.Focus()
End If
would be more complete.
Cheers,
Randy
ASKER
This worked perfectly. It didnt at first but all I did was adjust the tab order of the controls so the next control it went to was the date time picker. I have filed the other away if I need it to jump a few controls.
'Say the datetime picker is called dt1
dt1.focus