By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 2,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Make a custom control access variables in mainclass /calling class

P: n/a
Hi!

I've created a custom control (myDrawControl) that deals with drawing.
This control is then added to a form( myMainForm)

Now, whenever something is dragged and dropped onto myDrawControl , I want
to update a variable in myMainForm that keeps track of the objects that have
been added.(ArrayList objectsAdded)

Now I'm a little confused... How can I do this, when the method that deals
with drag&Drop for myDrawControl is not in the same class as the variable
"objectsAdded" I'm trying to access..? The custom control knows nothing
about the class that is using it, so how can this be accessed?
Do I have to add the "objectsAdded" variable as a reference to my control in
order to do this? So that I'll have a method setObjRef(ref myobject) in this
class? What are my options?
I have never really used custom controls before, so this was new to me. I've
been used to having all of the methods that performs an event on the control
, in the same class.
Regards Christian H.,


Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Christian,

Um, I may have misunderstood, but why do you need to know what your
myDrawControl contains? The myDrawControl should be able to handle
everything on it's own without relying on the owner.

If you can tell us in more detail what you are trying to accomplish we
might be able to come up with another way of doing it.
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

P: n/a
Ok here is a sample that will illustrate:

public class MainClass : System.Windows.Forms.Form
{
private MyCustomControl myControl;
private Arraylist someVariable.

//add different stuff to the form, including myControl

}
Now, all the events related to "myControl", is ofcourse inside the
"MyCustomControl" class, such as Drag&Drop, onMouseDown...etc...
Now, lets say I do a drag & drop with regards to "MyCustomControl":

public class MyCustomControl : System.Windows.Forms.Control {

private void MyCustomControl _DragEnter(object sender,
System.Windows.Forms.DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Text)){
e.Effect=DragDropEffects.Move;
}
//ATTENTION HERE:
//here is the point where I'd like to access the variable "someVariable"
in the class "MainClass "
//Do something like:
//MainClass.someVariable.add(someObject);
// or: parentObject.someVariable.add(someObject);
}

}

If you look at the original post , and this one, does it clear up what I
want to acceive?
You could say that I'm trying to access a variable that is in the "parent"
object, or that I'm trying to access a variable that is in the class that
instantiated myCustomControl.
My idea was that the only way to do such a thing, would be to create
property or method in "myCustomControl" that receives a reference to the
object in the parent class I want to access.
Example:

public class MyCustomControl : System.Windows.Forms.Control {

private ref someVariable myVar;
public void setSomeVariable(ref someVariable s){
this.myVar=s;
}

private void MyCustomControl _DragEnter(object sender,
System.Windows.Forms.DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Text)){
e.Effect=DragDropEffects.Move;
}
//ATTENTION HERE:
myVar.add(SomeObject)

}

}

Christian H.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opr616lin6hntkfz@localhost...
Hi Christian,

Um, I may have misunderstood, but why do you need to know what your
myDrawControl contains? The myDrawControl should be able to handle
everything on it's own without relying on the owner.

If you can tell us in more detail what you are trying to accomplish we
might be able to come up with another way of doing it.
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3

P: n/a
KC
Hi,

I can see a couple of solutions. One is to do as you suggest.

Depending on your needs, you could declare the array in MainClass public
static and access it from the custom control
with MainClass.someVariable.Add(...). This assumes that MyCustomControl
knows that it is always MainClass that is using it though.

Another way is to pass a reference to MainClass to the custom control:
myControl = new MyCustomControl (this);

Another, and probably the best IMO, is to use a delegate, or event, that
MainClass subscribes to. Either write you own custom delegate/event that
MainClass subscribes to. When your done with handling the drag and drop in
your custom control, fire off an event with the appropriate information, and
the MainClass will be notified.

Hope that helps!

/Kris

"Christian H" <n@n.no> wrote in message
news:F_***************@news4.e.nsc.no...
Hi!

I've created a custom control (myDrawControl) that deals with drawing.
This control is then added to a form( myMainForm)

Now, whenever something is dragged and dropped onto myDrawControl , I want
to update a variable in myMainForm that keeps track of the objects that have been added.(ArrayList objectsAdded)

Now I'm a little confused... How can I do this, when the method that deals
with drag&Drop for myDrawControl is not in the same class as the variable
"objectsAdded" I'm trying to access..? The custom control knows nothing
about the class that is using it, so how can this be accessed?
Do I have to add the "objectsAdded" variable as a reference to my control in order to do this? So that I'll have a method setObjRef(ref myobject) in this class? What are my options?
I have never really used custom controls before, so this was new to me. I've been used to having all of the methods that performs an event on the control , in the same class.
Regards Christian H.,

Nov 16 '05 #4

P: n/a

Yep, That last option seems to be the most clean one.
Do you have any examples / tutorials I can look at?
C.H

"KC" <no@mail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

I can see a couple of solutions. One is to do as you suggest.

Depending on your needs, you could declare the array in MainClass public
static and access it from the custom control
with MainClass.someVariable.Add(...). This assumes that MyCustomControl
knows that it is always MainClass that is using it though.

Another way is to pass a reference to MainClass to the custom control:
myControl = new MyCustomControl (this);

Another, and probably the best IMO, is to use a delegate, or event, that
MainClass subscribes to. Either write you own custom delegate/event that
MainClass subscribes to. When your done with handling the drag and drop in
your custom control, fire off an event with the appropriate information, and the MainClass will be notified.

Hope that helps!

/Kris

"Christian H" <n@n.no> wrote in message
news:F_***************@news4.e.nsc.no...
Hi!

I've created a custom control (myDrawControl) that deals with drawing.
This control is then added to a form( myMainForm)

Now, whenever something is dragged and dropped onto myDrawControl , I want to update a variable in myMainForm that keeps track of the objects that have
been added.(ArrayList objectsAdded)

Now I'm a little confused... How can I do this, when the method that deals with drag&Drop for myDrawControl is not in the same class as the variable "objectsAdded" I'm trying to access..? The custom control knows nothing
about the class that is using it, so how can this be accessed?
Do I have to add the "objectsAdded" variable as a reference to my

control in
order to do this? So that I'll have a method setObjRef(ref myobject) in

this
class? What are my options?
I have never really used custom controls before, so this was new to me.

I've
been used to having all of the methods that performs an event on the

control
, in the same class.
Regards Christian H.,


Nov 16 '05 #5

P: n/a
KC
Sure! Have a look at this PDF, which is an exerpt from a book:
http://www.oreilly.com/catalog/progc...apter/ch12.pdf

Here's a sample:

In MyCustomControl, add the following:
---------------
//define what the subscribers must implement when subscrining
public delegate void ObjectDroppedHandler(object sender, MyInfoEventArgs e);

//create the event object. the "event" keyword is a type of access control
keyword (see the PDF)
public event ObjectDroppedHandler OnObjectDropped;
---------------

You also create a MyInfoEventArgs type. This type contains information about
what happened. You can create your own, or use one of the predefined ones.
Here's a sample of a custom one (extends EventArgs).
---------------
public class MyInfoEventArgs : EventArgs {
public readonly int someStatus;
public readonly string someMessage;

public MyInfoEventArgs (int status, string message) {
someStatus = status;
someMesage = message;
}
}
---------------

In MyCustomControl, after you do your thing, fire off the event:
---------------
//do your thing
//create the event args
MyInfoEventArgs eventArgs = new MyInfoEventArgs(10, "Object X dropped");
//notify. set sender to this.
OnObjectDropped(this, eventArgs)
---------------

The last thing you need to do is to have MainClass subscribe to and handle
the events:
---------------
//Define a method that will handle the event. Must match MyCustomControl's
declaration
public void ObjectDroppedInCustControl(object theCustControl,
MyInfoEventArgs mie) {
myArrayList.Add( ((MyCustomControl)object).foo );
}

//and it needs to subscribe to the events, specifying which method you want
called back.
myCustControl.OnObjectDropped +=
MyCustomControl.ObjectDroppedHandler(ObjectDropped InCustControl);
---------------

That should do it!

/Kris

"Christian H" <n@n.no> wrote in message
news:RR***************@news4.e.nsc.no...

Yep, That last option seems to be the most clean one.
Do you have any examples / tutorials I can look at?
C.H

"KC" <no@mail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

I can see a couple of solutions. One is to do as you suggest.

Depending on your needs, you could declare the array in MainClass public
static and access it from the custom control
with MainClass.someVariable.Add(...). This assumes that MyCustomControl
knows that it is always MainClass that is using it though.

Another way is to pass a reference to MainClass to the custom control:
myControl = new MyCustomControl (this);

Another, and probably the best IMO, is to use a delegate, or event, that
MainClass subscribes to. Either write you own custom delegate/event that
MainClass subscribes to. When your done with handling the drag and drop in
your custom control, fire off an event with the appropriate information,

and
the MainClass will be notified.

Hope that helps!

/Kris

"Christian H" <n@n.no> wrote in message
news:F_***************@news4.e.nsc.no...
Hi!

I've created a custom control (myDrawControl) that deals with drawing.
This control is then added to a form( myMainForm)

Now, whenever something is dragged and dropped onto myDrawControl , I want to update a variable in myMainForm that keeps track of the objects that have
been added.(ArrayList objectsAdded)

Now I'm a little confused... How can I do this, when the method that deals with drag&Drop for myDrawControl is not in the same class as the variable "objectsAdded" I'm trying to access..? The custom control knows
nothing about the class that is using it, so how can this be accessed?
Do I have to add the "objectsAdded" variable as a reference to my

control
in
order to do this? So that I'll have a method setObjRef(ref myobject)

in this
class? What are my options?
I have never really used custom controls before, so this was new to
me. I've
been used to having all of the methods that performs an event on the

control
, in the same class.
Regards Christian H.,



Nov 16 '05 #6

P: n/a
Thank you for the explanation.

However, it doesn't explain what I wanted to know. I know you are trying
to make a list of objects that are added to your custom control in the
parent, or control owner. But why do you need to do so? When things
happen in the parent, it is the parent's job to notify it's children. It
might be that you have very good reasons for doing so, but if the parent
needs to do something with the controls I think it should instead tell the
child to do something and let it do the stuff itself.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.