tags:

views:

898

answers:

5

When you add a DateTimePicker control, you can select part of the control value (for example the month) and then use the up/down arrows to modify this part of the date/time value (for example, incrementing/decrementing the month).

What i would like to do is to allow the same thing with the mouse wheel. I tried to register on the event MouseWheel but I can't find a way to know which part of my date/time is currently selected, so I have no way to know if i should increment the time, the day, the month or the year.

Is there any way to do this ?

A: 
   dateTimePicker1.MouseWheel +=  new MouseEventHandler(dateTimePicker1_MouseWheel);

    private void dateTimePicker1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        int incrementValue = SystemInformation.MouseWheelScrollLines;
        //Do increment
    }

Reference : http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousewheel.aspx

Fiur
Nope, you didn't answer my question. I know how to get the increment (e.Delta is enough for me). I also know where is the cursor (e.Location) if i need it. What i need to know is which part of my date is currently selected. For example if my date is YYYY MM DD, i need to know if the user selected MM, DD, or YYYY, so that i can increment it. For example, in a textbox, you can get the selection start and length. Or there is a function that gives you the closest index to the position of your cursor. I didn't think anything like that for the DateTimePicker.
Ksempac
Sorry i didn't drinked my coffee and read only the title ;).
Fiur
A: 

If you set the ShowUpDown property to True you get a spin box rather than a drop down and this modifies the highlighted portion of the date. Though I've just found out that this is irrelevant as the up and down arrow keys alter the highlighted portion of the date regardless of whether this property is set to true or not.

If you can find this code you should be able to get the mouse wheel to work as well. I'll take a look and see what I can find.

UPDATE:

I've just found this note about the ShowCheckBox property:

Read/write. If true, a checkbox is displayed to the left of the date-time value in the control. When this checkbox is checked, the values in the control can be changed by the user at runtime by highlighting a portion of the date and pressing the arrow keys. When not checked, the date can only be changed by clicking the drop-down menu to display the calendar. Default is false.

Is this of any use?

UPDATE 2:

I've not been able to find any code that deals with this particular aspect of the DateTimePicker. Don't Microsoft release the code to these sort of controls anymore?

ChrisF
Nope, i already have ShowCheckBox set to true (because i need the checkbox). So, like i said, i can modify the values with the up/down arrows. What i want to do now, is having the same behavior with the mouse wheel (using up/down is pretty frustrating). The problem is, i can find a way to get which part of my control is highlighted.
Ksempac
I've been using .NET on and off for only 2 years, but I've never saw any internal code from Microsoft. All I saw is some samples on how they use the API they offer. If you want to implement your own behavior, it's up to you to figure out the "magic" you should use.
Ksempac
@Ksempac - back in the dim and distant past of Visual C++ and MFC you used to be able to get some of the MFC source code - an invaluable tool in helping work stuff out. MS seem to have stopped this practice with C# - unless I've just not found it yet.
ChrisF
A: 

Am not sure , but is this direction any good ..

Isnt it sure that , x coordinate for month < x for day < x for year (assuming no custom format) within the datepicker control , as its displayed along incresing x axis

then maybe we can capture the dateTimePicker1_MouseDown event and set the x coord where we clicked before the scroll in to a variable say:xCordOfClick , and then use this in Fiur's code for mousewheel to increment based on the difference in where we clicked and the x location of the datepicker .

something like

In dateTimePicker1_MouseDown(object sender, MouseEventArgs e)

    {
        xCordOfClick  = e.X;
    }

In mousewheel event

    {   
        //Do increment based on how far we clicked from the location of the control   
      // increment based on (xCordOfClick   - dateTimePicker1.Location.X).ToString() );
    }
AB
I use a custom format, but that's not a problem, since I'm the one setting it. I did think of something like this but i would like to avoid it since it's relying on assumption like size of the police or format used to determine how far we are in the control.
Ksempac
I agree , its a crude way of doing it , more like the screen scrapping world :) .. nice question though
AB
+6  A: 

A possible, but not quite elegant solution would be something like :

    private void dateTimePicker1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Delta > 0) System.Windows.Forms.SendKeys.Send("{UP}");
        else System.Windows.Forms.SendKeys.Send("{DOWN}");
    }

this should definitely work in general case, but some corner cases might have unexpected results (like having KeyUp/KeyDown events overriden)

Vorber
Thanks a lot. Actually i was trying something like that myself, with a User Control inherited from DateTimePicker, but didn't know how to send an event inside the MouseWheel event handler (tried this.OnKeyDown, but didn't get any result).
Ksempac
+2  A: 

I've tried to figure this one out too. In the end, I ended up with this:

Private Sub dtpDateTimePicker_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If e.Delta > 0 Then
        SendKeys.Send("{Up}")
    Else
        SendKeys.Send("{Down}")
    End If
End Sub

You can automatically add this handler to all your date pickers in your form and it's containers using this method. Just call it once in form_load:

 Public Sub AttachDateTimeMouseWheels(ByRef ownerForm As Control)

    Dim myControl As Control        

    For Each myControl In ownerForm.Controls

        If TypeOf myControl Is DateTimePicker Then
            AddHandler myControl.MouseWheel, AddressOf dtpDateTimePicker_MouseWheel
        Else
            If myControl.Controls.Count > 0 Then
                AttachDateTimeMouseWheels(myControl)
            End If
        End If

    Next

End Sub
Jiri