Solved

how to check if window is on top, window = hasfocus? - c# form application

Posted on 2008-06-17
6
5,282 Views
Last Modified: 2012-08-14
i would like to set focus on a text field, when window(form) in on top of all on the desktop.

i have experimented with this ...
        private void Form1_Resize(object sender, System.EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                MessageBox.Show("Maximized");
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                MessageBox.Show("Minimized");
            }
            if (ActiveForm == this)
            {
                MessageBox.Show("activated");
                textBox2.Focus();
            }
        }

but this code only shows ...
"MessageBox.Show("Minimized");, window.

it only detects miinimized state, not the returning to maximiz state.
i would like to know how to detect if a form application is on top.

thank u.


i'm fairly a n oob in this materiaal.

c# vsX2008

kind regards
0
Comment
Question by:Natural_Demon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:Bruce_1975
ID: 21801455
You can override the OnActivated() Method of the form.

Regards,
Bruce
protected override void OnActivated(EventArgs e)
{
    textBox2.Focus();
}

Open in new window

0
 

Author Comment

by:Natural_Demon
ID: 21802266
ok, and how to the detect the contrary?
if it's behind another program?

i have tested something like this after looking in google.

        protected override void OnDeactivated(EventArgs e)
        {
            MessageBox.Show("hubabuba");  
        }

this give error.



0
 
LVL 6

Expert Comment

by:Bruce_1975
ID: 21802339
Thats a possible way that should work. What error do you get?

Regards,
Bruce
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Natural_Demon
ID: 21802556
Error      1      
'WindowsFormsApplication1.Form1.OnDeactivated(System.EventArgs)':
no suitable method found to override formtest

: (
i found the keyword on msdn after u mentioned the other
0
 
LVL 6

Accepted Solution

by:
Bruce_1975 earned 500 total points
ID: 21802594
Simple solution... you misspelled it (remove the d at the end).

Regards,
Bruce
protected override void OnDeactivate(EventArgs e)
{
    Debug.WriteLine("Deactivated");
    base.OnDeactivate(e);
}

Open in new window

0
 

Author Comment

by:Natural_Demon
ID: 21802798
thnix, orks fine, and i tested a bit.
        protected override void OnActivated(EventArgs e)
        {
            textBox3.Text = "hhhhi";
            textBox3.Focus();
        }
 
 
        protected override void OnDeactivate(EventArgs e)
        {
            textBox3.Text = "";
        }

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question