Click here to Skip to main content
15,898,222 members
Home / Discussions / C#
   

C#

 
QuestionCan anyone recommend a book for writing custom control in C#? Pin
Li-kai Liu (Angus)28-Aug-02 16:19
Li-kai Liu (Angus)28-Aug-02 16:19 
AnswerRe: Can anyone recommend a book for writing custom control in C#? Pin
Nnamdi Onyeyiri28-Aug-02 21:50
Nnamdi Onyeyiri28-Aug-02 21:50 
GeneralRe: Can anyone recommend a book for writing custom control in C#? Pin
Li-kai Liu (Angus)28-Aug-02 22:11
Li-kai Liu (Angus)28-Aug-02 22:11 
GeneralRe: Can anyone recommend a book for writing custom control in C#? Pin
leppie29-Aug-02 0:47
leppie29-Aug-02 0:47 
AnswerRe: Can anyone recommend a book for writing custom control in C#? Pin
Alastair Stell29-Aug-02 4:07
Alastair Stell29-Aug-02 4:07 
GeneralTree/List Hybrids Pin
candan28-Aug-02 14:46
professionalcandan28-Aug-02 14:46 
GeneralRe: Tree/List Hybrids Pin
Stephane Rodriguez.29-Aug-02 1:34
Stephane Rodriguez.29-Aug-02 1:34 
QuestionHow do you unsubscribe to an event? Pin
Rodney S. Foley28-Aug-02 14:18
Rodney S. Foley28-Aug-02 14:18 
I sitting here coding along when I release I need to be able to unsubscribe to an event I just subscribed to. So I go to my books, and none of them talk about it, they show you how to subscribe to an event but not unsubscribe to it. So I do an internet search I get nothing. I do a Code Project search and I get nothing. I have a hard time believing that I am the first person who needed to know how to unsubscribe to an event in C#. In Java it was always straight forward with add and remove events tied to the object want to know about the event.

Well here is the way everyone teaches to subscribe to and event:
C#
<pre lang="cs">    OnEvent += new EventHandler(HandlerMethod);</pre>So I would assume that to unsubscribe you would do the reverse:<br />
<code lang="cs"><pre lang="cs">    OnEvent -= handleToEventHandlerObject;</pre>However since this the EventHandler was created anonymously you do not have a handle/reference to it.  <br />
<br />
So my next assumption would be this:<br />
<code lang="cs"><pre lang="cs">	EventHandler handleToEventHandler = new EventHandler(HandlerMethod);
		
	/ ... /
 		
	OnEvent += handleToEventHandler;
		
	/ ... /
	
	OnEvent -= handleToEventHandler;</pre>Does this sound right.  I have not tried it yet, since I was hoping there was a better way, and also I wanted to do some gripping about C#'s auto-magical delegate system for handling events.  Which have created problems, at least for me.  Mainly when you want to wrap another object, and provide pass through event handling, you can not.  Your wrapper class has to register for the events, and then provide its own delegates for handling it.  Also when you want to do something like subscribe and unsubscribe to an event it seams like subscribing is no big deal, but unsubscribing seams like it comes with a little extra (more like annoying) work.<br />
<br />
Just to say it, I think the Java event handling system was far better and more flexible.  There was nothing going on auto-magically under the covers.  You new there was a Collection holding all the classes who subscribed, and that they are required to have a method signature that you will call when it is thrown.  There were standardized add/remove methods that all you have to do is pass the object who subscribed which was 99% of the 'this' keyword.  You always hand a handle to the object you used to subscribe to it with.<br />
<br />
Well that’s it with my gripping about C# events.  If someone can confirm that above is the correct and only way to subscribe and unsubscribe to an event, or provide a alternative solution.<br />
<br />
:laugh: Thanks for putting up with me.  ;P<br />
<br />
Aalst

AnswerRe: How do you unsubscribe to an event? Pin
Paul Riley28-Aug-02 18:17
Paul Riley28-Aug-02 18:17 
GeneralRe: How do you unsubscribe to an event? Pin
Rodney S. Foley28-Aug-02 19:14
Rodney S. Foley28-Aug-02 19:14 
GeneralRe: How do you unsubscribe to an event? Pin
Paul Riley28-Aug-02 19:25
Paul Riley28-Aug-02 19:25 
GeneralRe: How do you unsubscribe to an event? Pin
Rodney S. Foley28-Aug-02 19:50
Rodney S. Foley28-Aug-02 19:50 
GeneralRe: How do you unsubscribe to an event? Pin
leppie28-Aug-02 22:02
leppie28-Aug-02 22:02 
Generalam i dreaming Pin
imran_rafique28-Aug-02 11:58
imran_rafique28-Aug-02 11:58 
GeneralRe: am i dreaming Pin
Rodney S. Foley28-Aug-02 14:46
Rodney S. Foley28-Aug-02 14:46 
GeneralRe: am i dreaming Pin
Nnamdi Onyeyiri28-Aug-02 21:48
Nnamdi Onyeyiri28-Aug-02 21:48 
GeneralRe: am i dreaming Pin
Paul Riley28-Aug-02 17:56
Paul Riley28-Aug-02 17:56 
GeneralRe: am i dreaming Pin
imran_rafique1-Sep-02 12:15
imran_rafique1-Sep-02 12:15 
GeneralRe: am i dreaming Pin
Paul Riley2-Sep-02 5:03
Paul Riley2-Sep-02 5:03 
GeneralRe: am i dreaming Pin
imran_rafique1-Sep-02 12:50
imran_rafique1-Sep-02 12:50 
Generalwho to use it Pin
imran_rafique28-Aug-02 11:22
imran_rafique28-Aug-02 11:22 
GeneralPropertySheet Pin
Mazdak28-Aug-02 9:49
Mazdak28-Aug-02 9:49 
Generalpassing System.Guid into mediaSeek.SetTimeFormat Pin
Jarrett Vance28-Aug-02 8:51
Jarrett Vance28-Aug-02 8:51 
GeneralASP.NET question Pin
User 988528-Aug-02 5:16
User 988528-Aug-02 5:16 
GeneralInterop reference problem... Pin
Ryan Cromwell28-Aug-02 4:48
Ryan Cromwell28-Aug-02 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.