I have a Button (button1) , How can I copy it's Click property (I mean button1.Click) to another Button (button2), Something like:
button2.Click=button1.Click;
You cannot do this. Events only allow you to add or remove handlers, you cannot get a list of all of the handlers that they have. It is a function that is intentionally removed from you.
You could keep track of the handlers you assign to a particular button yourself, independently, and then add those handlers to another button, to accomplish this effect.
Related
I want to be able to hold down a button (i.e. MouseLeftButtonDown) and have it's event repeat fire.
I code Mouse.Capture(button) in the MouseLeftButtonDown event and Mouse.Capture(null) in MouseLeftButtonUp event.
If I hold the mouse button down, the MouseLeftButtonDown only fires once. I can see this not by setting a breakpoint but by using Console.Writeline().
I had this very code working at some point. Then I refactored and put the mouse events in an Interface. Everything still works except this repeat action now!
Update - More information:
Here is the before call which was in a specific class:
Mouse.Capture(sender as MenuGelButton)
MenuGelButton is a xaml creation of ellipses and text to make a cool button.
Since adding the interface (which is common code for several xaml creations all of which inherit the interface), here is the current call from within the interface:
Mouse.Capture(sender as UIElement)
Update of Update:
A mouse click event is not designed to auto repeat fire. A keyboard event is. I had this working (and it continues to work) on a keyboard event not the mouse event. Sorry for the confusion...
I believe you are looking for Microsoft's RepeatButton. You can use it in XAML just like you would use a Button, and it fires a click event multiple times.
From their documentation:
A RepeatButton is a button that raises Click events repeatedly from the time it is pressed until it is released. Set the Delay property to specify the time that the RepeatButton waits after it is pressed before it starts repeating the click action. Set the Interval property to specify the time between repetitions of the click action. Times for both properties are specified in milliseconds.
Try doing something like this:
mouse button --> fires event
in the event, set a timer of your liking (how many times per second would you like the action to happen?)
in a loop, every time timer fires, check to see if mouse button is still "down". If yes, fire. If not, don't fire.
you'll probably want to add another event of mouse button up, that will stop the timer, so it won't fire anymore (or bind to a bool that will let you know if you want to fire ... up to you ..)
Hope it helps.
There are several KeyPress method in my code. All are working except the one due to no reference. I have checked the project for naming mistake. But there is no any mistake. How can I make reference for this method.
btnback_KeyPress is not working.
Looks like you just don't have the event hooked up. Go into the designer, select the btnback button, then in the Events tab of the Properties window, find the KeyPress event and subscribe the btnback_KeyPress method to it as a handler.
You need to wire the event to the button.
You can do so by selecting your button in the [Design] view and then selecting the Events tab (circled in red) from the properties view.
At KeyPress, enter your event name.
It looks like you're missing a line of code in the Form1.Designer (supposedly it's Form1). Look it up and add
this.btnback.KeyPress += new System.EventHandler(this.btnback_KeyPress);
I just created an Button in xaml so, it created an event for handling the button click.
I want to call the contents inside the event handler as a function. So, that i can call in some other place to satisfy my needs.
Currently i am working in Live tiles. examples that i got are all in the form of button click event to trigger the flip and cycle tiles.
I want to call those into function to make it work.
Or else any one give the example of Lives tiles from the beginning to end
In some cases I have found, developers unwire event then wire it again like this:
control.Click -= new eventHandler(eventHandler)
control.Click += new eventHandler(eventHandler)
why do they do that?
Possibly because there is other code in between that will trigger the event, and they don't want eventHandler to act on it. IMHO this isn't the best way to do things, but it's easy and it works.
If there is no other code in between there cannot possibly be a reason to remove and add the same event handler again.
I can explain this in a simple screnerio.
Imagine you have a "button1"
And you added the event on the run-time.
But once this button is clicked. The process behind it take let say "3Mins" to complete.
And you don't want the user to click this button again.
So you can do by Unwiring it in the first time.
So that the person don't click the button again and again. And adding the instruction in the queue.
If you call code that wires an event, and you don't wont to accidentally wire it up again if it was already wired, you can unwire, and then rewire it. This is the code sample above.
If it wasn't already wired in the first place, no error is thrown. But if the code had already been run first, you don't wont the event to run twice. Thus, unwire and then rewire.
I am writing an app where the user should be able to alter the action of a button.
The user should right-click a button, and choose an option from a pop-up context menu. Once the choice has been made the button will perform a different action when the user uses a normal click.
I've already gotten the "Click" event working for a normal button click, however the "MouseClick" event handler isn't working correctly.
The "MouseClick" event gets activated on regular left-clicks, but never get's called for right-click.
Is there some default event handling being performed that is ignoring that right-click?
I'm sorry to say that this would be a serious UI blooper. Perhaps it would make more sense to add a small combobox next to the button.
Perhaps something like this?
http://www.codeproject.com/KB/buttons/SplitButton.aspx
If you want to display a context menu with actions to choose from it should be enough to assign a ContextMenuStrip to the ContextMenuStrip property. There is usually no need to manually handle the mouse events for that.
surely it would be better to implement it on the MouseDown event rather than the MouseUp event. i dont understand how this is much different from MouseClick event
edit: Just tried this and it works a treat :)
In Button (and certain other controls), the MouseClick event is only fired for the left button. Refer to MSDN.
If you want to know about the right button, respond to the MouseUp event--though as other posters have pointed out, this isn't a great UI idiom.
Use the Mouse UP event... test that Button.X and Button.Y are within the size of the button otherwise you have moved the mouse away from the button.
Terry G