473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reference parent form elemets by child

Hi All

I got a small problem with VB 2005

I have let's say Form1 .Form1 has a Text Box1 and a Button1. When I click
the Button1 in a Form1, Form2 opens which has a button2 and a text box2 as
well.
When i click the button2 in the Form2 , Form2 should close and value in the
text box2 should be copied to Form1 Textbox1.
I know that I have to pass the Text box control to Form2 constructer when I
open the Form2

But what is the standard way of doing this?

Can you reference the Form1 current instance in some way?

/Best Regards
PRash
Nov 15 '06 #1
4 2465
PRash,

In VB2005, from Form2 you can reference the textbox on Form1 like this:

Form1.Textbox1.Text = "Hello"

In VB2005 you do not need to pass a reference to Form1 in Form2's constructor.

Kerry Moorman
"Prashwee" wrote:
Hi All

I got a small problem with VB 2005

I have let's say Form1 .Form1 has a Text Box1 and a Button1. When I click
the Button1 in a Form1, Form2 opens which has a button2 and a text box2 as
well.
When i click the button2 in the Form2 , Form2 should close and value in the
text box2 should be copied to Form1 Textbox1.
I know that I have to pass the Text box control to Form2 constructer when I
open the Form2

But what is the standard way of doing this?

Can you reference the Form1 current instance in some way?

/Best Regards
PRash
Nov 15 '06 #2
Prashwee wrote:
When i click the button2 in the Form2 , Form2 should close and value in the
text box2 should be copied to Form1 Textbox1.
I know that I have to pass the Text box control to Form2 constructer when I
open the Form2

But what is the standard way of doing this?
Forms are classes so, if you want to manipulate one (like handing it a
value), you have to have a reference to it.

When you open Form2, pass it a reference to Form1.

Class Form2
Public Sub New( ByVal oCaller as Form1 )
Me.New() ' run all the stuff created by the Designer
m_oCaller = oCaller
End Sub

Protected Overrides Sub OnClosed()
m_oCaller.TextBox1.Text = Me.TextBox2.Text
End Sub

Private m_oCaller As Form1

End Class

HTH,
Phill W.
Nov 15 '06 #3
Phill W. wrote:
Prashwee wrote:
When i click the button2 in the Form2 , Form2 should close and value in the
text box2 should be copied to Form1 Textbox1.
I know that I have to pass the Text box control to Form2 constructer when I
open the Form2

But what is the standard way of doing this?

Forms are classes so, if you want to manipulate one (like handing it a
value), you have to have a reference to it.

When you open Form2, pass it a reference to Form1.

Class Form2
Public Sub New( ByVal oCaller as Form1 )
Me.New() ' run all the stuff created by the Designer
m_oCaller = oCaller
End Sub

Protected Overrides Sub OnClosed()
m_oCaller.TextBox1.Text = Me.TextBox2.Text
End Sub

Private m_oCaller As Form1

End Class
Another option is to add a property to your form2 and access that in
Form1:

Class Form2

Public Property SomeString As String
End Property

End Class

Then in Form1, you can access that property after closing form2
(assuming you opened it with ShowDialog).

Nov 15 '06 #4
Or add a public method to Form1. Call it from Form2
and pass textbox2.text to the method.
The method in form1 would take a string and stuff it into the
appropriate textbox.

Robin S.
-----------------------------
"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
Phill W. wrote:
>Prashwee wrote:
When i click the button2 in the Form2 , Form2 should close and value in
the
text box2 should be copied to Form1 Textbox1.
I know that I have to pass the Text box control to Form2 constructer
when I
open the Form2

But what is the standard way of doing this?

Forms are classes so, if you want to manipulate one (like handing it a
value), you have to have a reference to it.

When you open Form2, pass it a reference to Form1.

Class Form2
Public Sub New( ByVal oCaller as Form1 )
Me.New() ' run all the stuff created by the Designer
m_oCaller = oCaller
End Sub

Protected Overrides Sub OnClosed()
m_oCaller.TextBox1.Text = Me.TextBox2.Text
End Sub

Private m_oCaller As Form1

End Class

Another option is to add a property to your form2 and access that in
Form1:

Class Form2

Public Property SomeString As String
End Property

End Class

Then in Form1, you can access that property after closing form2
(assuming you opened it with ShowDialog).

Nov 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex | last post by:
Is there a way to reference the "parent" object that creates a child object from within the child object? Or do I have to pass a reference to the parent object to the constructor of the child...
17
by: eric.nave | last post by:
What is the correct way to find all the form elements in a particular div? I'd like to be able to loop through them and disable them. For example: <form> <div id="div1"> <input type=text...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
16
by: John | last post by:
Hi I have posted this question several times but have received no reply. I have a main form which contains a panel which in turn contains a child form, like this; MainForm->MyPanel->ChildForm....
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
3
by: Eddie | last post by:
If FormMain = MDI parent, FormSub = Child parent, I execute FormSub from the menu like this way. FormSub^ sub = gcnew FormSub; sub->MdiParent = this; sub->Show(); This can generate child...
4
by: Richard Lewis Haggard | last post by:
What is the mechanism by which a child window can notify its parent that it has been clicked on? -- Richard Lewis Haggard www.Haggard-And-Associates.com
2
by: =?Utf-8?B?U2FuZHk=?= | last post by:
Hi everyone Some help pls. 1. MDI Parent Form Have a variable Public TestVariable As String = "AAA" In the Load procedure TestVariable = "BBB" 2. MDI Child Form
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.