Solved

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

Posted on 2014-10-25
10
270 Views
Last Modified: 2014-10-27
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.
0
Comment
Question by:RadhaKrishnaKiJaya
  • 5
  • 4
10 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40404434
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 40404814
make sure the modifier property of each control you want to access from the other form is set to Public
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40404931
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40404958
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
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 40405665
It worked. Thank you very much for your help.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40405708
Not a problem RadhaKrishnaKiJaya, glad to help.
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40406445
Hello Sir,

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

Thank u.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40406787
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
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40406890
It is working now. Thank you very much for your wonderful help.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40406894
Not a problem, glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now