Pass reference of form through constructor?

  • Thread starter Thread starter Nicholas Paldino [.NET/C# MVP]
  • Start date Start date
N

Nicholas Paldino [.NET/C# MVP]

Omar,

If you are extending Form1 by subclassing it in Form2, then you
shouldn't have to pass a reference to yourself in order to access the
textbox in Form1. Rather, since Form2 ^IS^ Form1, you can just set the
textbox in Form1 to have protected access, and then you should be able to
access the members just fine.

Hope this helps.
 
You need to duplicate your constructor

private Form _MyOtherForm;

Public Form1() : this (null)
{
}

Public Form1(Form MyOtherForm)
{
InitialiseComponent();
_MyOtherForm = MyOtherForm;
}

This will give you the ability to create the form using either:

new Form1();
or
new Form1(OtherFormInstance);

Even if you never use the first syntax you still need it so that the forms
designer can work.

If you are wondering what the "this (null)" does, it just calls the second
constructor passing in a value of null. So if someone uses "new Form1()"
then the first constuctor will be called, which will call the second
constructor.

Regards,
Michael Culley
 
Recently, I posted a question on how to invoke a textbox control in Form1
(parent form) from within Form2 (which is inherited from Form1).
Someone suggested to pass a reference of the Form1 to the Form2 through the
constructor of the Form2. He said that then I'd be able to invoke the
textbox control in Form1 (with code in Form2).
Since I'm pretty new with OOP, how would I be able to do that? I tried it
but I kept getting the error that I was trying to access a private member.

Thanks.
 
Thanks for the reply.
I had previously tried that. But instead of setting the textbox to
protected access, I set it to public access. In either case (with public or
protected), the only textbox that's being modified from the button (in the
subclass) is the one in the subclass (not in the base form). This button is
not inherited (it only exists in the subclass). If the textbox is private,
it doesn't compile.

In both cases I used:
this.textbox1.Text = "Text";
or
textbox1.Text = "Text";

from a button control in the subclass.

Omar

Nicholas Paldino said:
Omar,

If you are extending Form1 by subclassing it in Form2, then you
shouldn't have to pass a reference to yourself in order to access the
textbox in Form1. Rather, since Form2 ^IS^ Form1, you can just set the
textbox in Form1 to have protected access, and then you should be able to
access the members just fine.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- casper(1 spelled out){at)caspers(where I live, rhymes with
mouse)<dot]com

Omar Llanos said:
Recently, I posted a question on how to invoke a textbox control in Form1
(parent form) from within Form2 (which is inherited from Form1).
Someone suggested to pass a reference of the Form1 to the Form2 through the
constructor of the Form2. He said that then I'd be able to invoke the
textbox control in Form1 (with code in Form2).
Since I'm pretty new with OOP, how would I be able to do that? I tried it
but I kept getting the error that I was trying to access a private member.

Thanks.
 
Hi Omar,

Sorry for letting you waiting for so long.
It is weekend these 2 days, so I did not reply you, thanks for your
understanding.

As Nicholas post, he means you can invoke the textbox in "Form1", because
the
Form2 is inherited from form1, so it will also invoke the textbox.

For another method of passing reference of form1 to form2, Michael's post
give you
sample.

If there is still anything unclear, please feel free to let me know.

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: Pass reference of form through constructor?
| Date: Sun, 28 Sep 2003 19:00:44 -0500
| Lines: 56
| 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-251.prtc.net 66.50.71.251
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:187874
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks for the reply.
| I had previously tried that. But instead of setting the textbox to
| protected access, I set it to public access. In either case (with public
or
| protected), the only textbox that's being modified from the button (in the
| subclass) is the one in the subclass (not in the base form). This button
is
| not inherited (it only exists in the subclass). If the textbox is
private,
| it doesn't compile.
|
| In both cases I used:
| this.textbox1.Text = "Text";
| or
| textbox1.Text = "Text";
|
| from a button control in the subclass.
|
| Omar
|
| message | > Omar,
| >
| > If you are extending Form1 by subclassing it in Form2, then you
| > shouldn't have to pass a reference to yourself in order to access the
| > textbox in Form1. Rather, since Form2 ^IS^ Form1, you can just set the
| > textbox in Form1 to have protected access, and then you should be able
to
| > access the members just fine.
| >
| > Hope this helps.
| >
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - casper(1 spelled out){at)caspers(where I live, rhymes with
| > mouse)<dot]com
| >
| > "Omar Llanos" <None> wrote in message
| > | > > Recently, I posted a question on how to invoke a textbox control in
| Form1
| > > (parent form) from within Form2 (which is inherited from Form1).
| > > Someone suggested to pass a reference of the Form1 to the Form2
through
| > the
| > > constructor of the Form2. He said that then I'd be able to invoke the
| > > textbox control in Form1 (with code in Form2).
| > > Since I'm pretty new with OOP, how would I be able to do that? I tried
| it
| > > but I kept getting the error that I was trying to access a private
| member.
| > >
| > > Thanks.
| > >
| > >
| >
| >
|
|
|
 
Back
Top