How to restrict user from double clicking any command button - c#

There is an issue that I am facing, is to restrict user from double clicking on a command button. What it does is execute twice the code written inside it's click event. I've read many solutions for it, which I find are irrelevant. The command button, they say, should be disable after a click is performed so that user won't be able to perform another click. This will create an issue when an error occur and the code that is written to enable the button didn't execute.
Is there any other way to do it? Please suggest if any one have better option than this.

You could disable it, and put all your code within a try, catch, finally clause, and put the enabling code in finally.
Give it a read.
That way it should always run, exception or not.

I assume that you are referring to WPF, as Windows Forms has a specific double click event which can be disregarded.
Unfortunately WPF does not make a distinction between double clicking a button and clicking the button twice. This means that you have to perform the check to see if the second click occurred too soon to the first click to be regarded.
This could be done by storing the DateTime.Now in a member variable in the button click event handler and if the click occurs within a too short amount of time of the previous click, simply indicate that the button click event was handled and return without doing anything.

Related

Debugging Web Application

I have stepped through my code, and it stops when the page is fully loaded, as it should. However, their are a few buttons on the page, that I DO NOT see the names of in my code, so I can't discover where to add a breakpoint to, to follow the logical flow. How can I see what is happening, 'behind the scenes' when one of those buttons is pressed?
EDIT---Would this be an applicable scenario to use tracing?
If you add a Page_Load event to the WebForm, you can put a breakpoint there. That event / method will be called every time there is a server-side interaction with any of the controls on the page, followed by the button click (or whatever other interaction is occurring). When on that breakpoint during a PostBack, you can look at the Form["__EventTarget"] value to determine what control is being invoked and the Form["__EventArgument"] may contain additional details on what event is being triggered.

How to separately handling MouseClick and MouseDoubleClick event on ChartControl

I'm implementing a module which needs to handle both MouseClick and MouseDoubleClick on a ChartControl of DevExpress. The version that I'm using is v12.2.
When I double click on that chart, both events are fired. I'd like (and I think that it must be) it just fires one event, in this case, MouseDoubleClick.
So, anyone know how to fix that problem?
What's I've tried:
Handle MouseClick or Click event and see MouseEventArgs#Clicks property. But it's always 1.
What's I'm using:
Declare a boolean variable to tell if MouseDoubleClick is fired. On MouseClick handling, just waiting for a moment, then do the codes if that variable does not turn on. I think this is a bad implementation.
You need a time machine to see the difference between the two. Inevitably a double-click starts with a single click, you always see the first click first.
You can get a time machine that sees the future by using a timer that delays the past. Set its interval to SystemInformation.DoubleClickTime + 16 and start it in the Click event, stop it in the DoubleClick event. If the Tick event fires then it was a single click.
That works, but do note that the delayed response to a single click is fairly annoying. Best not to annoy your user with a user interface like that.

Getting events when Button object is released

On Windows 7 Phone, using Silverlight framework
I'd like to handle when a Button is released.
It's easy to tell when the button is pressed (Click event which is fired either when pressed or when released according to the ClickMode property)
I've played with all the other events provided with the Events editor (KeyUp, LostFocus, MouseLeave etc..)
But I'm yet to find something that is definitive in regards to getting an event when a button is released.
Ultimately, I'm trying to handle doing a click vs a long click when pressing on a button
Thanks
For your situation, KeyUp is only half of the story. You also need to handle KeyDown where you will save the current time which you will then compare to the current time value after KeyUp to determine whether the press was short or long. You also need to make sure that you track one particular key in case your handler(s) is/are intercepting all key strokes.
If for some reason ClickDown/Up don't work out you could try handling the Click event but starting with a ClickMode of press, then changing ClickMode to release on the press handler. This process, though not simple, would give you a chance to implement the down-hold and timer-release sequence that you're looking for.

Use right-click with Windows Forms Button

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

WPF: Is there an event that is fired when the user presses the close button [X]?

Is there an event that is fired when the user presses the close button?
This is because the Window Closing event is fired both when one closes the window manually (with the Close method) and also when the user presses the [X] button...but I somehow need to know only when the user presses the [X] button not when the window is closed manually.
I don't believe there is a way to tell those apart in WPF (though I'm not positive).
The way I always handled it in WinForms was to create a member variable "_Closing", set it to false, and a method "ReallyClose()" that would set _Closing to true, then call Close. My Closing handler would then cancel the close if _Closing was not set to true.
Yeah, it's a bit of a hack, but it worked.
I also don't think there is a way to tell them apart. You can put a handler on the Application.Exit event, but it doesn't distinguish between "red X button close" and "alt-F4 close" (or whatever other types of close you are considering).
BTW, if you check for Application.Exit, be sure to check for Application.SessionEnding too - if someone logs off while your app is running, you can't be guaranteed that Application.Exit will be called.
Try to put button with name Cancel and bool variable in your class so When you click on button set it to the true and in Closing Event check if is true use e.Cancel=false to exit window I tried everything and It doesn't work for me and I do on this way and also you can remove X button just to have Accept ot Cancel buttons if you insert some informations.

Categories

Resources