Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add a new tab and attach a control into my tabcontrol from an UserControl that attached to the tabcontrol.
I used DotNetBar Components.

I use the code below in form1 and I want to call It from UserControl

C#
public void NewTab (string TabName)
        {
            TabItem newtab1;
            usercontrol_1 a1 = new usercontrol_1();
            a1.Dock = DockStyle.Fill;
            newtab1 = this.tabControl1.CreateTab(TabName);
            newtab1.AttachedControl.Controls.Add(a1);
            this.tabControl1.SelectedTab = newtab1;
        }



this is how I call this function from UserControl:
C#
Form1 f1 = new Form1();
f1.NewTab("new tab");
f1.show();

But I don't want to open another Form1!
Posted
Updated 6-May-14 22:59pm
v2
Comments
CHill60 7-May-14 4:44am    
"nothing happened"? Does this mean that you couldn't see the Form1 instance that you created?
Have you done f1.Show()?
MohammadHG 7-May-14 4:46am    
Thanks for your your help, I don't want to open another form1.
CHill60 7-May-14 5:06am    
Ok, now I'm confused. V1 of your question said you were calling that function from other forms, now you are saying you are calling it from the UserControl - do you want the new tab on the same form as the UserControl or on Form1 (but the one that is already running, not a new one)??
MohammadHG 7-May-14 5:17am    
Sorry about that. Actually there is a form and a tabcontrol in it, there is an usercontrol that attached to the tabcontrol ant I want to create a new tab and attach an usercontrol by clicking a button from usercontrol that already exist in tabcontrol.

Try something like
C#
foreach (Form form in Application.OpenForms)
{
    if (form is Form1)
    {
       Form1 f = (Form1)form;
       f.NewTab("new tab");
       break;
    }
}
 
Share this answer
 
v3
Comments
MohammadHG 7-May-14 5:25am    
It is giving an Error in this line: form.NewTab("new tab");
Error: Windows.Form dows not contain a definition for NewTab
CHill60 7-May-14 6:18am    
Sorry - I left the cast out. I've updated my solution (but not with tested code - I can't right now, sorry)
MohammadHG 7-May-14 6:37am    
Thank you so much.
change the code as below
C#
Form1  f1 = (this.Parent as Form1);
f1.NewTab("new tab");
 
Share this answer
 
v2
Comments
CHill60 7-May-14 5:15am    
OP doesn't want another instance of Form1!
DamithSL 7-May-14 5:20am    
is there any new form created in my code? i'm taking parent of the control and then add new tab.
CHill60 7-May-14 6:16am    
Fair enough - I misread that (apologies). But to be fair, how do we know that Form1 is the parent form?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900