Question

I have some tabs are open and each tab page imprisoned form. I want to open another from in current tab page by click on button in form , and the text of current tab will change. I use the code below so that if the button is pressed, the name of the current tabpage change:

private void buttonNewForm_Click(object sender, EventArgs e)
        {
            newForm childForm = new newForm(tbc1);
            //TopLevel for form is set to false
            childForm.TopLevel = false;
            //select current TabPage
            int curr = tbc1.SelectedIndex;
            TabPage tbp = tbc1.TabPages[curr];
            tbc1.TabPages[curr].Text = "name of new form";
            tbp.Controls.Add(childForm);
            //Added form to tabpage
            childForm.WindowState = FormWindowState.Maximized;
            childForm.Show();
            Refresh();

        }

It worked well until I put the code in the main form that would prevent me flickering passage of tabs:

protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
                return cp;
            }
        } 

How can I make to work again without delete the code of preventing flickering?

Was it helpful?

Solution 2

Finally I solved the problem. Instead refresh() I had to use tbc1.refresh()

tbc1 is the name of the tabcontrol I use.

I did not have, after I replace all forms to usecontrols apart from main form, and I needed to do some change to the above code:

private void buttonNewForm_Click(object sender, EventArgs e)
        {
            newForm childForm = new newForm(tbc1);
            //select current TabPage
            int curr = tbc1.SelectedIndex;
            TabPage tbp = tbc1.TabPages[curr];
            tbc1.TabPages[curr].Text = "name of new form";
            tbp.Focus();
            tbp.Controls.Clear();
            tbp.Controls.Add(childForm);
            tbc1.Refresh();

        }

OTHER TIPS

To prevent flickering try this

SuspendLayout();
// Add control and make size changes
childForm.Show();
Refresh();
ResumeLayout();

You should consider using a UserControl rather than a form to embed in a tab page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top