469,329 Members | 1,407 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

Newbie question : Form and Classes interaction

Hi,
I am using Visual Studio 2005 and am using Visual C#. I notice
that the program.cs creates an instance of a form and runs it. I have
a PictureBox in the form and a Class File x.cs and in the Class file
x.cs want to access some of the picturebox parameters e.g size but I
do not have access to these parameters. I could write a function in
form.cs to return these parameters but as I don't have access to an
instance of form I cannot call the function . How do I get around this
problem. I know I could pass the PictureBox as a parameter from the
form to the Class but I would like to know how classes interact with
the Form ??
Jun 27 '08 #1
2 2624
On Apr 29, 6:18*am, pat.saund...@sungardps.com wrote:
Hi,
* * * * I am using Visual Studio 2005 and am using Visual C#. I notice
that the program.cs creates an instance of a form and runs it. I have
a PictureBox in the form and a Class File x.cs and in the Class file
x.cs want to access some of the picturebox parameters e.g size but I
do not have access to these parameters. I could write a function in
form.cs to return these parameters but as I don't have access to an
instance of form I cannot call the function . How do I get around this
problem. I know I could pass the PictureBox as a parameter from the
form to the Class but I would like to know how classes interact with
the Form ??
Hi,

I do not completely understand what you want, but you can pass a
parameter of type PictureBox to the class from the form
Jun 27 '08 #2
On Tue, 29 Apr 2008 03:18:49 -0700, <pa**********@sungardps.comwrote:
I am using Visual Studio 2005 and am using Visual C#. I notice
that the program.cs creates an instance of a form and runs it. I have
a PictureBox in the form and a Class File x.cs and in the Class file
x.cs want to access some of the picturebox parameters e.g size but I
do not have access to these parameters. I could write a function in
form.cs to return these parameters but as I don't have access to an
instance of form I cannot call the function . How do I get around this
problem. I know I could pass the PictureBox as a parameter from the
form to the Class but I would like to know how classes interact with
the Form ??
As Ignacio says, you can pass the PictureBox instance reference to your X
class. This could be done via the constructor, or a method, or even by
setting a property. Based on the first clause of your last sentence, I
would even guess that you already know this.

But there's nothing magical about that. Your Form-derived class is just
another class. It works the same way as any other class, and thus other
classes "interact with the Form" in exactly the same way they would
interact with any other class. If you have a way to pass the PictureBox
reference to your X class, then you should be able to easily pass the
Form-derived class reference as well.

In particular, the PictureBox reference is, by default, only going to be
accessible from code within your Form-derived class. So if you're passing
that reference -- for example, "pictureBox1" -- you could just as easily
pass the reference to the Form, using "this".

Not that this is specifically related to your question, but IMHO it's not
usually a good idea to pass references to control instances contained
within a Form-derived class to some other class. In very specific
situations it might be okay, but most of the time you will want your
Form-derived class to keep the controls hidden, exposing only specific
characteristics of those controls in very limited ways. For example, if
you want some other class to be able to get the size of a PictureBox,
create a new read-only property (i.e. has only a getter) on the
Form-derived class that returns the size. Then the class that needs the
size will need a reference to the Form-derived class, not the PictureBox
itself, and will retrieve the size of the PictureBox indirectly through
the Form-derived class's property.

So I think you're right to want to know about passing the Form-derived
class reference to class X rather than the PictureBox reference. However,
it's not really clear what issue you're having, since you seem to already
have all of the basic parts (i.e. you know you can pass a reference to
another class, and you apparently already have a situation where you have
access to the PictureBox reference, and thus have access to the
Form-derived class reference).

If the above doesn't clear things up, maybe you could post a simple,
minimal code example that shows what you have so far, and how you want it
to change to allow whatever it is you're doing.

Pete
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by CJM | last post: by
2 posts views Thread by thomasp | last post: by
3 posts views Thread by Vitaly Zayko | last post: by
19 posts views Thread by AMP | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.