Reaching a property

  • Thread starter Thread starter newbie
  • Start date Start date
N

newbie

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
 
newbie said:
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
 
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
 
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:[email protected]]
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
 
Alberto,
OK! Works!
Solution looks fine with me - thanks.
Marc's eventing option is too complicated for me - thanks anyway.

greetz.

-------------------------------------------------------------------
 
Marc Gravell said:
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.
 
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 <x> will 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top