Link to home
Start Free TrialLog in
Avatar of si2030
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).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

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

Open in new window

main-form.jpg
Avatar of CodeJunky
CodeJunky
Flag of United States of America image

If i am understanding correctly use the combobox's textchange event and add the following code:
'Say the datetime picker is called dt1
dt1.focus
 
ASKER CERTIFIED SOLUTION
Avatar of omegaomega
omegaomega
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
        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
Avatar of si2030
si2030

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.