0

I have a form page with text boxes and data grid view and other forms that contain a tab control. I want to add the first form tab in the second form. I tried to write the code for the form to appear but it is larger than the tab container and doesn't fit. Only half of the form appears.

This is my code:

private void tcMainPage_SelectedIndexChanged(object sender, EventArgs e)
{
      if (tcMainPage.SelectedIndex == 0)
      {
          GTOWN.PrintingPage BookInfo = new PrintingPage();
          BookInfo.TopLevel = false;
          BookInfo.FormBorderStyle = FormBorderStyle.None;
          BookInfo.Dock = DockStyle.Fill;
          tpSearch.Controls.Add(BookInfo);
          BookInfo.Show();
       }    
}

this is the form

and that is what appears

Bill Tarbell
  • 4,933
  • 2
  • 32
  • 52
waleed jab
  • 31
  • 1
  • 1
  • 2

4 Answers4

0

Set your main FORM as a Container.

yourForm.IsMdiContainer = true;

Then add the child form to the tabPage:

private void tcMainPage_SelectedIndexChanged(object sender, EventArgs e)
{
    if (tcMainPage.SelectedIndex == 0)
    {
        PrintingPage newFrm = new PrintingPage
        {
            MdiParent = this,
            // This set the form parent as the tabClicked
            Parent = tcMainPage.TabPages[0]
        };
        newFrm.Show();
    }
}
0

my tab form work good in the same code

thank you all my code was correct but the problem was in tab property i deleted the tab and add another one and the code is working now

thank you

waleed jab
  • 31
  • 1
  • 1
  • 2
0

I face this issue and I create this if may help

public void addform(TabPage tp, Form f)
    {                      
        
        f.TopLevel = false;
        //no border if needed
        f.FormBorderStyle = FormBorderStyle.None;            
        f.AutoScaleMode = AutoScaleMode.Dpi;
        
        if (!tp.Controls.Contains(f))
        {
            tp.Controls.Add(f);
            f.Dock = DockStyle.Fill;
            f.Show();
            Refresh();
        }
        Refresh();            
    }
-2

Forms are top-most objects and cannot be placed inside of other containers.

You may want to refactor your code so that the items on your Form are on a UserControl instead. At that point you can then add that UserControl to both a Form and a TabControl

public UserControl myControl(){ /* copy your current view code here */}

public Form myForm(){
  Controls.Add(new myControl());
}

public Form myTabbedForm(){
  var tabControl = new TabControl();
  var page1 = new TabPage();
  page1.Controls.Add(new myControl());
  tabControl.TabPages.Add(page1);
  this.Controls.Add(tabControl);
}
Bill Tarbell
  • 4,933
  • 2
  • 32
  • 52
  • `BookInfo.TopLevel = false;` turns the form into a type of UserControl. – LarsTech Jun 21 '17 at 19:04
  • @LarsTech No, it doesn't. If you set a Form's topLevel to false it does not magically change the object's type. It is still a Form and still has all of the inappropriate methods of a form that should not be called or even be available if you have it as a child of a standard container. – Bill Tarbell Jun 21 '17 at 19:31
  • Maybe I should have used the words "acts like a UserControl". Your first sentence is just blatantly wrong though: `Forms are top-most objects and cannot be placed inside of other containers.` Yes, they can be placed inside containers if the TopLevel property is set to false. – LarsTech Jun 21 '17 at 19:40