Filling textbox in parent form from child form

O

Omar Llanos

I have Form1 and Form2 (which is inherited from Form1), and I created a
button in Form2 that will fill up a textbox in Form1. What code would do
that?

I tried the simplest way:

//from child form in button2 clicked event:

textbox1.Text = "Hello";

but it gave a protection level error since the parent textbox is private. I
then changed it to public but it only changes the textbox contents in the
second form. I don't mind if it changes both textboxes , but I need to be
able to modify the contents of the textbox in the parent form.

Anyone know how to do this?

Thanks in advance.
 
J

Jeffrey Tan[MSFT]

Hi Omar,

I think you will show your form2 in the form1,
So you can pass the reference of the form1 to the form2 through the
constructor of the form2.
Then, in class form2, you got the reference of the form1, you can invoke
its textbox control.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Omar Llanos" <None>
| From: "Omar Llanos" <None>
| Subject: Filling textbox in parent form from child form
| Date: Thu, 25 Sep 2003 21:56:44 -0500
| Lines: 21
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: cvx-ppp-66-50-133-170.coqui.net 66.50.133.170
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:187455
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have Form1 and Form2 (which is inherited from Form1), and I created a
| button in Form2 that will fill up a textbox in Form1. What code would do
| that?
|
| I tried the simplest way:
|
| //from child form in button2 clicked event:
|
| textbox1.Text = "Hello";
|
| but it gave a protection level error since the parent textbox is private.
I
| then changed it to public but it only changes the textbox contents in the
| second form. I don't mind if it changes both textboxes , but I need to be
| able to modify the contents of the textbox in the parent form.
|
| Anyone know how to do this?
|
| Thanks in advance.
|
|
|
|
 
O

Omar Llanos

Thanks for the info.
In my case, I open both at the same time through a mainMenu in my MDI
container.

This is the code in the mainMenu:
frm_AddNewProduct frm_myProducts = new frm_AddNewProduct(); //the parent

frm_myProducts.MdiParent = this;

frm_myProducts.Show();

Form2 form2 = new Form2(); //Form2 inherited from frm_myProducts

test.MdiParent = this;

test.Show();



How would I change this code (the reference part you mentioned) so I can
manipulate any control in frm_AddNewProduct from Form2 (inherited from
frm_AddNewProduct).

Thanks again,

Omar
 
J

Jeffrey Tan[MSFT]

Hi Omar,

The logic in your code is not very clear.
As you said, the frm_myProducts is a parent form, but what does
"frm_myProducts.MdiParent = this;" mean?
What is "test"?

For your problem, you should create a overloading constructor in class
Form2
which takes a form1 reference parameter.
Then you can do pass the reference of form1 like this:
Form2 form2 = new Form2(frm_myProducts);

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Omar Llanos" <None>
| From: "Omar Llanos" <None>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Filling textbox in parent form from child form
| Date: Sat, 27 Sep 2003 00:32:34 -0500
| Lines: 93
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 66-50-71-192.prtc.net 66.50.71.192
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:187640
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks for the info.
| In my case, I open both at the same time through a mainMenu in my MDI
| container.
|
| This is the code in the mainMenu:
| frm_AddNewProduct frm_myProducts = new frm_AddNewProduct(); //the parent
|
| frm_myProducts.MdiParent = this;
|
| frm_myProducts.Show();
|
| Form2 form2 = new Form2(); //Form2 inherited from frm_myProducts
|
| test.MdiParent = this;
|
| test.Show();
|
|
|
| How would I change this code (the reference part you mentioned) so I can
| manipulate any control in frm_AddNewProduct from Form2 (inherited from
| frm_AddNewProduct).
|
| Thanks again,
|
| Omar
|
| | >
| > Hi Omar,
| >
| > I think you will show your form2 in the form1,
| > So you can pass the reference of the form1 to the form2 through the
| > constructor of the form2.
| > Then, in class form2, you got the reference of the form1, you can invoke
| > its textbox control.
| >
| > Hope this helps,
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | Reply-To: "Omar Llanos" <None>
| > | From: "Omar Llanos" <None>
| > | Subject: Filling textbox in parent form from child form
| > | Date: Thu, 25 Sep 2003 21:56:44 -0500
| > | Lines: 21
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: cvx-ppp-66-50-133-170.coqui.net 66.50.133.170
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:187455
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I have Form1 and Form2 (which is inherited from Form1), and I created
a
| > | button in Form2 that will fill up a textbox in Form1. What code would
| do
| > | that?
| > |
| > | I tried the simplest way:
| > |
| > | //from child form in button2 clicked event:
| > |
| > | textbox1.Text = "Hello";
| > |
| > | but it gave a protection level error since the parent textbox is
| private.
| > I
| > | then changed it to public but it only changes the textbox contents in
| the
| > | second form. I don't mind if it changes both textboxes , but I need to
| be
| > | able to modify the contents of the textbox in the parent form.
| > |
| > | Anyone know how to do this?
| > |
| > | Thanks in advance.
| > |
| > |
| > |
| > |
| >
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top