Solved

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

Posted on 2008-06-17
6
5,337 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

622 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