Detecting if my form is active

ajpri

New member
Joined
Sep 17, 2010
Messages
2
Programming Experience
1-3
Hello,
I am new to vb.net and what I want to do is change the bg color if my form is active.
I have tried searches and try and fail but I cant figure it out - please tell me im missing something easy.
 
If you want information about when something happens you should look to the events: Form Events (System.Windows.Forms)
You will find the first event listed very close to your request, and should be able to find the counterpart also.
 
i tried that before but no luck...is there an event like
If me.isactive = True then
me.backcolor = color
else
me.backcolor = othercolor
End if
 
I don't think that you actually did try what JohnH suggested. What you posted has nothing whatsoever to do with events. If you did what JohnH suggested then you'd have seen the Activated and Deactivate events of the form. Handle those events and do the appropriate thing in each one.
 
The event is called Activated, but you don't test for it like you're doing in your code. Instead, you "hook in" to the event.

You need to be in the editor, looking at the code for the form in question. At the top of the edit window, there are two drop down boxes. In the one on the left, click and look for the little lightning icon. It the one with the name in parenthesis. Select that one, and then on the right, select Activated.

That will create some code for you that looks like this.
VB.NET:
Private Sub YourForm_Activated _ 
                (ByVal sender As Object, ByVal e As System.EventArgs) _ 
                Handles Me.Activated
 
 
End Sub

And so that's where you put your me.backcolor = color, but you don't have to test for active. The code will only execute when the form is "Activated."

And now you might wonder how to change the color back when the form is no longer active. It's done the same way, but there's a different event. Hit the FI key when you're in Visual Studio and read up on the various form events. Good luck! ;)
 
Couldn't you also use Application.OpenForms() to see if the form is open?
 
Josh, hi, and yes, I think so, but isn't it better to put the code you want conditionally executed in a place the execution path is guaranteed to take when the desired condition happens? In other words, Application.OpenForms() works, but only when it is executed. What about the times the form is open but the code isn't looping through the OpenForms?
 
Oops! Sorry, I need to read posts better, I thought that he wanted to check if form x was open from another form, not activate on an event...

EDIT: What about the GotFocus() and LostFocus() events? Wouldn't they be more suited for this purpose, seeing as though the Activated() event would also be called if the program was doing some background work?
 
Last edited:
What about the GotFocus() and LostFocus() events? Wouldn't they be more suited for this purpose
No, Control.GotFocus is a low-level control event, documentation explains you should use Activated event for forms.
 
Thanks.

-Josh
 
Back
Top