Solved

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

Posted on 2014-10-25
10
288 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 63

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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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