Message box block the mouseLeftButtonUp event - c#

I'm having a problem with release a captured mouse. My application demand that I make a sequence of actions (means that they will run automatically one after another). The sequences are:
_ Select an object
_ Enter a value on a message box for the object to rotate according to the entered value.
At run time: I click on the object, it activate the MouseLeftButtonDown event of the selected object. Then the message box appears. This message box blocks my mouse to activate MouseLeftButtonUp event of that object. So I have to click the object one more time to activate the MouseLeftButtonUp event or my application cannot continue running.
Can anyone helps me with this?

That's why a lot of controls only take action on the MouseUp event. They use the MouseDown event to do something like indicating state or selection. And use mouse capture (Capture property in Winforms) to ensure that they'll get the MouseUp event even if the mouse is moved outside of the window. Try it in your browser right now, press and hold the right mouse button, nothing happens, let it go. Exact same behavior when you left-click a link or button.
Sounds like that's what you want to do as well.

Related

Activate Click and MouseDown events at the same time?

I am trying to create a form where the user selects one of 2 radio buttons, "fast" and "slow", then presses a "go" button on the form. It should work as follows:
When "fast" is selected and "go" is pressed, the user needs to continue holding down the button in order to make the player move and the player stops when the user releases their finger from the button. For this, I am using the MouseDown and MouseUp events.
If "slow" is selected, the user can only move one step at a time, regardless of how long the button is held down. for this, I am using the Click event.
To test this concept, I put a MessageBox in the Click and MouseDown events to see how they work together. When I click on the button, however, I am only seeing the Mousedown event get triggered, regardless of which radio button is selected and the Click event is only triggered when the go button is in focus and I press enter.
How can I make it so that both events get called simultaneously (and then I can put the respective if statement in to differentiate between "fast" and "slow" radio buttons)? (The button is wired up to these events by double-clicking on their respective handler in the design window, not hardcoded).
private void go_button_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("Mousedown"); // Doesn't show in console window in Release mode, still trying to solve this
MessageBox.Show("Mosuedown");
}
private void go_button_Click(object sender, EventArgs e)
{
Console.Out.WriteLine("go_buttonclick"); // Doesn't show in console window in Release mode, still trying to solve this
Console.WriteLine("go_buttonclick"); // Doesn't show in console window in Release mode, still trying to solve this
MessageBox.Show("go_buttonclick");
}
I found what I was doing wrong, both events are being triggered at the same time, but the MessageBox in the MouseDown event wasn't allowing me to see the Click and MouseUp events also being triggered. When I turned off the MouseDown MessageBox, the other 2 MessageBoxes appeared, thanks!

c# Prevent dragging box over another

I am using visual studio. In my program I have the option for a user to activate certain widgets that they want to have. Each widget is a UserControl, so when they click to show a widget, it is activating the corresponding user control. Once they activate the control, they can drag the boxes wherever they wish so that they can customize how their screen looks. However, whenever you drag one widget over another, it just stacks them on top of each other, and this is not what I want. If I am dragging a box and it touches another one, I want it to stop and not be able to overlap it. How can I do this? I suspect it has something to do with the "DragOver" event, but not for sure.
The DragOver event will be raised on these case :
[+] If the user moves out of a window, the DragLeave event is raised.
[+] If the mouse enters another control, the DragEnter for that control is raised.
[+]If the mouse moves but stays within the same control, the DragOver event is raised.
If this is the event that you want, so try to terminate the widget (User Control) on UserControl_DragOver.
You can terminate your UserControl like this :
private void Btn_Cancel_Click(object sender, EventArgs e)
{
this.Parent.Controls.Remove(this);
}
(If you mean sth else, tell in comment)

C#.NET mnemonics unexpected behaviour

I have few buttons in my windows application which has mnemonics.Now if i press multiple keys the events of the button clicks gets fired as i have used mnemonics.
But the behaviour is not as expected,because second button event handler is getting executed before the first button event handler has finished its execution,and also i have written my code in such a way that in the event handler of first button i am disabling my second button,still the second button event handler is getting executed.The problem here is due to mnemonics.PLease suggest me a better way to handle mnemonics as soon as possible.
Thanks in advance.
If your logic is tied to your buttons, change that. Let your buttons merely manipulations an object that implements your logic. You can then check in this object whether the requested action is allowed (i.e. whether the previous operation has finished.
Alternatively you can just disable buttons when running and re-enable then when.finished.

How to handle button "up" events across multiple similar buttons.

I have multiple buttons that are using a single command with a command parameter to act on their "down" event among all types of interactions (click, keypress, touch). Turns out this isn't enough for my application, and I must also know when the button is released. How do I do something similar for these buttons "up" event?
Look at the OnIsPressedChanged() method. You may have to derive a class from Button to be able to handle it.
Also, you may handle the MouseLeftButtonUp event, but it will fire only if the button is pressed by a mouse click. If you want to catch keyboard, you will have to process KeyUp events and determine if the the button was clicked as a result of that key (Space, Enter in some cases, a shortcut key, etc.)
Unfortuntely there's no special command for the UP-event.
But you can use EventToCommand behavior
I've found a combination of events are required to listen to:
PointerReleased, PointerCanceled, PointerCaptureLost should all be listened to in a touch environment for "up" events.

C# Event-Handling

I am working on a C# piano. I have already built the music keyboard and the staff. Everytime a user presses a key, it is displayed on the staff in its relevant position.
The music note displayed on the staff is stored in an array of pictureboxes, as shown below.
public void addPictureBox(int x, int y, Image image)
{
picBox[cnt] = new PictureBox();
picBox[cnt].Image = image;
picBox[cnt].Location = new Point(x, y);
picBox[cnt].BackColor = Color.Transparent;
panel3.Controls.Add(picBox[cnt]);
picBox[cnt].BringToFront();
picBox[cnt].MouseDown += new MouseEventHandler(Pic_MouseDown);
picBox[cnt].MouseUp += new MouseEventHandler(Pic_MouseUp);
cnt++;
}
The Pic_MouseDown and Pic_MouseUp events allow the user to play the note by clicking on it from the staff.
What I want to do now is to create an event on picBox[cnt] for dragging. However, picBox[cnt].MouseDown and picBox[cnt].MouseUp have already been registered to Pic_MouseDown and Pic_MouseUp event handlers.
How can I do an event to handle dragging since MouseDown and MouseUp have already been registered to other event handlers?
Thanks :)
The great thing about event handlers is that you can have as many attached handlers as you want. The += (operator overload) means you are attaching a new event handler to the existing handlers. You can add as many event handlers as you desire.
Event Handler Overview
If you create a isDragging boolean instance field, which you set to true in the mouse down and false in mouse up, then you can use the mouse move event to detect whether the object should be moved or not.
You'll need to use a combination of MouseDown, MouseMove and MouseUp events. In MouseDown, you do little more than set a flag to indicate that the mouse was pressed, and record where it was pressed. In MouseMove, you check to see if the button is still down and the cursor has moved further than SystemInformation.DragSize, which indicates that the user is dragging rather than clicking, and start the drag operation if needed. In MouseUp, you either complete the drag or perform the click action.
I believe the conventional wisdom for Drag-n-drop is
Call DoDragDrop in the (source control's) MouseDown handler
set AllowDrop = true in the targe control's AllowDrop property
There's a whole series of drag events to fine tune behavior
One does not seem to have to worry about any latent MouseUp or MouseClick events. I'm assuming this is because the physical actions of button press and release are done over different controls and/or the mouse physically moved "far enough".

Categories