Simple way to get UpDown box to move in values not 1 (non unitary) Topic is solved

Helpful script writing tricks and HowTo's
IWantItAll2
Posts: 8
Joined: 14 Jun 2023, 10:00

Simple way to get UpDown box to move in values not 1 (non unitary)  Topic is solved

Post by IWantItAll2 » 15 Jun 2023, 04:48

I Hope this helps someone. It took me a long time to work out how to get this to work and i was very frustrated.
The code is a simple way to change a Updown box in values that aren't 1. so you can change Inc to any value and it should work.
P.S im sure someone smarter than me can make an easier way, but i couldn't find any help online for this!

Code: Select all

; AutoHotkey Version: 2.0.2.00
; Language:       English
; Platform:       Win10
; Function: adjust upDown control in steps that arent 1

Inc := 0.25 ; amount to increment by

myGUI :=Gui()

	myGUI.Add("Text", , "My value")
	myGUI.Add("Edit", "ys w80 vTextValue", "5")	
	myGUI.Add("upDown", "-2 vUDValue Range-1-1").OnEvent("Change", UD_change) ;-2 to make UD and edit seperate
	
myGUI.Show()
myGUI.submit(0)
return

UD_change(*) ; updown clicked
{
	FormVars := myGUI.Submit(0)

	If( FormVars.UDValue < 0 ) ; decide if value should go up or down
	{
		newValue := FormVars.TextValue - Inc
	}
	Else
	{				
		newValue := FormVars.TextValue + Inc		
	}
	
	myGUI['UDValue'] = 0 ;set UD to 0 so value out is -1 or 1
	myGUI['TextValue'].Value := newValue ;set the value back in the form
	return	
}

User avatar
kunkel321
Posts: 1293
Joined: 30 Nov 2015, 21:19

Re: Simple way to get UpDown box to move in values not 1 (non unitary)

Post by kunkel321 » 23 Jul 2024, 09:36

Cool code solution! On a related note: Here is a setup for Listbox navigation that "loops." Also Shift+Up/Down allows to scroll by multiple lines.

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; Loop ListBox (experimental) proof of concept. 
; with hotkeys rather than onEvent.

fruits := ['Apple', 'Banana', 'Cherry', 'Date', 'Elephant', 'Fig', 'Guava', 'Honeydew', 'Kiwi', 'Lemon', 'Mango']
skipNum := 4
gu := gui(,)
guHwnd := gu.Hwnd	
gu.Add('edit', 'w200', "Fruit Looping")
lb := gu.add('listBox', 'w200 r' fruits.Length, fruits)
gu.show()

#HotIf WinActive(guHwnd) and lb.Focused and lb.Value = fruits.Length
	Down::lb.Value := 1
#HotIf WinActive(guHwnd) and lb.Focused and (lb.Value <= fruits.Length - skipNum) 
	+Down::lb.Value := lb.Value + skipNum
#HotIf WinActive(guHwnd) and lb.Focused and lb.Value = 1
	Up::lb.Value := fruits.Length
#HotIf WinActive(guHwnd) and lb.Focused and (lb.Value >= skipNum) 
	+Up::lb.Value := lb.Value - skipNum
#HotIf

Esc::ExitApp
The HotIfs are not the most elegant thing, but it works.

EDIT: Just Me posted an alternate version here: viewtopic.php?f=82&t=126015&p=559057&hilit=fruits+%3A%3D+%5B%27apple%27%2C+%27banana%27%2C+%27cherry%27%2C+%27date%27%2C+%27elephant%27%5D#p559057
It doesn't have the "skipjump" but his solution for the "looping" is a little more elegant.
name := "ste(phen|ve) kunkel"

kiwichick
Posts: 189
Joined: 21 Jan 2014, 22:03

Re: Simple way to get UpDown box to move in values not 1 (non unitary)

Post by kiwichick » 28 Jul 2024, 04:08

Great script! However, it seems to only work for increments that are multiples of, or divisions of, 0.25.

For example:
0.5 [0.25 x 2] works.
0.75 [0.25 x 3] works.
0.125 [0.25 / 2] works.
0.1 doesn't work. The result of clicking the up arrow is: 5.0999999999999996. The result of clicking the down arrow is: 4.9000000000000004.
0.33 doesn't work. The result of clicking the up arrow is: 5.3300000000000001. The result of clicking the down arrow is: 4.6699999999999999.

Post Reply

Return to “Tutorials (v2)”