tabcontrol other form

johmolan

Well-known member
Joined
Oct 11, 2008
Messages
129
Programming Experience
Beginner
When I add the command:

Me.TabControl1.TabPages.Remove(Me.TabPage2)

To a button click in my main form it works like a charm,
But when I put it in another form and add the command to a buttonclick like this:

Form1.TabControl1.TabPages.Remove(Form1.TabPage2)
It just does not work. I get no debug errors but it stil doesn't work

Anybody know why?
 
What's 'Form1' defined as in the "other" form? You need an instance of the first form in the 2nd form for it to work
 
How do I do that?

This is the code I use in form2

Imports WindowsApplication1.Form1
Imports Microsoft.VisualBasic
Imports System
Public Class Velg_Prosess


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Form1.TabControl1.TabPages.Remove(Form1.TabPage2)

End Sub
End Class

Form1 is my main form
 
If form 1 owns form 2 then you can use the Owner property on form 2:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  DirectCast(Me.Owner, Form1).TabControl1.TabPages.Remove(Form1.TabPage2)
End Sub
Otherwise you'll need to have a global variable with a scope of friend in a module and in form1's load event you set that var equal to Me (the keyword Me) then in form2 you simply use the variable name in place of the whole DirectCast stuff.

And this line:
Imports WindowsApplication1.Form1

is never needed
 
I tried to put the code like this

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
test.TabControl1.TabPages.Remove(Form1.TabPage2)

whwere test is my "global variabel"

But I am a little confused about that global one.

I declared it in form1.class as a public variable
and set test = Me in the form1 load method,

But I do get the error that is is not declared in Form2
 
I have made a couple of methods:

VB.NET:
Public Sub Tab2()
TabControl1.TabPages.Insert(1, TabPage2)
TabControl1.Refresh()
End Sub

Public Sub Tab21()
Me.TabControl1.TabPages.Remove(TabPage2)

End Sub
Which will remove or add tab pages.

I try to call them from another form with the call:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form1.Tab2()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form1.Tab21()

End Sub
But nothing happends. can someone please tell me what am I doing wrong??
 
First off, it is as JuggaloBrotha said in post#2, you need to reference an instance of the form.
You started to get on the correct track with the global 'test' variable. Your button click event would have worked if you had continued to reference the instance.

I tried to put the code like this

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
test.TabControl1.TabPages.Remove(Form1.TabPage2)

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        test.TabControl1.TabPages.Remove([B]test.[/B]TabPage2)

The same goes for the last bits of code.

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
[B]test[/B].Tab2()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
[B]test[/B].Tab21()

End Sub

I have not actually run any of these so double check and lets see where you're at.
 
Ok , If I use "Dim form1 as new Form1"
and then use the lines under it should work?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
form1.Tab2()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
form1.Tab21()

End Sub
 
No you cannot "Dim" the variable Form1. You need to have a reference to the form that is calling the second form. You could pass a reference to the second form of the first form. :confused: lol

Here we are going to send a reference to Form1 into Form2 by using the New contructor.

First Form
VB.NET:
Public Class Form1

     Private ButtonClick(byval sender as Object, byval e as System.EventArgs) handles BtnOne.Click

          Dim frm2 as New Form2(Me)
          frm2.Show()

     End Sub
End Class

here is the second form.
VB.NET:
Public Class Form2

     Dim Form1Passed as Form1

     'This allows us to receive a reference to the first form
     Public Sub New(ByRef PassedForm as Form1)
          InitializeComponent()

          Form1Passed = PassedForm

     End Sub

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     Form1Passed.Tab2()

     End Sub

     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
     Form1Passed.Tab21()

     End Sub
End Class

This is not the exact way that JuggaloBrotha stated in post#2, but it is another way to do it.
 
Back
Top