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!!!
Loading
jason bournePosted Feb 26, 2009, 11:09 AM
Jan MontanoPosted Feb 25, 2009, 10:05 PM
frmSearch formSearch = new frmSearch();
private void btnSearch_Click(object sender, EventArgs e)
{
//frmSearch formSearch = new frmSearch();
formSearch.username = username;
formSearch.TopLevel = false;
formSearch.FormBorderStyle = FormBorderStyle.None;
// modify this call so that it won't duplicate in your panel
pnlMain.Controls.Add(formSearch);
formSearch.Dock = DockStyle.Fill;
formSearch.Show();
formSearch.BringToFront();
}
Niradhip ChakrabortyPosted Feb 25, 2009, 4:07 PM
To close all Mdi child forms from code within the Mdi parent itself, you can use:
foreach (Form f in this.MdiChildren)
{
f.Close();
}
To close them from some other form, replace 'this' with a reference to the Mdi Parent form.
To open another form, just create an instance of it and call the Show() or ShowDialog() methods on that instance, depending on whether its modal or not.
jason bournePosted Feb 25, 2009, 12:21 PM
I want to be able to close a different form from the asset button. For instance when you click the asset button,
if other forms are created (except main form of course)
close those forms
open the asset form and place in in the panel (which is on the main form).
otherwise everytime you click on the asset button it is created a whole new asset form and placing it on top of the other forms in the panel. I want to destroy the other forms in the panel or else show hide them.
but since the button is private, i cannot access the other forms that are created...
does this make sense? I can provide more code if needed
thanks again for your input so far!! :-)
Mirko MeierPosted Feb 25, 2009, 10:06 AM
public void CloseForm(){
this.Close();
}
in the btnAssets_Click method you can call the public method.