Quote:
Originally Posted by jackpotsvr
Ah I see, well I don't want to clear the things from memory orso, since the thing I will clear is just UserComponent1 so I can get to UserComponent2 (and I want to be able to switch between those, so clearing it from a memory will just make the program running slow I guess). But isn't there anyways to make a void doing the 'Form1.Controls.Clear();' ?
|
Save the instance of the form in a global variable called myForm or something. Then, from your "void", you call myForm.Controls.Clear();
You could assign your global variable (myForm) a value in your Main procedure (or somewhere else, it's all up to you).
Something like this:
(Program.cs)
Code:
static class Program
{
private static Form1 myForm;
public static Form1 MyForm
{
get { return myForm; }
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
myForm = new Form1();
Application.Run(myForm);
}
}
Then, when you want to refer to your form from inside your own method, you just do
Code:
public void SwitchUC()
{
Program.MyForm.Controls.Clear();
}
Many will probably argue that this is not the ideal approach, but I doubt you care much about design anyways.