How to set spin button increment on time field?

LO 6.4.3.2-snap1, Ubuntu 18.04 with all updates.

Hello :slight_smile:

I have a time field on a form with the spin button option enabled.

This works BUT it increments/decrements by 1 minute per click. On Number field controls we can set the increment value to something other than 1 but this option disappears on the Time Field control.

Has anybody found a way to set the increment to a different value (e.g. 15 minute intervals)?

I know I can get over this by replacing the field with two dropdowns but, if possible, it is more appropriate in this case to use a single field.

Would the two dropdowns work for other people who need this? If so, consider posting an answer, as it may be the only solution that does not require a macro.

Hello,

Even after a bit of digging, have not found a way to change the increment. BTW, the increment is by 1 minute or hour depending upon where the cursor is placed. The increment/decrement works regardless if the spin button is present or not. Just use the arrow up/down keys.

Only thought at this time is by creating your own macro.

Edit:

Sorry for delay but turned out to be unexpectedly busy today.

Have found a way to do in a macro with some push buttons. Agree with @jimk abut lack of knowing which button is pressed.

There are methods in the controls view for up() and down(). The additional problem here is as I mentioned, depending upon where the cursor is as to whether the hours or minutes is affected. So that can be done by setting the selection. You can use two push buttons - one for up & one for down (adjusting by 15 or whatever you want). The spin buttons can be left in the time control if you want single adjustments.

Both push buttons call the same macro. Used Execute action event. The name is checked to see what function to apply.

Option Explicit

Sub TimeFieldAdjust(oEvent)
    Dim oForm  As Object
    Dim oDocCtl As Object
    Dim oField  As Object
    Dim oCtlView  As Object
    Dim x As Integer
    Dim oSelection As New com.sun.star.awt.Selection
    oForm = ThisComponent.Drawpage.Forms.getByName("Form")
    oDocCtl = ThisComponent.getCurrentController()
    oField = oForm.getByName("TimeField1")
    oCtlView = oDocCtl.GetControl(oField)
    oSelection.Min = 0
Rem Setting Max to 1 or 2 places it in the hours portion
    oSelection.Max = 4
    oCtlView.Selection = oSelection
    If oEvent.Source.Model.Name = "PBdown" Then
        For x = 1 to 15
            oCtlView.down()
        Next x
    else
        For x = 1 to 15
            oCtlView.up()
        Next x
    End If
End Sub

Of course, change names of form, control and push button (my other for test was PBup) to fit your needs.

For 15 minute increments, I created a separate spin control and then set the code below to run on the “While Adjusting” event.

Sub SpinAdjustment(oEvent)
	oSpin = oEvent.Source
	spinUp = True
	If oSpin.getValue() = 0 Then
		spinUp = False
	End If
	oSpin.setValue(1)  'default value'
	oForm = oSpin.getModel().getParent()
	oTimeCtrl = oForm.getByName("Time1")
	oTime = oTimeCtrl.getCurrentValue()
	MINUTES = 15
	If spinUp Then
		If oTime.Minutes + MINUTES < 60 Then
			oTime.Minutes = oTime.Minutes + MINUTES
		Else
			oTime.Minutes = oTime.Minutes + MINUTES - 60
			If oTime.Hours = 23 Then
				oTime.Hours = 0
			Else
				oTime.Hours = oTime.Hours + 1
			End If
		End If
	Else	'spin down'
		If oTime.Minutes - MINUTES >= 0 Then
			oTime.Minutes = oTime.Minutes - MINUTES
		Else
			oTime.Minutes = oTime.Minutes - MINUTES + 60
			If oTime.Hours = 0 Then
				oTime.Hours = 23
			Else
				oTime.Hours = oTime.Hours - 1
			End If
		End If
	End If
	oTimeCtrl.Time = oTime
End Sub

My idea was to give the spin control a range of 0 to 2, with default value 1. Then if it ends up as 2, spin up. Otherwise it will end up as 0, which is spin down. It seems like there should be a way for the event to tell us whether it was up or down, but I did not find a way.

Hello :slight_smile:

Big thanks to @Ratslinger and @jimk for their work, pointing me in the right direction.

In the sample DB TimeSpinner.odb I have used both a spinner and listboxes to change the time of the same field - neither of which I have been able to achieve without using macros! (OK, I admit it, I like coding.)

When the form loads it sets the listboxes to the correct values but I haven’t put in the code to synchronize them when changes are made via the spinner - figured it highly unlikely that this would be a required scenario!

Jim’s solution is the more elegant and, after playing around for a while I found that the getValue() routine returns positive values for increment, negative for decrement. Armed with this info, I have made a couple of minor changes:

  • Use the standard options to set the
    spin control’s increment to the
    desired value.
  • Set the default to 0
  • Set the minimum to -100 (or any
    negative value less than the negative
    of your increment. e.g. increment is
    15, minimum -16). If you don’t do
    this then the down button can become
    unavailable.

I also modified the macro slightly and made it easy to see where to put the name of the Time field for those who may find these things a little daunting.


Sub SpinAdjustment(pEvent)
DIm oSpin as object:Dim oForm as object:Dim oTimeCtl as object
Dim spinUp as boolean
Dim increment as integer
Dim oTime 
Dim sTimeFieldName as string ' name of time field'


'#######################################################################'
'###		ENTER NAME OF TIME FIELD HERE						########'
'#######################################################################'

sTimeFieldName = "timMain"

'#######################################################################'


    oSpin = pEvent.Source
'###	get value of inc / dec then reset current value of control    '
'###	If the value is negative then down button was pressed, if positive then up'

    increment = oSpin.getValue()
    oSpin.setvalue(0)	

'###	Are we up or down? '
    spinUp= iif( increment >0,true, false)
    
'###	find the time field'
    oForm = oSpin.getModel().getParent()
'###	use name supplied above'
    oTimeCtl = oForm.getByName(sTimeFieldName)
    oTime = oTimeCtl.getCurrentValue()
   
    If spinUp Then
        If oTime.Minutes + increment < 60 Then
            oTime.Minutes = oTime.Minutes + increment
        Else
            oTime.Minutes = oTime.Minutes + increment - 60
            If oTime.Hours = 23 Then
                oTime.Hours = 0
            Else
                oTime.Hours = oTime.Hours + 1
            End If
        End If
    Else    'spin down'
        If oTime.Minutes + increment >= 0 Then
            oTime.Minutes = oTime.Minutes + increment
        Else
            oTime.Minutes = oTime.Minutes + increment + 60
            If oTime.Hours = 0 Then
                oTime.Hours = 23
            Else
                oTime.Hours = oTime.Hours - 1
            End If
        End If
    End If
    oTimeCtl.boundfield.updatetime(oTime)
 
End Sub