none
Windows losing focus RRS feed

  • Question

  • Hey,

    I have a program that regularly accesses a database on the net. When it does this it brings up a small status window that says "Receiving data..." or something similar. It's really just to make the user aware of what the wait is. This status window is basically formed by spawning a thread that makes a new window. At the end of the database access the status window is closed.

    Anyway, I'm finding that sometimes the user's window (*not* the status window) loses focus and instead ends up on another window in the running program. Sometimes the entire program loses focus. This only happens after the status window is closed. If I don't use the status window this behaviour doesn't happen.

    Anyone know what's going on? I have looked at using the TopMost property, but it isn't really what I want. I'm finding users are complaining when they try to use other programs if I have the TopMost property set.

     

    Thanks

    Tuesday, September 28, 2010 6:06 AM

Answers

  • Well it seems to work. No funny behaviour on the windows (well, not yet and I've tested it enough times to be pretty sure).

     

    It doesn't work if I load the program through the debugger, but the users won't be doing this so I'm happy.

     

    Thanks Mike_999!

    • Marked as answer by BigBill Thursday, September 30, 2010 4:59 AM
    Thursday, September 30, 2010 4:59 AM

All replies

  • If you use Form.TopMost property, the form will overlap all other non-topmost forms, but also those from other applications. Instead of this, set the Form.Owner property to the parent form – the one which should be under the form (e.g. the main form).
    Tuesday, September 28, 2010 6:41 AM
  • I just tried adding the following line in the constructor, and it doesn't seem to be working:

     

    this.Owner = ParentForm;

    I also added it to some other portions of the code, in case the window needed to be fully displayed before you could set it.

    Tuesday, September 28, 2010 8:06 AM
  • How you are showing status dialog? Form.Show or Form.ShowDialog().

    If it is 'ShowDialog' , above settings will not work. If it is 'show' you can pass owner like this

    Form1 fm = new Form1()

    fm.Show(this);//here this will represent owner of the window, which you can get through property

    "fm.Owner"

    Tuesday, September 28, 2010 9:28 AM
  • It was originally using Application.Run(form) to open the window - I'm not sure why, I have inherited this code from somebody else. Since then I have tried both Show() and ShowDialog() (with no parameters passed). They all seem to be equally as bad.

    It's a bit hard for me to try fm.Show(this), because the status window is usually called from classes that are not forms. It just occurred to me that maybe this is the problem?

    Tuesday, September 28, 2010 11:15 AM
  • It was originally using Application.Run(form) to open the window - I'm not sure why, I have inherited this code from somebody else. Since then I have tried both Show() and ShowDialog() (with no parameters passed). They all seem to be equally as bad.

    It's a bit hard for me to try fm.Show(this), because the status window is usually called from classes that are not forms. It just occurred to me that maybe this is the problem?


    Can you post the code for launching status window?
    Wednesday, September 29, 2010 4:54 AM
  • The status window is called from a static class. The calling code is pretty simple:

    StatusWindow.ShowStatusWindow();
    StatusWindow.SetStatus("Retrieving data from database...");
    
    //Run database queries
    
    StatusWindow.CloseForm();
    

    The important methods in the StatusWindow class:

    static public void ShowStatusWindow()
    {
    	// Make sure it's only launched once.
    	if( ms_frmStatus != null )
    		return;
    	ms_oThread = new Thread( new ThreadStart(StatusWindow.ShowForm));
    	ms_oThread.IsBackground = true;
    	ms_oThread.ApartmentState = ApartmentState.STA;
    	ms_oThread.Start();
    }
    
    // A private entry point for the thread.
    static private void ShowForm()
    {
    	ms_frmStatus = new StatusWindow();
    	//Application.Run(ms_frmStatus);
            ms_frmStatus.ShowDialog();
    }
    
    // A static method to close the StatusWindow
    static public void CloseForm()
    {
    	if( ms_frmStatus != null && ms_frmStatus.IsDisposed == false )
    	{
    		// Make it start going away.
    		ms_frmStatus.m_dblOpacityIncrement = - ms_frmStatus.m_dblOpacityDecrement;
    	}
    		ms_oThread = null;	// we don't need these any more.
    		ms_frmStatus = null;
    }
    
    Wednesday, September 29, 2010 7:20 AM
  • Try 'TopLevel' property of the form. This is normally applicable for the main forms. Since your status dialog is main form it should be 'TopLevel'.

    Refrence http://msdn.microsoft.com/en-us/library/system.windows.forms.form.toplevel.aspx

    Wednesday, September 29, 2010 10:00 AM
  • Well it seems to work. No funny behaviour on the windows (well, not yet and I've tested it enough times to be pretty sure).

     

    It doesn't work if I load the program through the debugger, but the users won't be doing this so I'm happy.

     

    Thanks Mike_999!

    • Marked as answer by BigBill Thursday, September 30, 2010 4:59 AM
    Thursday, September 30, 2010 4:59 AM