Discussion Locked

VB timer event

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I, a beginner with VB, developed a program that periodically checks a text file and uploads values into a form over a network. I am using a timer to run this code. The problem is, is that when the program is running but is minimized it steals the focus from the active application when the timed event runs. I want it to run in the background when it is not the active app,. How do I get it to do its job, without stealing focus? The event runs about every 30 seconds or so. Please include specific code syntax/object properties where possible.

Dan Kreiling

9 replies

by norbster

I just did a quick test and I found that if I SetFocus on a control on my minimized form, may test app would take the focus from other applications.

You do NOT need to SetFocus on a control to access it. So, check your code and make sure you are not using SetFocus in your timer event.

by dksj

I have not used the SetFocus method in any of my code. Actually I tried to use the LostFocus event associated with the form where the timer resides to disable the timer but it did not work. For some reason the LostFocus event is not running becauseit is not changing the timer enabled property to false. When I select another window I think that the LostFocus event should run.

by norbster

Well, there are two more things I can think of without having seen your code.
1. It may not be the timer that is causing your form to grab focus, it may be another control you are accessing during the timer event. Try changing the timer event to just put a string into your textbox and see if you still grab the focus. If you *do* still get the focus then the timer is not operating correctly, reinstall the latest service pack. If you *don't* get focus then add code back into the timer event until you find the line of code that is causing your app to get the focus.

2. If you are using directory, drive and file listboxes try using the Dir() function instead.

If the timer turns out to be the real problem, go to http://www.mvps.org/ccrp/ and download their timer, it works better than the intrinsic VB control.

by matthew.alverson

Forms do not get focus until they are the active form and have no controls on them. Use the activate and deactivate events instead of the got_focus and lost_focus events when working with forms. I just created a sample app that has a text box and a timer. It set the value of text1.text = "hello" when the timer activated. It did not steal the focus from the forground application. You will probably need to search elsewhere in your code. Look at how you are loading the data into the form.

by 3wadman

This is best way to avoid memmory leaks and system crashes from background processes in 95/98 applications.(But it will be hard for a beginner.)
Convert your form into a module and the data into a text file. (comma delimited maybe?)
You don't need a form.
Save and compile it as a class object. Register it with Regsvr32.exe by dragging it from one folder window to Regsvr32.exe in the Windows folder window.
Schedule the class object to run from the controle panel. (It will give back the memory when it's done)

This discussion is closed to new replies.