11 Replies - 7015 Views - Last Post: 13 June 2011 - 12:25 PM Rate Topic: -----

#1 Cathy_T   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-January 11

How do I detect when focus is lost?

Posted 11 June 2011 - 07:04 PM

I've made a timer using windows forms. When the timer is no longer in focus or the user clicks on another application I want the timer to be shown in the task bar. I can't figure out how to do this. I've looked at LostFocus in the MSDN library but I guess I'm just not understanding how to get it to work.
Is This A Good Question/Topic? 0
  • +

Replies To: How do I detect when focus is lost?

#2 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How do I detect when focus is lost?

Posted 11 June 2011 - 07:30 PM

As far as I know, the timer isn't a GUI control but one that runs (exists) in the background. You should detect when the Form itself is out of focus and then set a system tray icon with the timer information.

What part do you need help with?
Was This Post Helpful? 0
  • +
  • -

#3 Cathy_T   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-January 11

Re: How do I detect when focus is lost?

Posted 11 June 2011 - 07:55 PM

The timer has buttons on it where the user can pause, stop, start, exit, etc... I'm having troubles figuring out when the form has lost focus or is out of focus. If the form is minimized I put it in the system tray. Currently, I'm not sure how, but when my timer loses focus it puts it in the system tray. I didn't put any code in that would put the timer in the system tray if focus is lost. I want to put the timer in the task bar if focus is lost. Does any of what I said make sense??
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How do I detect when focus is lost?

Posted 11 June 2011 - 08:35 PM

I was talking about this Timer.

To answer your question, you can detect when your Form loses focus.
Was This Post Helpful? 0
  • +
  • -

#5 Cathy_T   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-January 11

Re: How do I detect when focus is lost?

Posted 11 June 2011 - 09:11 PM

View PostSergio Tapia, on 11 June 2011 - 08:35 PM, said:

I was talking about this Timer.

To answer your question, you can detect when your Form loses focus.


As I stated previously I don't understand how to implement it. I already looked at the MSDN example but it doesn't make sense to me.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How do I detect when focus is lost?

Posted 11 June 2011 - 09:40 PM

Cathy... What Sergio is trying to say is that we have no idea what kind of timer you're talking about.

System.Windows.Forms.Timer is a .NET type that we are all familiar with but it does not have a GUI interface with buttons etc. as you have described.

So, obviously, you have made your own class/form/UserControl to be some sort of graphical timer or stopwatch.

But you need to provide the code for this as this is your own object, not a standard .NET type, and as such we don't know what you've written.

Quote

Currently, I'm not sure how, but when my timer loses focus it puts it in the system tray. I didn't put any code in that would put the timer in the system tray if focus is lost.


Well, someone did. That type of behavior does not happen automatically. It has to be added by the developer.

It sounds like a large chuck of your code was something you didn't write, but got from someone else, such as a schoolmate, workmate, on-line code... something like that. Is that right?

You'll need to show us the code for your project before we can even begin to decipher what's going on.
Was This Post Helpful? 0
  • +
  • -

#7 Cathy_T   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-January 11

Re: How do I detect when focus is lost?

Posted 11 June 2011 - 10:39 PM

Okay, I have a timer that I've created using a windows form and the clock timer.

Here is some of my code

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (bPaused == true)
            {
                ClockTimer.Enabled = true;
                btnPaused.Enabled = true;       // Enable Pause button
                btnStop.Enabled = true;         // Enable Stop button
                btnStart.Enabled = false;       // Disable Start button
                btnDone.Enabled = false;        // Disable Done button
            }// end if
            else
            {
                ClockTimer.Enabled = true;
                btnPaused.Enabled = true;       // Enable Pause button
                btnStart.Enabled = false;       // Disable start button
                btnDone.Enabled = false;        // Disable Done button
            }// end else
        }// end btnStart_Click

        private void ClockTimer_Tick(object sender, EventArgs e)
        {
            // Verify if the timer is done counting down
            if ((iMinutes == 0) && (iSeconds == 0))
            {

                // If window is minimized make it normal sized
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.WindowState = FormWindowState.Normal;
                    SysTrayIcon.Visible = false;
                }// end if

                // Activate the form.
                this.Activate();
                
                ClockTimer.Enabled = false;
                btnStop.Enabled = false;        // Disable Stop button
                btnPaused.Enabled = false;      // Disable Pause button
                btnStart.Enabled = true;        // Enable Start button
                btnDone.Enabled = true;             // Disable Done button
                lblMin.Text = "00";
                lblSec.Text = "00";
                player.Play();                  // Play sound / wave 
                iSeconds = 0;
                iMinutes = 25;
            }// end if
            else
            {
                btnStop.Enabled = true;
                // Else continue counting down
                //  Is it time for the seconds to start over at 59?
                if (iSeconds == 0)
                {
                    iSeconds = 59;

                    // Is it time for the minutes to start over at 59?
                    if (iMinutes == 0)
                    {
                        iMinutes = 59;
                    }// end if
                    // Else subtract 1 from the minutes
                    else
                    {
                        iMinutes--;
                    }// end else
                }// end if
                // Else subtract 1 from the seconds
                else
                {
                    iSeconds--;
                }// end else

                // Display the current value of minutes and seconds in the
                //  corresponding fields.

                // If minutes < 10 append a 0 to the front of the string
                if (iMinutes <= 9)
                {
                    lblMin.Text = "0" + iMinutes.ToString();
                }// end if
                // Else convert the minutes from an integer to a string
                else
                {
                    lblMin.Text = iMinutes.ToString();
                }//end else

                // If seconds < 10 append a 0 to the front of the string
                if (iSeconds <= 9)
                {
                    lblSec.Text = "0" + iSeconds.ToString();
                }// end if
                // Else convert the seconds from an integer to a string
                else
                {
                    lblSec.Text = iSeconds.ToString();
                }// end else
            }// end else
        }// end ClockTimer_Tick

        private void SysTrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // If window is minimized make it normal sized
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = FormWindowState.Normal;
                SysTrayIcon.Visible = false;
            }// end if

            // Activate the form.
            this.Activate();
        }// end SysTrayIcon_MouseDoubleClick


        private void ETForm_SizeChanged(object sender, EventArgs e)
        {
            // If window is minimized make the system tray icon visible
            if (this.WindowState == FormWindowState.Minimized)
            {
                SysTrayIcon.Visible = true;
            }// end if
        }// end ETForm_SizeChanged


There is more code of course that handles the events for when the stop button is pressed, pause button and done button. Is this helpful for answering my question?

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 12 June 2011 - 05:51 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How do I detect when focus is lost?

Posted 12 June 2011 - 08:22 AM

Let's talk a bit about cleaning up some of this...

if (bPaused == true)
            {
                ClockTimer.Enabled = true;
                btnPaused.Enabled = true;       // Enable Pause button
                btnStop.Enabled = true;         // Enable Stop button
                btnStart.Enabled = false;       // Disable Start button
                btnDone.Enabled = false;        // Disable Done button
            }// end if
            else
            {
                ClockTimer.Enabled = true;
                btnPaused.Enabled = true;       // Enable Pause button
                btnStart.Enabled = false;       // Disable start button
                btnDone.Enabled = false;        // Disable Done button


Can become smarter and more elegant. For one we can get rid of the if all together. And get rid of the duplicate lines. We don't like duplicate code.

                ClockTimer.Enabled = bPaused;
                btnPaused.Enabled = bPaused;       // Enable Pause button
                btnStop.Enabled = bPaused;         // Enable Stop button
                btnStart.Enabled = !bPaused;       // Disable Start button
                btnDone.Enabled = !bPaused;        // Disable Done button




Quote

I didn't put any code in that would put the timer in the system tray if focus is lost. I want to put the timer in the task bar if focus is lost.

To have your form show in the Task Bar all you have to do is set that property to true.

From within your form
this.ShowInTaskBar = true;

From there you could hide the form
this.Hide();

Just make sure you have a way to .Show() it again, like making the TaskTry icon & menu visible so you can pick a menu option to show it.

Is it possible that when you are loosing focus you are minmizing the form in order to 'put it in the taskbar'? Which then triggers the handler for when you minimize, putting it in the TaskTray? You do realize that the minimize event is raised no matter how the form is minimized, right? Not just by the user clicking the 'minimize' widget, but also if you minimize programmatically.
Was This Post Helpful? 3
  • +
  • -

#9 Cathy_T   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-January 11

Re: How do I detect when focus is lost?

Posted 12 June 2011 - 12:28 PM

View PostCathy_T, on 11 June 2011 - 10:39 PM, said:

MOD EDIT: When posting code...USE CODE TAGS!!!


Sorry! :-(
Was This Post Helpful? 0
  • +
  • -

#10 Cathy_T   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-January 11

Re: How do I detect when focus is lost?

Posted 12 June 2011 - 12:44 PM

View PosttlhIn`toq, on 12 June 2011 - 08:22 AM, said:

Let's talk a bit about cleaning up some of this...

if (bPaused == true)
            {
                ClockTimer.Enabled = true;
                btnPaused.Enabled = true;       // Enable Pause button
                btnStop.Enabled = true;         // Enable Stop button
                btnStart.Enabled = false;       // Disable Start button
                btnDone.Enabled = false;        // Disable Done button
            }// end if
            else
            {
                ClockTimer.Enabled = true;
                btnPaused.Enabled = true;       // Enable Pause button
                btnStart.Enabled = false;       // Disable start button
                btnDone.Enabled = false;        // Disable Done button


Can become smarter and more elegant. For one we can get rid of the if all together. And get rid of the duplicate lines. We don't like duplicate code.

                ClockTimer.Enabled = bPaused;
                btnPaused.Enabled = bPaused;       // Enable Pause button
                btnStop.Enabled = bPaused;         // Enable Stop button
                btnStart.Enabled = !bPaused;       // Disable Start button
                btnDone.Enabled = !bPaused;        // Disable Done button




Quote

I didn't put any code in that would put the timer in the system tray if focus is lost. I want to put the timer in the task bar if focus is lost.

To have your form show in the Task Bar all you have to do is set that property to true.

From within your form
this.ShowInTaskBar = true;

From there you could hide the form
this.Hide();

Just make sure you have a way to .Show() it again, like making the TaskTry icon & menu visible so you can pick a menu option to show it.

Is it possible that when you are loosing focus you are minmizing the form in order to 'put it in the taskbar'? Which then triggers the handler for when you minimize, putting it in the TaskTray? You do realize that the minimize event is raised no matter how the form is minimized, right? Not just by the user clicking the 'minimize' widget, but also if you minimize programmatically.


Okay, I see what you are saying about the duplicate lines.

No, I only put it in the system tray if it's been minimized not if it loses focus. Yes, I realize that it can be "minimized" either by the user or programmatically but I didn't program it to minimize upon focus lost. I want to put the timer in the system tray if it's been minimized by the user however if the timer loses focus I wanted to put it in the taskbar.
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How do I detect when focus is lost?

Posted 13 June 2011 - 04:58 AM

I'm not sure what was so hard to understand about what the OP was saying. She(assuming she by the name) created an APPLICATION that is a timer. She is not referring to the various Timer classes/controls in .Net. I was able to see this from the first post.

To determine when your application loses focus, you can use the Deactivated event for the form. This will fire when the form loses focus and is no longer the active form.
Was This Post Helpful? 2
  • +
  • -

#12 Cathy_T   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-January 11

Re: How do I detect when focus is lost?

Posted 13 June 2011 - 12:25 PM

Cool! Thanks eclipsed4utoo! That should work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1