Solved

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

Posted on 2008-06-17
6
5,244 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
  • 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
Independent Software Vendors: 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

Industry Leaders: 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!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

680 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