?
Solved

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

Posted on 2014-10-25
10
Medium Priority
?
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 63

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 70

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 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
 
LVL 63

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 63

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 63

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

777 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