5
Reply

closing a windows form from another form

jason bourne

jason bourne

Feb 25 2009 8:41 AM
5.6k
Im going to explain what I am trying to do before I explain my problem:

I have an application that I am building that manages company assets. the frmMain (the main form) has buttons lined up along the left hand side of it (in a panel). The right hand side of the form has another panel (covering most of the form). This panel is what the seperate forms are loaded into when the user clicks on the category to the left. If they want to search for an asset, they click search asset on the left and the search form (frmSearch) loads in the right panel. This keeps the users from having numerous forms popping up constantly.

this is the problem:

Everytime the user clicks on a button to the left and new form is created and placed in the right hand panel. This means if the user searches for an asset with the frmSearch and then adds an asset with frmAsset, if they click on frmSearch again its creating another search form and loading it overtop of the frmSearch and frmAsset forms. this is hogging reasources and is not professional. I know of the .Close and .Dispose methods but they cannot be used because the forms I am creating are not accessible by the other forms.

here is some of the code:

        private void btnSearch_Click(object sender, EventArgs e)
        {

            frmSearch formSearch = new frmSearch();
            formSearch.username = username;
            formSearch.TopLevel = false;
            formSearch.FormBorderStyle = FormBorderStyle.None;
            pnlMain.Controls.Add(formSearch);
            formSearch.Dock = DockStyle.Fill;
            formSearch.Show();
            formSearch.BringToFront();

        }
        private void btnAssets_Click(object sender, EventArgs e)
        {
            frmAsset ass = new frmAsset();
            ass.username = username;
            ass.TopLevel = false;
            ass.FormBorderStyle = FormBorderStyle.None;
            pnlMain.Controls.Add(ass);
            ass.Dock = DockStyle.Fill;
            ass.Show();
            ass.BringToFront();
          
        }

how can I close formSearch from btnAsset? Any ideas on this would be great because I have exhausted all my resources. If you need any more code or any more explanation, please ask!! Thanks in advance!!!



Answers (5)