473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reaching a property

hi,

I have the following construct:

public class Form1:System.Windows.Forms.Form
{
.....
----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 1332
"newbie" <ne****@somewhere.comwrote in message
news:3k********************************@4ax.com...
public class Form1:System.Windows.Forms.Form
{
.....
----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 'instanceOfForm1' 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.Windows.Forms.Form
{
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.Forms.Form
{
Get/SetTextOnTexBox1 () ...
HideTextOnTexBox1 () ...
}

Regards, Alex Meleta
Blog: devkids.blogspot.com

-----Original Message-----
From: newbie [mailto:ne****@somewhere.com]
Posted At: Tuesday, April 24, 2007 12:46 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Reaching a property
Subject: Reaching a property

hi,

I have the following construct:

public class Form1:System.Windows.Forms.Form
{
.....
----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.orgwro te:
>"newbie" <ne****@somewhere.comwrote in message
news:3k********************************@4ax.com.. .
>public class Form1:System.Windows.Forms.Form
{
.....
----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 'instanceOfForm1' 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**********@gmail.comwrote in message
news:er**************@TK2MSFTNGP03.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.controlname

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
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
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
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...
1
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...
7
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
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
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...
2
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...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.