Here is my code :
public Form1(ScriptContext context)
{
InitializeComponent();
}
public void loadform(object Form)
{
if (this.panel2.Controls.Count > 0)
this.panel2.Controls.RemoveAt(0);
Form f = Form as Form;
f.TopLevel = false;
f.Dock = DockStyle.Fill;
this.panel2.Controls.Add(f);
this.panel2.Tag = f;
this.Visible = true;
f.Show();
}
private void button1_Click(object sender, EventArgs e)
{
loadform(new Form2());
}
It doesn't work : in loadform i can not pass ScriptContext context. I can only do : loadform(new Form2)
How to manage it in the function ? Please
i tried to change the loadform to laodform(object Form(ScriptContext context)) but it does not work...
Here is my code :
public Form1(ScriptContext context)
{
InitializeComponent();
}
public void loadform(object Form)
{
if (this.panel2.Controls.Count > 0)
this.panel2.Controls.RemoveAt(0);
Form f = Form as Form;
f.TopLevel = false;
f.Dock = DockStyle.Fill;
this.panel2.Controls.Add(f);
this.panel2.Tag = f;
this.Visible = true;
f.Show();
}
private void button1_Click(object sender, EventArgs e)
{
loadform(new Form2());
}
It doesn't work : in loadform i can not pass ScriptContext context. I can only do : loadform(new Form2)
How to manage it in the function ? Please
i tried to change the loadform to laodform(object Form(ScriptContext context)) but it does not work...
If you want to pass the context into the constructor of Form2 you need to do two things. Save the context to a local variable and change the constructor of Form2.
public class Form1 : Form
{
private ScriptContext _scriptContext;
public Form1(ScriptContext context)
{
InitializeComponent();
_scriptContext = context;
}
public void loadform(object Form)
{
// not important
}
private void button1_Click(object sender, EventArgs e)
{
loadform(new Form2(_scriptContext));
}
}
public class Form2 : Form
{
public Form2(ScriptContext context)
{
InitializeComponent();
// do something with context
}
}
Additionally, your loadform
method should not have object
as the parameter type, it can't work with anything other than Form
so type the parameter as Form
or Form2
.