Click here to Skip to main content
15,886,736 members
Home / Discussions / C#
   

C#

 
QuestionProblem with click event of a button on C # Pin
BEN SBAI5-Jan-18 13:41
BEN SBAI5-Jan-18 13:41 
AnswerRe: Problem with click event of a button on C # Pin
Jim Meadors5-Jan-18 16:06
Jim Meadors5-Jan-18 16:06 
AnswerRe: Problem with click event of a button on C # Pin
OriginalGriff5-Jan-18 20:04
mveOriginalGriff5-Jan-18 20:04 
AnswerRe: Problem with click event of a button on C # Pin
BillWoodruff5-Jan-18 21:33
professionalBillWoodruff5-Jan-18 21:33 
GeneralRe: Problem with click event of a button on C # Pin
OriginalGriff5-Jan-18 21:46
mveOriginalGriff5-Jan-18 21:46 
GeneralRe: Problem with click event of a button on C # Pin
BillWoodruff6-Jan-18 2:46
professionalBillWoodruff6-Jan-18 2:46 
QuestionDisabling events of objects stored in array Pin
Pogoodill5-Jan-18 2:32
Pogoodill5-Jan-18 2:32 
AnswerRe: Disabling events of objects stored in array Pin
OriginalGriff5-Jan-18 3:14
mveOriginalGriff5-Jan-18 3:14 
It's exactly the same syntax: the "-=" removes a specific event handler from the chain of handlers for each event.

To explain that, assume you have a class with an Event:
C#
public class MyClass
    {
    public event EventHandler TestEvent;
    protected virtual void OnTestEvent(EventArgs e)
        {
        EventHandler eh = TestEvent;
        if (eh != null)
            {
            eh(this, e);
            }
        }
    public void Signal()
        {
        OnTestEvent(null);
        }
    }
And that you create an instance, and attach two handlers to the event, and raise teh event:
C#
void mc_TestEvent1(object sender, EventArgs e)
    {
    Console.WriteLine("Event handler 1");
    }
void mc_TestEvent2(object sender, EventArgs e)
    {
    Console.WriteLine("Event handler 2");
    }


private void MyButton_Click(object sender, EventArgs e)
    {
    MyClass mc = new MyClass();
    mc.TestEvent += mc_TestEvent1;
    mc.TestEvent += mc_TestEvent2;
    mc.Signal();
    }
You will get this output:
Event handler 1
Event handler 2
Because event handlers are "chained" - all handlers attached to the event for that specific instance will get fired.
So this code:
C#
MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass();
mc1.TestEvent += mc_TestEvent1;
mc2.TestEvent += mc_TestEvent2;
mc1.Signal();
Will only output one line:
Event handler 1
Because only one event handler is attached for each instance. To get the output
Event handler 1
Event handler 2
You would have to call Signal on both instances:
C#
MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass();
mc1.TestEvent += mc_TestEvent1;
mc2.TestEvent += mc_TestEvent2;
mc1.Signal();
mc2.Signal();
the "-=" syntax removes an event handler:
MyClass mc = new MyClass();
mc.TestEvent += mc_TestEvent1;
mc.TestEvent += mc_TestEvent2;
Console.WriteLine("Two:");
mc.Signal();
mc.TestEvent -= mc_TestEvent1;
Console.WriteLine("One:");
mc.Signal();

Gives you:
Two:
Event handler 1
Event handler 2
One:
Event handler 2

So, if you have a collection of MyClass objects, you can remove the handler inside the loop using exactly the same syntax:
C#
foreach (MyClass mc in myClassesCollection)
    {
    mc.TestEvent -= mc_TestEvent1;
    }


Note that this doesn't "turn the events off" it just removes a specific handler method from the instance (which probably as far as you care, turns it off!)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

GeneralRe: Disabling events of objects stored in array Pin
Pogoodill5-Jan-18 3:55
Pogoodill5-Jan-18 3:55 
AnswerRe: Disabling events of objects stored in array Pin
OriginalGriff5-Jan-18 4:59
mveOriginalGriff5-Jan-18 4:59 
GeneralRe: Disabling events of objects stored in array Pin
Pogoodill5-Jan-18 6:13
Pogoodill5-Jan-18 6:13 
AnswerRe: Disabling events of objects stored in array Pin
BillWoodruff6-Jan-18 2:12
professionalBillWoodruff6-Jan-18 2:12 
AnswerRe: Disabling events of objects stored in array Pin
Gerry Schmitz6-Jan-18 6:55
mveGerry Schmitz6-Jan-18 6:55 
QuestionGet current point in time in recurring time interval. Pin
Member 136084655-Jan-18 2:10
Member 136084655-Jan-18 2:10 
AnswerRe: Get current point in time in recurring time interval. Pin
Ralf Meier5-Jan-18 2:38
mveRalf Meier5-Jan-18 2:38 
AnswerRe: Get current point in time in recurring time interval. Pin
Gerry Schmitz6-Jan-18 7:06
mveGerry Schmitz6-Jan-18 7:06 
QuestionHow to fetch master detail data using EF and LINQ Pin
Mou_kol5-Jan-18 0:15
Mou_kol5-Jan-18 0:15 
AnswerRe: How to fetch master detail data using EF and LINQ Pin
Vincent Maverick Durano5-Jan-18 2:20
professionalVincent Maverick Durano5-Jan-18 2:20 
GeneralRe: How to fetch master detail data using EF and LINQ Pin
Mou_kol6-Jan-18 6:08
Mou_kol6-Jan-18 6:08 
GeneralRe: How to fetch master detail data using EF and LINQ Pin
Vincent Maverick Durano8-Jan-18 15:33
professionalVincent Maverick Durano8-Jan-18 15:33 
QuestionPetaPoco (T4 Template) in .Net Core 2.0 for MSql : - SerializationException in generating Database.cs Pin
J Kaur4-Jan-18 15:18
J Kaur4-Jan-18 15:18 
AnswerRe: PetaPoco (T4 Template) in .Net Core 2.0 for MSql : - SerializationException in generating Database.cs Pin
BillWoodruff4-Jan-18 21:37
professionalBillWoodruff4-Jan-18 21:37 
PraiseRe: PetaPoco (T4 Template) in .Net Core 2.0 for MSql : - SerializationException in generating Database.cs Pin
J Kaur11-Jan-18 10:52
J Kaur11-Jan-18 10:52 
AnswerRe: PetaPoco (T4 Template) in .Net Core 2.0 for MSql : - SerializationException in generating Database.cs Pin
Vincent Maverick Durano5-Jan-18 1:59
professionalVincent Maverick Durano5-Jan-18 1:59 
PraiseRe: PetaPoco (T4 Template) in .Net Core 2.0 for MSql : - SerializationException in generating Database.cs Pin
J Kaur11-Jan-18 10:51
J Kaur11-Jan-18 10:51 

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.