By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,916 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,916 IT Pros & Developers. It's quick & easy.

Filling textbox in parent form from child form

P: n/a
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.

Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

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: <uq**************@TK2MSFTNGP12.phx.gbl>
| 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!TK2MSFTN GP12.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.
|
|
|
|

Nov 15 '05 #2

P: n/a
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

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:Sr**************@cpmsftngxa06.phx.gbl...

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: <uq**************@TK2MSFTNGP12.phx.gbl>
| 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!TK2MSFTN GP12.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.
|
|
|
|

Nov 15 '05 #3

P: n/a

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: <uq**************@TK2MSFTNGP12.phx.gbl>
<Sr**************@cpmsftngxa06.phx.gbl>
| 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: <ue*************@tk2msftngp13.phx.gbl>
| 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!tk2msftn gp13.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
|
| "Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
| news:Sr**************@cpmsftngxa06.phx.gbl...
| >
| > 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: <uq**************@TK2MSFTNGP12.phx.gbl>
| > | 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!TK2MSFTN GP12.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.
| > |
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.