Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Show or hiding forms/modifying control properties ..from different a form.. (http://www.programmingforums.org/showthread.php?t=11645)

cloud- Oct 20th, 2006 12:03 PM

Show or hiding forms/modifying control properties ..from different a form..
 
Hi.
I'm having problems with accessing forms and their properties/functions from different forms, I have a menu (ToolStrip) which has a drop down list for the forms shown, so checking/unchecking the form will hide or show the selected form.
Firstly when hiding/showing my forms they don't keep the data that was inputted because I'm calling new forms to be created (I don't know another way to call a form as an 'MdiChild'. Here's the code I'm using to open the form as a child (from the menu of the parent container).
:

            if (scriptEditorToolStripMenuItem.Checked == true)
            {
                Form Script = new TFX_Script();
                Script.MdiParent = this;
                Script.Show();
            }
            if (scriptEditorToolStripMenuItem.Checked == false)
            {
                foreach (Form Script in this.MdiChildren)
                {
                    Script.Hide();
                }

            }

TFX_Script is the child form.

This worked well except for two problems. The data inputted into the form (it's controls.. textboxes, checkboxes etc.) will be gone when the new form is created, I don't want that >_<!

The second problem is that obviously the child form can be fully closed within itself via the 'X' button. I don't know how to access the parents menu to change the checkbox (visible or not visible), Also all data will be lost again, I'd rather cancel the close and hide it, while changing the state of the checkbox on the parents menu.

Atm, I'm using a not-so-good work around, when the menu is clicked it checks if the form is opened or not, then changes the checkbox in the submenu (for the form) accordingly.
:

            foreach (Form Script in this.MdiChildren)
            {
                if (Script.Visible == false)
                {
                    scriptEditorToolStripMenuItem.Checked = false;
                }
                else
                {
                    scriptEditorToolStripMenuItem.Checked = true;
                }
            }

This doesn't look too good since you can see the check (in the menu) flash and it doesn't fix my other problem with loosing the data also.

If you can help me sort this out it will be greatly appreciated! :D
Thanks~

-cloud- <_<

cloud- Oct 24th, 2006 7:30 PM

Sorted~

If someone ever needs to know..

Calling parents functions from child..

:

ParentForm SomeName = (ParentForm)MdiParent;
SomeName.Function();


..and for hiding/showing the forms, loop through them to check if the form already exists.. if not create a new one..

Sorry for answering my own question <_<;

/Kill thread

melbolt Nov 1st, 2006 1:08 AM

there's an easier way, don't declare your new form in the handler, instead declare it at the top of your class and then just .show or .hide accordingly in the handlers. that way you only have one form throughout the life of the parent instead of creating a brand new one each time the event is raised.

Iftikhar Nov 1st, 2006 2:33 PM

Because you are creating new form by using new keyword so all of your previous data is lost.
You can have private objects of the forms you want to show hide. Before displaying them check the specific object is null or not. If it is null then create a new form with new keyword else just show the form or hide it. In this way you will not loose data. for example
:

public class MainForm
{
private TFX_Script o_TFX_Script;

click_function()
{
  if (o_TFX_Script == null)
  {
    o_TFX_Script = new TFX_Script();
    o_TFX_Script.Show();
  }
  else
  {
    o_TFX_Script.Show();
  }
}
}


cloud- Nov 10th, 2006 11:51 AM

That's so much better! Thanks!~ :D
I ran in to trouble with my method, changing the child's properties didn't work as planned, this has fixed all my troubles. ^^


All times are GMT -5. The time now is 1:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC