locked
Form is appearing behind another form?

    Question

  • I have a form that on the textchanged event of a textbox should appear.  The textBox is pupulated on the form load even from a .txt file.  Both forms are set to TopMost = False.  Still, for some reason, this form is opening behind the main form.  Does anyone have any ideas as to why this might be?  All of the other forms work fine.

    Thanks,
    Tuesday, May 15, 2007 7:58 PM

Answers

  • The reason why the second form is behind the main form, is because the second form is being created before the main form has been created. This is  because the "TextChanged event handler" is being processed before the "Load event handler", for the main form, has finished processing.

     

    You could move the textbox init code to the main form "activated" event handler, and  introduce a boolean flag to ensure that the init code is ran only once.

    Tuesday, May 15, 2007 8:40 PM
  • What ArranNyc is getting at is this:

    • Create a form-level variable - e.g. bool _isInitialized;
    • Within your "Activated" event handler, check to see if _isInitialized is false (e.g. if (!_isInitialized) {...) and update your textbox or whatever else you need to do.
    • At the end of your "Activated" event handler, set _isInitialized to true (e.g. _isInitialized = true;).  That way, the next time the "Activated" event handler is called it won't run through your initialization code again.

    Hope this helps.

    Wednesday, May 16, 2007 5:20 PM

All replies

  • The reason why the second form is behind the main form, is because the second form is being created before the main form has been created. This is  because the "TextChanged event handler" is being processed before the "Load event handler", for the main form, has finished processing.

     

    You could move the textbox init code to the main form "activated" event handler, and  introduce a boolean flag to ensure that the init code is ran only once.

    Tuesday, May 15, 2007 8:40 PM
  • Thank you very much for your response, it is much appreciated.  I could not understand why it was showing up behind the main form.  How do I introduce a boolean flag?  I'm not sure what you mean?  I will try changing it to the "activated" event handler.

    Thanks Again,
    Tuesday, May 15, 2007 10:52 PM
  • What ArranNyc is getting at is this:

    • Create a form-level variable - e.g. bool _isInitialized;
    • Within your "Activated" event handler, check to see if _isInitialized is false (e.g. if (!_isInitialized) {...) and update your textbox or whatever else you need to do.
    • At the end of your "Activated" event handler, set _isInitialized to true (e.g. _isInitialized = true;).  That way, the next time the "Activated" event handler is called it won't run through your initialization code again.

    Hope this helps.

    Wednesday, May 16, 2007 5:20 PM
  • Thank you very much for your help and suggestions.  I greatly appreciate it.

    Thanks Again,
    Thursday, May 17, 2007 6:01 PM