admin管理员组文章数量:1434909
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...
Share Improve this question asked Nov 17, 2024 at 16:22 Romain MaillotRomain Maillot 12 bronze badges 4- It's not entirely clear what you're trying to do, but if you want to pass the context passed to the constructor of Form1 into the constructor of Form2 you're going to have to save it to a class variable (field). At the moment you're ignoring it. – iakobski Commented Nov 17, 2024 at 18:21
- Thanks you ! I do differents operations in Form 1 and Form 2 but I need the ScriptContext context to do what I want in the two Forms... – Romain Maillot Commented Nov 17, 2024 at 18:45
- What does the constructor of Form2 look like? – iakobski Commented Nov 17, 2024 at 18:48
- public partial class Form2 : Form { public Form2() { InitializeComponent(); } – Romain Maillot Commented Nov 18, 2024 at 9:11
1 Answer
Reset to default 0If 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
.
本文标签: eclipseLoad multiples windows FORM in C passing argumentsStack Overflow
版权声明:本文标题:eclipse - Load multiples windows FORM in C# passing arguments - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745632030a2667333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论