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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing if form is active

Status
Not open for further replies.

dbsquared

Programmer
Nov 7, 2002
175
US
This is sort of an expansion on the issue I was haveing with the timer. I got the timer to work now I have an issue if the form isn't in focus (say the screensaver kicked on or you bring up another program or something) then the code crashes.

When the timer expires it calls this sub

Shared Sub CheckStatus(ByVal State As Object)
Dim myform As Form

myform = frmMain.ActiveForm
myform.Activate()
myform.Refresh()
'MessageBox.Show("I am refreshed")
myform = Nothing
End Sub

the problem is if frmMain is not the active form then myform doesn't get set and crashes when it tries to refresh it.
My question is how can I test if frmMain is active and if it is refresh it if not go on doing whatever?

Thanks

To go where no programmer has gone before.
 
I don't really understand why you want it to be active or why you can't reference your form even if the screensaver is on. The program should just continue in the background. The program can even change a form that hasn't been shown yet. so I think you are doing something wrong somewhere else.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I turned off everything else but this, and still everytime I had another screen open or let the screen saver hit the code crashed at the myform.refresh(). Because myform would equal nothing when the form wasn't the active in the system.

would a try, catch capture this?

To go where no programmer has gone before.
 
Just to make sure I understand.

You have a form.
You want to refresh the form at regular intervals.
If the form is not active, you do not want to refresh it.

correct?

then this should work:
Code:
Shared Sub CheckStatus(ByVal State As Object)
  Dim myform As Form = frmMain.ActiveForm
  if not myform is nothing then
    myform.Activate()
    myform.Refresh()
    myform = Nothing
  end if
End Sub

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Here is what I did is this and it works but I don't think it is the most elegant.

Dim myform As Form
Try
myform = frmMain.ActiveForm
myform.Refresh()
myform = Nothing
Catch eException As Exception
End Try

To go where no programmer has gone before.
 
BTW how do you put the code into the box?

To go where no programmer has gone before.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top