We help IT Professionals succeed at work.

Forcing focus from a user control to the next control in a form....

si2030
si2030 asked
on
Medium Priority
280 Views
Last Modified: 2012-05-06
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
Comment
Watch Question

John RugoSenior SQL DBA

Commented:
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
 
Developer
CERTIFIED EXPERT
Commented:
Hello, Simon,

I think perhaps you are looking for something like:

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

Cheers,
Randy

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
omegaomegaDeveloper
CERTIFIED EXPERT

Commented:
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

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.