Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#
Article

Synchronizing the Active State of multiple forms

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
14 Mar 20022 min read 58.3K   849   23   1
Assembly with classes for synchronizing the active state of multiple forms

Sample image 1 Sample image 2

Introduction

This article is an adapted port to C# of my other article. The code of this article implements some forms that synchronize the active state with the owner form.

Features implemented:

  • Forms that can synchronize the active state with a owner form;
  • Forms that automatically hide when the application is deactivated and when the owner form is minimized or hidden;
  • Forms that are automatically disabled when owner form is disabled.

Usage

I will base these examples in the source-code of the demo application included in this article.

  1. Add a reference to the assembly Rgl.Windows.Forms.dll in your solution.

  2. If you are implementing a dialog box or frame window derive your class from Rgl.Windows.Forms.Form, or else, derive your class from Rgl.Windows.Forms.FloatingForm (i.e. if your form will be a toolbar or a toolwindow).

    public class Form1 : Rgl.Windows.Forms.Form
    or
    public class FloatingForm1 : Rgl.Windows.Forms.FloatingForm

Reference

For sample usage refer to the demo application and source-code.

Properties

  • ForceActive (Read-Only)
    Is true when this Form is being draw in the active state.
  • SyncActive (Read-Write)
    Is true if this Form will synchronize the active state with owner Form.
  • KeepAllwaysVisible (Read-Write)
    Is true if this Form will stay visible when application is deactivated.

Helper methods

  • SetVisible
    Call this to change the visible state of this form. Sometimes we cannot use the Visible property of the Form because when we set it to true the Form is also activated; by using this method we can specify if we want to activate the form or not.

Virtual methods (to override if needed)

  • OnForceActiveChanged
    Called when the ForceActive property is changed.
  • OnCreateFloatingForms
    Called when this Form is being created.
    If you want to create and display some floating forms at the creation time of this Form you should create them in this method and not in the OnLoad method or event handler (read more about this in the BaseForm.cs file).

Under the hood

The key for synchronizing the active state of a form lies on the WM_NCACTIVATE message. By handling or sending this message we can force the painting of a form in the active or inactive state.

The automated hiding of a form is archived by handling the WM_ACTIVATEAPP message.

The other two important messages that need to be handled are WM_ACTIVATE and WM_SHOWWINDOW (handled by overriding OnVisibleChanged); you can find information about them in the source code and in the MSDN library.

To finish, I just want to say that when implementing this assembly I have run into some "strange" problems, read all about them in the source-code.

History

  • 14 mar 2002
    • first public version.

Do you have any question or comments? Contact me!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDocking? Pin
Yaron K.18-Nov-03 3:48
Yaron K.18-Nov-03 3:48 
I was looking for a code creating floating forms, so this was very helpfull, thank you.
Is there a way to make the floating form docked? I tried setting myForm.Dock = DockStyle.Right but it did nothing.

Thank you.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.