473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Event Handler

EntryTeam
Hi, all.
I'm trying to move picturebox event handler to my own class.
The event handler looks like this:
Expand|Select|Wrap|Line Numbers
  1. private void MyPicBox_Click(object sender, System.EventArgs e)
  2. {
  3.     // my code here
  4. }
The problem is: I don't know how to call this method from my class and what arguments to pass to it.

Suppose, my class's name is X.
Than, in the MainForm (where exactly in the MainForm) should be something like this:
Expand|Select|Wrap|Line Numbers
  1. X.MyPicBox_Click(?, ?)
Aug 31 '09 #1
7 2166
GaryTexmo
1,501 Expert 1GB
You need to connect the event handler of your picture box to the method you want to handle it. To do this, create a new event handler... if you're using visual studio than just type MyPicBox.Click and it should come up with instructions on what to do, but I think the code is something like...

Expand|Select|Wrap|Line Numbers
  1. MyPicBox.Click += new EventHandler(X.MyPicBox_Click);
You could put this in the constructor of the form that defines MyPicBox, or the initialize method, or the form load method, etc...

I hope that helps!
Aug 31 '09 #2
@GaryTexmo
I didn't post all of my code, so it's a little bit more complicated:
Expand|Select|Wrap|Line Numbers
  1. // how do i pass "some_class_copy" to MyPicBox_Click ? 
  2. public void MyPicBox_Click(object sender, System.EventArgs e, 
  3.         ref SomeClass some_class_copy)
  4. {
  5.   SomeMethod(ref some_class_copy); 
  6. }
Aug 31 '09 #3
GaryTexmo
1,501 Expert 1GB
So you have an extra parameter that the event needs to send? I have no idea how to make an already defined method take an extra argument. Out of curiosity, what exactly is some_class_copy? Is this something you need to pass directly, or is there a way you can get it from within the event handler itself?
Aug 31 '09 #4
@GaryTexmo
I can get it working in another way.
What I am doing/trying to do here is to "clean up" my MainForm, so there wont be any method bodies - only method calls and global variables.
This should make a code to look more professional.
Aug 31 '09 #5
GaryTexmo
1,501 Expert 1GB
If it helps any, I'm not sure it's a good idea to handle an event from an object in one class with a handler in another class. I'm probably not expressing myself very well, but I blame Monday... my head's totally not in it :D

What I mean is, wouldn't it be better to just have the form that contains the picture box handle the event from the picture box, then make use of the class to do whatever it needs to do?

Expand|Select|Wrap|Line Numbers
  1. class Main
  2.   ...
  3.   private PictureBox myPicBox = new PictureBox();
  4.   private X myX = new X();
  5.   ...
  6.   public Main()
  7.   {
  8.     ...
  9.     myPicBox.Click += new EventHandler(myPicBox_Click);
  10.     ...
  11.   }
  12.   ...
  13.   public void myPicBox_Click(object sender, System.EventArgs e)
  14.   {
  15.     myX.DoSomething(sender, e, ...);
  16.   }
  17.   ...
  18. }
Aug 31 '09 #6
tlhintoq
3,525 Expert 2GB
Having one class subscribe to an event in another class isn't a problem so long as they aren't on different threads.

The simple way to think about this is that you want your Picturebox to raise an event and your 'someclass' to listen for the event. What the event is can be whatever you want to write. Personally I try to keep the raised event to a minimum of information and have all the details in the class that is listening. The class raising the event shouldn't really know much if anything about the class that is listening. It just yells out "This just happened", and the class listening is responsible for how it reacts.

Perhaps instead of using the built-in event of "Click" you need to create a custom event with a custom Args class. When the picturebox is clicked, it's click handler can then raise your custom event.

Expand|Select|Wrap|Line Numbers
  1. public void MyPicBox_Click(object sender, System.EventArgs e)
  2. {
  3.   CustomArgs myCustomArgs = new CustomArgs();
  4.   myCustomArgs.Name = "bob";
  5.   myCustomArgs.Age = 27;
  6.   myCustomArgs.Something = ref mySomething;
  7.   MyCustomEvent(myCustomArgs);
  8. }
Then you listen for the customevent

Expand|Select|Wrap|Line Numbers
  1. FromWithBox.MyPicBox.CustomEvent += new CustomEventHandler(CustomEvent_handler); 
Aug 31 '09 #7
2GaryTexmo
myX.DoSomething(sender, e, ...);
Good one. Interesting "way around" :))
2tlhintoq
CustomArgs myCustomArgs = new CustomArgs();
This one's also very interesting - a way to pass whole "pack" of arguments as a single object - I guess this will help me much, 'cause I'm already almost getting lost within methods having huge lists of arguments. And this is only a beginning of my project :))
Aug 31 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
6
by: vbMark | last post by:
If I have a control, for example a CheckedListBox, how do I add and event to code, for example that a box has been checked by the user? Thanks
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
3
by: Beth | last post by:
in the following: this.ExitButton.Click += new System.EventHandler(this.ExitButton_Click); if I saw an equation, such as y +=x; then y = y+x. But what is the meaning in the event handler. I...
5
by: Richard Grant | last post by:
Hi, I need to "save" in a variable the event handler sub of a control's event, then perform some process, and finally "restore" the originally saved event handler. Example in pseudo-code: 1)...
1
by: tdan | last post by:
I do not know how to get Event.stopObserving() to work in the context I am using it. I am displaying a Color Selection Table and attaching 2 events: 1. onmouseover to display the color to the user...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
2
by: =?Utf-8?B?UlJK?= | last post by:
Hello, I would like to add the Paint handler to the form. How to do it? Thanks in advance.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.