0

I have a button on my WPF form, and when I click and hold on it, I want to be able to drag the mouse up or down to increase/decrease a value.

Here's an explanation I made (not the prettiest)

enter image description here

On step 1: The user clicks and holds down the Left mousebutton.

Step 2: the user continues to hold down the left mousebutton while dragging the mouse upward.

(I want the value to get updated while the user is dragging too)

Step 3: The user releases the left mousebutton, stopping the updates.

Here's where it gets difficult:

How do I get the distance dragged from ex. a timer-elapsed event?

So let's say the first step is called a, and the part when the user releases the mousebutton is called b. I want to get the distance from a to b from ex. a timer (the timer making it update while dragging)

How would I accomplish this?

Tokfrans
  • 1,939
  • 2
  • 24
  • 56
  • Use the MouseDown and MouseUp attached events? You can use the MouseEventAgs.GetPosition (http://msdn.microsoft.com/en-us/library/ms591423(v=vs.110).aspx) to figure where the up and down occurred then calculate the difference (i.e the travel distance). – Peter Ritchie Mar 05 '14 at 17:17
  • @PeterRitchie If I use these events, I cannot update the value while I'm dragging. Only the start - end positions – Tokfrans Mar 05 '14 at 17:19
  • 1
    You can also use the MouseMove attached event. See http://msdn.microsoft.com/en-us/library/system.windows.input.mouse_attachedevents(v=vs.110).aspx – Peter Ritchie Mar 05 '14 at 17:20
  • @PeterRitchie Hmm, this can work. Let me try something out. – Tokfrans Mar 05 '14 at 17:21
  • @PeterRitchie Ah, that made it work. I used the mousedown to get the point of origin, and then I just updated another point which got updated by the mousemove, and did some simple math. That works – Tokfrans Mar 05 '14 at 17:35

1 Answers1

0

You're looking for a Slider:

<Slider Maximum="10" Minimum="1" Orientation="Vertical"/>

Output:

enter image description here

No need to reinvent the wheel...

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Yes, spot on. But I only wanted a button to control the value, not a whole slider. – Tokfrans Mar 05 '14 at 17:43
  • @Tokfrans the "whole slider" is just a `Thumb` and a `Track`, either change the slider to not contain the `Track`, or change the `Track` style to something else. in WPF, UI elements are `lookless` and the choice of the proper controls is based on functionality, not appearance. – Federico Berasategui Mar 05 '14 at 17:45