Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:29:29+00:00 2026-06-13T10:29:29+00:00

I have two forms, mainForm and subForm. When mainForm loses focus I want subForm

  • 0

I have two forms, mainForm and subForm. When mainForm loses focus I want subForm to disappear and then reappear as mainForm regains focus. I’m using the Activated and Deactivate events on the mainForm to keep track of whether mainForm has focus or not. When the Activated is fired I do subForm.Show() and the opposite for Deactivate. The problem I have is that when subForm gains focus mainForm disappear because I don’t know how to say programmatically “make subForm disappear when the mainForm’s Deactivate event fires except if it’s because the subForm gained focus. The whole point of what I’m doing is to make both windows disappear when the mainForm loses focus because the user clicked on another application or use ALT+TAB to switch. I don’t want to leave the subForm behind. Is there any way of checking as the Deactive fires whether it was because another form belonging to the application gained focus as opposed to some other application?

class MainForm : Form
{
    SubForm subForm = new SubForm();

    private void mainForm_Activated(object sender, EventArgs e)
    {
        this.subForm.Show();
    }

    private void mainForm_Deactivate(object sender, EventArgs e)
    {
        this.subForm.Hide()

        // I need some logic to make sure that it is only hidden
        // when the mainForm loses focus because the user clicked
        // some other application in the taskbar and not when the
        // subForm itself gains the focus.
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:29:30+00:00Added an answer on June 13, 2026 at 10:29 am

    This works on my machine.

    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
    
        private Form2 _form2;
        private void Form1_Load(object sender, EventArgs e) {
            _form2 = new Form2();
            _form2.Show();
            HandleFocusEvents();
        }
    
        private void HandleFocusEvents() {
            this.LostFocus += Form_LostFocus;
            _form2.LostFocus += Form_LostFocus;
            this.GotFocus += Form_GotFocus;
        }
    
        private void Form_LostFocus(object sender, EventArgs e) {
            if (!_form2.ContainsFocus && !this.ContainsFocus) {
                _form2.Hide();
            }
        }
    
        private void Form_GotFocus(object sender, EventArgs e) {
            if (!_form2.Visible) {
                _form2.Show();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application, I am using two forms mainform and subForm . The mainForm
I am a beginner in c#, I have two forms mainForm and subForm .
I have two forms. The first form is the mainForm, this never goes anywhere.
I have two forms on one page and want to have the input boxes
I have two forms. First one is to decide button numbers by using jslider.
So I have two forms. LibraryBookDialog.cs and MainForm.cs. I'm trying to pass an object
I have two forms: MainForm SettingsForm As you can imagine, MainForm uses values such
I have two forms named 'mainForm' and 'addRslt'. The idea is when users click
In C#, I have two Forms: mainForm and form1. class Form1; //... class mainForm
So I have two forms, mainform and extraform. extraform is set always moved to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.