Form1 controls are not visible from form2 if form1 is in a tab control

Hi Experts,

I have a form "Form1" which is in a tab control. It has a  textbox  "Text1" and a button "Modify". When I click "Modify"  it opens "Form2". How can I access the textbox which is in 'Form1' from 'Form2'. Form1.textbox1 does not work.

Thank u in advance.
RadhaKrishnaKiJayaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
In Form1 create a Public Property with the getter and setter accessing the TextBox.Text property. When creating the Form2 pass to it as a parameter the object, Me, in Form2 take that object and place it in a class level parameter of Type Form. Then in Form2 to access the TextBox of Form1 all that is needed is to use that Form parameter. So in Form2 lets say you stored that parameter in a variable called form1Obj, and Form1's Public Property lets say it was Form1TB, then to get or set the TextBox in Form1 from Form2 all you need to do is the following.

'' Get the value in TextBox on Form1 from Form2
Dim tb As String =  form1Obj. Form1TB
'' To store a value in that same TextBox you can do this.
form1Obj. Form1TB = "Some string Value"
0
Éric MoreauSenior .Net ConsultantCommented:
make sure the modifier property of each control you want to access from the other form is set to Public
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much for your reply.

You wrote:
"In Form1 create a Public Property with the getter and setter accessing the TextBox.Text property. When creating the Form2 pass to it as a parameter the object, Me, in Form2 take that object and place it in a class level parameter of Type Form"

Can you please write some code how to do this part?

Thank you.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Fernando SotoRetiredCommented:
Hi RadhaKrishnaKiJaya;

Something like this.

Public Class Form1

    '' Public property to gain access to the TextBox1 on this Form from outside this Form
    Public Property Form1TB() As String
        Get
            Return TextBox1.Text
        End Get
        Set(ByVal Value As String)
            TextBox1.Text = Value
        End Set
    End Property

    '' Create and show Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim f1 As Form2 = New Form2(Me)
        f1.Show()

    End Sub

End Class

Public Class Form2

    '' Reference to Form1 instance
    Dim form1Obj As Form1

    Public Sub New(ByVal formParameter As Form1)
        InitializeComponent()
        '' Get a reference to Form1 and store it in a local variable
        form1Obj = formParameter
    End Sub

    Private Sub setTextBox_Click(sender As Object, e As EventArgs) Handles setTextBox.Click
        '' Set the value in TextBox1 on Form1
        form1Obj.Form1TB = "Value From Form2"
    End Sub

    Private Sub getTextBox_Click(sender As Object, e As EventArgs) Handles getTextBox.Click
        '' TextBox1 in this statement is in Form2
        TextBox1.Text = form1Obj.Form1TB
    End Sub

End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadhaKrishnaKiJayaAuthor Commented:
It worked. Thank you very much for your help.
0
Fernando SotoRetiredCommented:
Not a problem RadhaKrishnaKiJaya, glad to help.
0
RadhaKrishnaKiJayaAuthor Commented:
Hello Sir,

Can you please help me to access the dataGridview control which is in form1 from form2.

Thank u.
0
Fernando SotoRetiredCommented:
Please open a new thread for this new question. But the answer to this is to create another Public property in Form1 to set and get the values from the DataGridView. Nothing new in Form2 seeming you already implemented the class object back to Form1, just use that same object to access the new Public Property.
0
RadhaKrishnaKiJayaAuthor Commented:
It is working now. Thank you very much for your wonderful help.
0
Fernando SotoRetiredCommented:
Not a problem, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.