user2330632
user2330632

Reputation: 15

Possible to move a Form from one Container to another? How?

Setup information:

I have a form

Insurance insuranceForm = new Insurance();

and have added this form to tabControl_insurance

tabControl_insurance.SelectedTab.Controls.Add(insuranceForm);

There can be between 0 and 8 tabs within tabControl_insurance at any time (controlled in runtime).

What I want to do:

I want to move insuranceForm to whatever tab is currently selected (if any), rather than having 0 to 8 copies [slight variations] of insuranceForm. Is this possible?

As far as I know, a forms Container is set when the form is first created via Controls.Add(). The Container() of a form cannot be re-set.

Thankyou

Upvotes: 0

Views: 524

Answers (2)

M Antonio
M Antonio

Reputation: 144

Yes its possible, just instantiate your class 'Insurance' publicly and and change the TopLevel property to 'false'. and add that object where ever you want. by 'this.tabControl.TabPages[x].Controls.Add('your_form'); the form will be moved where ever tab you want with no changes on its state.

sample code:

Form2 f = new Form2();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        f = new Form2()
        {
            Top = 0,
            Left = 0,
            Width = 100,
            Height = 100,
            TopLevel = false
        };
    }

    private void button1_Click(object sender, EventArgs e)
    {

        int x = int.Parse(this.textBox1.Text);             
        this.tabControl1.TabPages[x].Controls.Add(f);
        f.Show();

        this.tabControl1.Refresh();
    }

hope it helps.

Upvotes: 0

Diego Balduino
Diego Balduino

Reputation: 58

I think what do you want is possible, follow a example using a component and how you can add/remove to navigate between tabs.

//Method Add Component (can be any)
Button button = new Button() { Location = new Point(12, 12) };
tabControl1.SelectedTab.Controls.Add(button);

//Method Remove Component (Can be any too)
var controls = tabControl1.SelectedTab.Controls.Cast<Control>().Where(x => x.GetType() == typeof(Button)).ToList();

foreach (var item in controls)
{
    tabControl1.SelectedTab.Controls.Remove(item);
}

This way you can add or remove components from tab at runtime.

I hope it helps

Upvotes: 1

Related Questions