472,971 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Make a custom control access variables in mainclass /calling class

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
6 2388
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Marcin Kalicinski | last post by:
Hi, I have a set of C-like functions for file access (like fopen, fwrite, fread, fseek etc.). But I want to access the files using C++ stream, not these functions. What I probably need to do is...
6
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int...
3
by: Joe Bloggs | last post by:
Does anyone know if its possible to pass parameters or the values of Request.QueryString from a web page to a custom control class? I'm using a C# Web Application. For Example I have Web Page1...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
11
by: Pete Kane | last post by:
Hi All, does anyone know how to add TabPages of ones own classes at design time ? ideally when adding a new TabControl it would contain tab pages of my own classes, I know you can achieve this with...
0
by: Mike | last post by:
Hey everyone... I've got three problems with a custom DataGridView column I've built following the "How To: Host Controls in Windows Forms DataGridView Cells" article. The base editing control...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
6
by: recherche | last post by:
Hi! Is the following program valid, although it compiles and executes successfully? // params modifier // used to declare array parameter // value type, no constructor using System;
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.