Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change focus from other thread

Status
Not open for further replies.

flbos

Programmer
Sep 15, 2005
41
NL
In my application I start an additional thread which continually checks the idle time of the system. If the system has been idle for more than 30 seconds, the thread navigates the application to the begin screen by calling the function "ToBegin()" of frmmain. This works fine except for the fact that I don't succeed in placing the focus to "text1"
I added the "frm.text1.Focus()" command but this does not work. If I add "text1.Focus()" at the end of the function "ToBegin()" this also does not work...

Anyone who knows how I can change the focus to "text1" in my situation?

I start the thread like this:

Code:
oIdle = New Idle
Dim t As New Threading.Thread(AddressOf oIdle.IdleTime)
oIdle.frm = Me
t.Start()
Application.DoEvents()

The code below shows the class I use for the extra thread.

Code:
Public Class Idle
    Dim FormIdle As New sfIdleTime.clsIdleTime
    Public frm As frmMain
    Public Event ThreadComplete()
    Public Sub IdleTime()
        Dim Time As Integer
        Dim Maxtime As Integer = 30
Begin:
        Time = FormIdle.GetIdleTime()
        If Time > Maxtime Then
            frm.ToBegin()
            frm.Focus()
            frm.text1.Focus()
            System.Threading.Thread.Sleep(10000)
            GoTo Begin
        Else
            GoTo Begin
        End If
    End Sub
End Class
 
Try putting 'Application.DoEvents' after your 'frm.text1.Focus', and it would be easer to put your loop like the following:

Code:
    Public Sub IdleTime()
        Dim Time As Integer
        Dim Maxtime As Integer = 30

        Do
            Time = FormIdle.GetIdleTime()
            If Time > Maxtime Then
                frm.ToBegin()

                [COLOR=green]'It would be better to put the following 2
                'lines in the ToBegin() sub/function[/color]
                frm.Focus()
                frm.text1.Focus()

                Application.DoEvents
                System.Threading.Thread.Sleep(10000)
             End If

             [COLOR=green]'The following line will stop your app from
             'hogging the cpu[/color]
             System.Threading.Thread.Sleep(100)
        Loop
    End Sub
 
thank you for your suggestion for improving the procedure!

I added Application.DoEvents() but this does not solve my problem, the textbox still does not get the focus..

Any other ideas?
 
Did you try to put the 'frm.Focus()' and 'frm.text1.Focus()' in the 'ToBegin()' sub/function? If not give it a try.

I had a simular problem, but the DoEvents() call fixed it.

I'll have a think about it, and come back to you...

One last question, what is the FormIdle doing at the time you want your other form (frm) to have focus? Does it have the focus?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top