Adding a tab to a TabControl1 from another Form

T

Tom McL.

My Program has two forms From1 and Form2.
On From1 I have a TabControl1 witch has
8 tabs currently. I would like to add a new
tab from Form2 is this possible?
I have tried the following code on Form2
with no success:

Public MyFrom1 As New From1

Private Sub btnNewTab_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)Handles btnNewTab.Click

MyFrom1.TabPage1 = New TabPage

' Adds the tabPage1 to this collection.
MyFrom1.TabControl1.TabPages.Add(MyFrom1.TabPage1)

' Name the Tab
MyFrom1.TabPage1.Text = "myTabPage"
MyFrom1.TabControl1.Refresh()

End Sub


Thanks

Tom
 
G

Guest

Is this code on Form2 and you already have Form1 open? If I'm right, You just
created another Form1 in the memory in addition to the Form1 that already
open. If you open Form1 before Form2, pass Form1 to Form2 in the constructor
as a parameter, save it in the class level, and do all the adding tab work on
this variable.
You can do it in one line:
tabControl1.TabPages.Add(New TabPage("myTabPage"))
Where tabControl1 is the class level variable that points to Form1 instance.
 
T

Tom McL.

Amiram,

I called From2 from Form1 as a ShowDialog()

Now I am at a loss, I don't understand Constructors is the constructor
created
in Form2?

A short example would help a lot if you have time. I have been working with
VB 5 and tring to understand VB.net.

Thanks again

Tom
 
G

Guest

You can find a sub named "New" in the hidden section of your form. This is a
constructor.

Add a variable to hold Form1 reference:
private ParentForm AS Form1

Add your own constructor (leave the hidden one)
Public Sub New(ByVal ParentForm as Form1)
Me.ParentForm = ParentForm
End Sub

Whenever you need to add pages to the tabcontrol write:
ParentForm.tabcontrol1.Pages.Add(New TabPage("name"))

I suggest you change the names Form1 and Form2 to meaningful names. If you
came from vb5, take some time to learn vb.net, especially classes and
inheritance before you start.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top