473,804 Members | 2,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reaching a property

hi,

I have the following construct:

public class Form1:System.Wi ndows.Forms.For m
{
.....
----a TextBox is created by the Designer in this area: 'textBox1'
.....
}
//=============== =============== ============
public class myExternalClass
{
.....
-----How to reach the Form1 'textBox1' from this point?
}

Thanks.
Newbie
Apr 24 '07 #1
7 1354
"newbie" <ne****@somewhe re.comwrote in message
news:3k******** *************** *********@4ax.c om...
public class Form1:System.Wi ndows.Forms.For m
{
.....
----a TextBox is created by the Designer in this area: 'textBox1'
.....
}
//=============== =============== ============
public class myExternalClass
{
.....
-----How to reach the Form1 'textBox1' from this point?
}
1) In Form1, in the properties for textBox1, change the Modifier to
"Public".

2) When you create the instance of Form1 whose textBox1 you want to
reach, save the reference to the instance that you create. For instance, if
you are creating your first copy of Form1 like this:

Form1 instanceOfForm1 = new Form1();
instanceOfForm1 .Show();

then you have to keep the variable 'instanceOfForm 1' because you will use
it in the next step.

3) When you want to access the textBox1 in that instance of Form1,
access it as instanceOfForm1 .textBox1

Apr 24 '07 #2
IMO, that is a very hacky solution to the problem; it causes external
classes to reference UI, and binds you to a UI implementation.

A far better option is usually eventing, e.g. the UI subscribes to
some event on the external class that gives it enough information to
update itself. The exact details would depend on the particulars.

Marc
Apr 24 '07 #3
You should expose a GetTextBox prop/method in the Form1 class, like

public class Form1:System.Wi ndows.Forms.For m
{
TextBox GetTextBox1() ...
}

and then get access to the control anyhow using this stuff.

But better solution is to encapsulate TextBox in the Form1 and expose
the methods/props *to do* something with the control without accessing
to the control itself, like:

public class Form1:S ystem.Windows.F orms.Form
{
Get/SetTextOnTexBox 1 () ...
HideTextOnTexBo x1 () ...
}

Regards, Alex Meleta
Blog: devkids.blogspo t.com

-----Original Message-----
From: newbie [mailto:ne****@s omewhere.com]
Posted At: Tuesday, April 24, 2007 12:46 PM
Posted To: microsoft.publi c.dotnet.langua ges.csharp
Conversation: Reaching a property
Subject: Reaching a property

hi,

I have the following construct:

public class Form1:System.Wi ndows.Forms.For m
{
.....
----a TextBox is created by the Designer in this area: 'textBox1'
.....
}
//=============== =============== ============
public class myExternalClass
{
.....
-----How to reach the Form1 'textBox1' from this point?
}

Thanks.
Newbie

Apr 24 '07 #4
Alberto,
OK! Works!
Solution looks fine with me - thanks.
Marc's eventing option is too complicated for me - thanks anyway.

greetz.

-------------------------------------------------------------------
On Tue, 24 Apr 2007 12:14:22 +0200, "Alberto Poblacion"
<ea************ *************** ***@poblacion.o rgwrote:
>"newbie" <ne****@somewhe re.comwrote in message
news:3k******* *************** **********@4ax. com...
>public class Form1:System.Wi ndows.Forms.For m
{
.....
----a TextBox is created by the Designer in this area: 'textBox1'
.....
}
//=============== =============== ============
public class myExternalClass
{
.....
-----How to reach the Form1 'textBox1' from this point?
}

1) In Form1, in the properties for textBox1, change the Modifier to
"Public".

2) When you create the instance of Form1 whose textBox1 you want to
reach, save the reference to the instance that you create. For instance, if
you are creating your first copy of Form1 like this:

Form1 instanceOfForm1 = new Form1();
instanceOfForm1 .Show();

then you have to keep the variable 'instanceOfForm 1' because you will use
it in the next step.

3) When you want to access the textBox1 in that instance of Form1,
access it as instanceOfForm1 .textBox1
Apr 24 '07 #5
"Marc Gravell" <ma**********@g mail.comwrote in message
news:er******** ******@TK2MSFTN GP03.phx.gbl...
IMO, that is a very hacky solution to the problem; it causes external
classes to reference UI, and binds you to a UI implementation.

A far better option is usually eventing, e.g. the UI subscribes to some
event on the external class that gives it enough information to update
itself. The exact details would depend on the particulars.
Agreed. But that is not a helpful response for a newbie. Basically, he
doesn't know how to access *something* inside the class, and judging by the
way that the question was phrased, he couldn't even tell the difference
between the class definition and an instance of the class. So an answer of
"take the thing that you don't know how to access and encapsulate it inside
another thing that you will still not know how to access" is not going to be
very useful.

Apr 24 '07 #6
I see what you are saying, and had the OP provided some kind of usable
"here's what I have, here's what I want" snippet, I would have happily
filled in a few of those blanks; maybe I'm too much of a purist, but
I'd rather take the time to explain the right way (and why) of doing
things than simply hand out a kludge - or at the very least, make that
person aware that this although solution <xwill probably work for
now, it is not the recommended approach for most scenarios. There
wasn't enough detail in the original to do this usefully (it would
have been painting from scratch, not filling in the blanks).

But; the OP is happy, which counts for a lot.

Marc
Apr 24 '07 #7
Call like formname.contro lname

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
Apr 25 '07 #8

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

Similar topics

4
1573
by: wenjie wang | last post by:
Greetings, I'd like to know is there any situation where your program could crash without reaching the breakpoint which you set at the beginning of main().
1
1573
by: leo | last post by:
Hello guys I have a problem when reaching the end of file (EOF). Is there any way I can start at the beggining of file (BOF) when looped second time sample code <% Do While Not rstA.EOF
3
2501
by: Johnny M | last post by:
using Access 2003 Pardon the subject line, but I don't have a better word for this strange behavior (or behavior I don't understand!!!) I have a class module named DepreciationFactor. One of the properties is a follows (irrelevant code omitted):
1
3533
by: sweety | last post by:
fread hangs before reaching eof() Hi, I'm reading the datat from the file. I could read the data by fread but it hangs on last data. its juz before reaching eof(). The same file could read all the data in debug mode but could not read in executable mode. I'm using VS .net 2003 and VC++ 7.1
7
6409
by: prakashleo | last post by:
hi to all I am using vb 6.0 I want to move to the next line in the text box after reaching text length 70 characters can any one help me
2
3548
by: suneet.taparia | last post by:
Hi, On IE7 I'm using a script: function saveValues() { var validateOK=true; if (typeof validate !='undefined') validateOK = validate(document.EditForm);
3
3331
by: carl.dhalluin | last post by:
Hi, I am playing with the atexit module but I don't find a way to see the difference between a script calling sys.exit(<returncode>) and the interpreting arriving at the end of the source code file. This has a semantic difference for my applications. Is there a way to determine in an exithandler (that is registered using atexit.register)
2
2082
by: MLH | last post by:
I have an A97 form with Allow Edits, Allow Deletions and Allow Additions properties set. Scrolling through records will eventually take me to the end of the records and one more PgDn will take me to a new record. I also have a cmd button on the form to take me instantly to a new record. There are 2 controls on the form I would like disabled whenever I'm at a new record. I want them enabled at all other times. Which event property would...
1
1686
by: CCHDGeek | last post by:
I am automating a process using Access 2003 (SP2) by importing csv files as tables. The amount of data was causing the database to reach the 2gb limit. Now I am deleting the tables after I am finished using them but, the database is still reaching the 2 gb limit. If I compact and repair the database it's size is 445mb. Is there a way to "refresh" the database to realize that the 2 gbs of data are no longer there without compacting and...
0
9572
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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...
0
10070
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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
6845
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
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
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
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.