473,769 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass reference of form through constructor?

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.
Nov 15 '05 #1
4 3228
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
news:et******** ******@tk2msftn gp13.phx.gbl...
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.

Nov 15 '05 #2
You need to duplicate your constructor

private Form _MyOtherForm;

Public Form1() : this (null)
{
}

Public Form1(Form MyOtherForm)
{
InitialiseCompo nent();
_MyOtherForm = MyOtherForm;
}

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

new Form1();
or
new Form1(OtherForm Instance);

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

"Omar Llanos" <None> wrote in message
news:et******** ******@tk2msftn gp13.phx.gbl...
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.

Nov 15 '05 #3
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.T ext = "Text";
or
textbox1.Text = "Text";

from a button control in the subclass.

Omar

"Nicholas Paldino [.NET/C# MVP]" <ca*******@casp ershouse.com> wrote in
message news:uy******** ******@TK2MSFTN GP12.phx.gbl...
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
news:et******** ******@tk2msftn gp13.phx.gbl...
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.


Nov 15 '05 #4

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: <et************ **@tk2msftngp13 .phx.gbl>
<uy************ **@TK2MSFTNGP12 .phx.gbl>
| 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: <O0************ **@tk2msftngp13 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: 66-50-71-251.prtc.net 66.50.71.251
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1878 74
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.T ext = "Text";
| or
| textbox1.Text = "Text";
|
| from a button control in the subclass.
|
| Omar
|
| "Nicholas Paldino [.NET/C# MVP]" <ca*******@casp ershouse.com> wrote in
| message news:uy******** ******@TK2MSFTN GP12.phx.gbl...
| > 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
| > news:et******** ******@tk2msftn gp13.phx.gbl...
| > > 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.
| > >
| > >
| >
| >
|
|
|

Nov 15 '05 #5

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

Similar topics

5
9496
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
11
8815
by: Vanessa | last post by:
Hi, I would like to know whether there's any way for me to pass an object by reference to another form? Regards Vanessa
12
1801
by: Casey | last post by:
Yeah, I know this question was asked by someone elselike 2 weeks ago. But I need some additional help. I have a program I'm developing, and multiple different forms will be opened. For now though, I just have two forms. One is the main window, and the other sets some preferences like names, and email addresses. When someone opens this window, and enters values on this page, I'd like it to set the values of public variables on the main...
110
9955
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
1
2307
by: Chandru | last post by:
hi Thanks, some of reply for my query. the actual problem is, I have two forms Form-A and Form-B. In Form-A i have two controls TextBox and Button, and in Form-B i have the same one textBox and Button control. While i chicking Form-A's Button i create a object for Form-B and show Form-B as Modaless Form. I entered some text in Form-B's textBox and press the Form-B's Button to close(Hide) the form Form-B, while closing i wants to pass
2
2039
by: Chane | last post by:
hi i have doubt in how to pass values back to the called form. I have two forms, each having a textbox and a button control. First i'm run the first form and click the button, it creates object for second form and showed as modaless. And i entered some text in second form's textbox. While i clicked the seconds form's button the value in textbox of second form to be transfered to first form textbox. Thanx
9
2333
by: Frank Rizzo | last post by:
I've got a number of user controls on the web page. How can I pass some data to it? I don't see where the user control is instantiated in the page code-behind page. Thanks.
7
1708
by: forest demon | last post by:
all i want is to do is to pass a form reference to a separate class and be able to manipulate properties/components/controls of said form. this should be as simple as passing a TextBox, Container object or something similar, to do the same, but no. what am i missing, besides my mind? thanks folks...
12
11110
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.