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
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: eclipseLoad multiples windows FORM in C passing argumentsStack Overflow