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