473,779 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.(ArrayLis t 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
"objectsAdd ed" 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 "objectsAdd ed" 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 2439
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
"MyCustomContro l" class, such as Drag&Drop, onMouseDown...e tc...
Now, lets say I do a drag & drop with regards to "MyCustomContro l":

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

private void MyCustomControl _DragEnter(obje ct sender,
System.Windows. Forms.DragEvent Args e) {
if (e.Data.GetData Present(DataFor mats.Text)){
e.Effect=DragDr opEffects.Move;
}
//ATTENTION HERE:
//here is the point where I'd like to access the variable "someVariab le"
in the class "MainClass "
//Do something like:
//MainClass.someV ariable.add(som eObject);
// or: parentObject.so meVariable.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 "myCustomContro l" 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(obje ct sender,
System.Windows. Forms.DragEvent Args e) {
if (e.Data.GetData Present(DataFor mats.Text)){
e.Effect=DragDr opEffects.Move;
}
//ATTENTION HERE:
myVar.add(SomeO bject)

}

}

Christian H.

"Morten Wennevik" <Mo************ @hotmail.com> wrote in message
news:opr616lin6 hntkfz@localhos t...
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.someV ariable.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.(ArrayLis t 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
"objectsAdd ed" 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 "objectsAdd ed" 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******** ********@TK2MSF TNGP11.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.someV ariable.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.(ArrayLis t 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 "objectsAdd ed" 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 "objectsAdd ed" 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 ObjectDroppedHa ndler(object sender, MyInfoEventArgs e);

//create the event object. the "event" keyword is a type of access control
keyword (see the PDF)
public event ObjectDroppedHa ndler 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 ObjectDroppedIn CustControl(obj ect theCustControl,
MyInfoEventArgs mie) {
myArrayList.Add ( ((MyCustomContr ol)object).foo );
}

//and it needs to subscribe to the events, specifying which method you want
called back.
myCustControl.O nObjectDropped +=
MyCustomControl .ObjectDroppedH andler(ObjectDr oppedInCustCont rol);
---------------

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******** ********@TK2MSF TNGP11.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.someV ariable.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.(ArrayLis t 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 "objectsAdd ed" 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 "objectsAdd ed" 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
5762
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 to create custom streambuf class (am I right?). Is there any free source code or tutorial available on how to do that? I have googled for it, but I only found partial and untested solutions. Worse, the solutions were pretty complicated,...
6
3590
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 myVal = class1.method(); It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is effectively wasted.
3
10837
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 which has to parameters passed to it from another Web page Parameter # 1 dbid and Parameter # 2 reportid. I know that I can access the values of the parameters from the code behind .aspx.cs using Request.QueryString so
6
4902
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
7
2701
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 the goals -- the make statement is mostly syntactic sugar for:: class <name> <tuple>: __metaclass__ = <callable>
11
18150
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 ListView columns so it should be doable, thanks
0
2376
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 is a ComboBox which is similar to the built-in Combo column but will hopefully be more flexible. First: I am using this control in multiple columns which _should_ hold unique items. However, the items added to column A are visible in
15
6522
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 accept other controls. The control i drag drop on it becomes the child of my custom control's parent form and not the child of my custom control. Then i added this line "" before my custom control class (i dont know what this line does). Now
6
1537
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;
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.