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).dtInvoiceDate.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

'Usercontrol created on the main form.
Dim thisUserControl As New uscClientSelect()
        thisUserControl.Location = New Point(115, 15)
        thisUserControl.TabIndex = 0

Open in new window

Who is Participating?
omegaomegaConnect With a Mentor DeveloperCommented:
Hello, Simon,

I think perhaps you are looking for something like:

        DirectCast(Me.Parent, Form).GetNextControl(Me, forward:=True).Focus()

If i am understanding correctly use the combobox's textchange event and add the following code:
'Say the datetime picker is called dt1
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(Me, forward:=True)
        Do Until (ctlNext Is Nothing OrElse ctlNext.CanFocus OrElse ctlNext Is Me)
            ctlNext = frmParent.GetNextControl(ctlNext, forward:=True)
        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.
        End If

would be more complete.

si2030Author Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.