Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i make an event in C# that when i click on a button change the solid brush color?(i use from a canvas)
Posted
Comments
ZurdoDev 4-Jun-15 14:49pm    
What part are you stuck on?
Akbar Fardi 4-Jun-15 14:54pm    
i define a canvas and a button .in click event of button i write this:
SolidColorBrush myBrush = new SolidColorBrush(Colors.Red);
but it dose not work.
[no name] 4-Jun-15 14:56pm    
We would have no idea at all what you think "does not work" means.
Akbar Fardi 4-Jun-15 14:59pm    
when i click on the button it should change the solid brush color but anything didn't change.
[no name] 4-Jun-15 15:01pm    
Probably because you are not actually doing anything with your locally declared brush. You have to actually do something with it or nothing will appear to happen. Re-read your textbook on variable scope.

1 solution

You cannot "make an event", because this event instance already exist in each instance of the class Button. It is called Button.Click. You don't need to "make" it, you need to handle it. That means: adding an event handling to an invocation list of an event instance, which is the instance (not-static) event member of the class Button: Button.Click. The handler is added using the += operator. Equivalent thing can be done in XAML, using the designer or not. The XAML sample can be found here: https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click%28v=vs.110%29.aspx[^].

If you do it in code, the syntax is
C#
myButton.Click += MyClickHander;
// where MyClickHander is the name of the method of the signature:
// public delegate void RoutedEventHandler(
//	Object sender,
//	RoutedEventArgs e
//)
// the method itself should better be private
// for example:
//
void MyClickHander(Object sender, RoutedEventArgs e) {
      // change you color here
}
Please see:
https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.routedeventhandler%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.routedeventargs%28v=vs.110%29.aspx[^].

Note 1: Event mechanism in WPF is very special (https://msdn.microsoft.com/en-us/library/ms753115%28v=vs.110%29.aspx[^]), but in the simple cases we are discussing you don't need to understand anything specific to WPF, it will be enough just follow the patter used for all event handling used in .NET.

Note 2: Some developers may say that my first line in my code sample on top is wrong, because new new RoutedEventHandler(MyClickHander) instead of just MyClickHander. Don't trust them; this is not needed. This "new" is a bogus which probably stems from the imperfections of some Microsoft documentation. This exactly the same kind of bogus as int a = new int(); absolutely redundant construct with correct syntax.

But I use even simpler approach which does not even require knowing the event argument types (in such simple cases). This is done with the use of anonymous methods, especially in lambda syntax:
C#
myButton.Click += (sender, eventArgs) => { SomeCodeHandlingThisEvent(); }

Even if you need to use code specific the particular eventArgs type (this is needed, for example, when you need to get mouse coordinates, and in many other cases), you don't need to write its type, which is inferred through the mechanism of type inference.

—SA
 
Share this answer
 
v4
Comments
Afzaal Ahmad Zeeshan 4-Jun-15 15:21pm    
+5,

A great answer as always, perhaps only one thing. Instead of creating a (C# 2 way of) delegate (as in your first example) you can simply create the function to hold the same signature. Which would remove the ambiguity for the readers who are novice to the delegates.

I also had the problem understanding the delegates for a time. So, I believe this can be made better.
Sergey Alexandrovich Kryukov 4-Jun-15 16:27pm    
Thank you, Afzaal.
Yes, of course. I actually mentioned it in the comment in my first code sample. As you think it would need some clarification, I just added a sample code to show it.
I also added one new note (#2) which is related to it.
—SA
Afzaal Ahmad Zeeshan 4-Jun-15 16:48pm    
Thank you for more clarification Sergey sir, the new is mostly used when it is not even required just to avoid NullReferenceException (unexpectedly), but I myself use these lambda expression, they are also translated to the delegates. They are much more simpler and in-line. The beauty of C# is the structure and compactness it has.
Sergey Alexandrovich Kryukov 4-Jun-15 17:05pm    
Right. As to NullReferenceException, it is irrelevant to the cases when you add a handler using a method name: no chance to get null, as well in case of using assignment to an integer value instead of stupid new int() (amazingly, I actually saw it recently on one CodeProject question unrelated to the issue, one of the members commented on it; I never saw such weird thing before; moreover, new int() can even hurt performance, because it created a boxed System.Int32 followed by unboxing at the assignment).

As to the use of anonymous delegates (lambda or not) in event handling: in additional to known benefits, I think it also improve code maintenance. I generally never add any event using the designer and not even in XAML; in my opinion, using the designer for anything non-visual is always the overuse/abuse or it; it also hurts maintenance.

Now, some news: it looks like I'm becoming an author of such a folklore genre as proverb. Please see (the last quite in the post): Feeding Apple or Why Don't We Answer Some Questions. :-)

—SA
Afzaal Ahmad Zeeshan 4-Jun-15 17:13pm    
Nice saying, I also agree that using the designer or the XAML markup would reduce efficiency as compared to attaching the handlers on the run-time (probably in the constructor).

Also, that new int() poor performance is clearly not a technical assumption, but a very general conclusion for first being boxed (as object) then being unboxed (as a valid integer value). If I have to re-vote a 5 I would, for this statement.

I would definitely give it a look (and to others along with it). :) Thank you for sharing the news of starting the blog. I might be interested in reading the blog of yours, commonly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900