Solved

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

Posted on 2008-06-17
6
5,150 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now