Solved

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

Posted on 2014-10-25
10
259 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It worked. Thank you very much for your help.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem RadhaKrishnaKiJaya, glad to help.
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
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
Comment Utility
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
Comment Utility
It is working now. Thank you very much for your wonderful help.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, glad to help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

15 Experts available now in Live!

Get 1:1 Help Now