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

Posted on 2009-02-12
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

'Usercontrol created on the main form.

Dim thisUserControl As New uscClientSelect()


        thisUserControl.Location = New Point(115, 15)

        thisUserControl.TabIndex = 0

Open in new window

Question by:si2030
    LVL 4

    Expert Comment

    If i am understanding correctly use the combobox's textchange event and add the following code:
    'Say the datetime picker is called dt1
    LVL 12

    Accepted Solution

    Hello, Simon,

    I think perhaps you are looking for something like:

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

    LVL 12

    Expert Comment

    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.


    Author Closing Comment

    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.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now