Tuesday, August 24, 2010

Form Control in Visual C#

I am currently writing a program in C#, and am almost complete except for one minute detail, I can't completely control multiple forms. I have three forms setup, two visible and one ';invisible';. The two visible forms are for functions involving creating a contact for a business. Now the two forms need to be able to be used interchangeably; however, they cannot be open at the same time. This is able to be controlled, for the most part, by the ';invisible'; form. The third ';invisible'; form is a notifyIcon in the system tray with a menu with three menuItems: open small form, open large form, exit. These three will do the following:





Open Small Form: Opens the small form, closes the large form if open





Open Large Form: Opens the large form, closes the small form if open





Exit: Application.Exit





Now here is the problem. On the small form, there is a details button which SHOULD open the large form. This does not give me the option to explicitly control this form from the ';invisible'; form, but only from the small form.








Is it possible to create a control on the third form that opens the large form when the button on the small form is clicked? I cannot post source code because it is the intellectual property of the company... the bought my thoughts...





But it will get posted in its entirity, with change to intellectual thoughts and behaviors, for free use, without any legal obligations to me :), or in other words, freeware! So completing this means the world doens't have to pay microsoft for their contact manager anymore and they can have a robust program that does it all for them... I know... overusedForm Control in Visual C#
Is this set up as an MDI application?





Regardless, if you want to interact with one form from another, you need to have a reference to the form. This is typically done by setting a form's Owner property. From there, you can cast this property to an instance of a form type.





When you open the small form, check it's owner property (which should have been set by the large form when it instanced and opened it). You can use that property to hide or close the large form:





private smallForm_FormLoad(object sender, EventArgs e)


{


(smallForm.Owner as largeForm).Close();


}





You'd have similar code in the large form's form load event handler.

No comments:

Post a Comment