user1121190
user1121190

Reputation: 23

Access to controls of a Form opened inside a tab

I'm having problem accessing my winform controls when I open it inside a tab.

I have Form1 and Form2. Form1 has a tab, and I open form2 as below:

Dim ff As New Form2

ff.TopLevel = False
ff.FormBorderStyle = FormBorderStyle.None
ff.Width = TabControl1.TabPages(tabs).Width
TabControl1.TabPages(tabs).Controls.Add(ff)

Everything is fine but I can not access Form2 controls from Form1.

I appreciate any help

Upvotes: 2

Views: 1899

Answers (1)

LarsTech
LarsTech

Reputation: 81620

You need to make the form visible when you add it to the control collection:

TabControl1.TabPages(tabs).Controls.Add(ff)
ff.Visible = True

Since you have the declaration in Form1 (presumably), you can just access the controls collection directly:

For Each c As Control in ff.Controls
  ' do something with c
Next

If "ff" is not declared at the form level, then you can assign the name property to the form and then find it through the control collection:

Dim ff As New Form2
ff.Name = "ff"

Then later:

Dim ff As Form2
If TabControl1.TabPages(tabs).Controls.ContainsKey("ff") Then
  ff = TabControl1.TabPages(tabs).Controls("ff")
End If

Upvotes: 1

Related Questions