Link to home
Create AccountLog in
Avatar of Natural_Demon
Natural_DemonFlag for Spain

asked on

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

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
Avatar of Bruce_1975
Bruce_1975

You can override the OnActivated() Method of the form.

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

Open in new window

Avatar of Natural_Demon

ASKER

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.



Thats a possible way that should work. What error do you get?

Regards,
Bruce
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
ASKER CERTIFIED SOLUTION
Avatar of Bruce_1975
Bruce_1975

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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