Alternative to UpDown control! (Simple Increment Implementation!)

Post your working scripts, libraries and tools for AHK v1.1 and older
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Alternative to UpDown control! (Simple Increment Implementation!)

03 Sep 2018, 19:03

Hi,

Since I think UpDown control is limited or maybe not so very well designed (damn it, neither A_GuiEvent nor A_EventInfo tells you which button was pressed), so I decided to use the below alternative instead (Hope you find it useful! Thanks):
UpDown Custom Control - Edit (Increments).gif
(157.19 KiB) Downloaded 190 times

Code: Select all

gui, add, Edit, w50 vUserValue center gUpDown, 5
gui, add, button, x+5 h11 w20 gUpDown, ▴
gui, add, button, y+0 h11 w20 gUpDown, ▾

gui, add, text, x+5 y+-18, Increment=
gui, add, edit, x+5 w50 vIncrement center, 1 


gui, show
return

UpDown:		;_________________ UpDown ____________________

guicontrolget, UserValue
guicontrolget, Increment

	;"==" is always case-sensitive

if (a_guicontrol == "▴")
UserValue := UserValue + Increment

if (a_guicontrol == "▾")
UserValue := UserValue - Increment

	;Ternary operator, "? = if", ": = else"
	;in the exmple below, Range is from -50 to 100

if UserValue is number
UserValue := UserValue < -50 ? -50 : UserValue > 100 ? 100 : UserValue
else if (UserValue != "" and UserValue != "-")
UserValue := "0"

guicontrol, -g, UserValue		;remove gLabel from "UserValue" control in order to prevent infinite loop
guicontrol, , UserValue, % UserValue
send, {End}				;move the caret\cursor to the end of the edit control
guicontrol, +gUpDown, UserValue		;add gLabel back again to "UserValue" control

hh++				;just for test
tooltip, % "loop test " hh	;if "guicontrol, -g, UserValue" above line was not in use, this tooltip would go into an infinite loop

return

guiclose:	;_________________ gui close _________________
exitapp
just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Alternative to UpDown control! (Simple Increment Implementation!)

04 Sep 2018, 03:48

I cannot see any advantage over the UpDown control:

Code: Select all

#NoEnv
SetBatchLines, -1

Gui, Add, Edit, w50 vUserValue Center gUpDown, 5

Gui, Add, Updown, x+0 hp -16 vSign Range-1-1 gUpDown, 0 ; create an isolated UpDown control

Gui, Add, Text, x+5 yp hp +0x0200, Increment= ; +0x0200 = SS_CENTERIMAGE (center vertically)
Gui, Add, Edit, x+5 w50 vIncrement Center, 1

Gui, Show
Return

UpDown:		;_________________ UpDown ____________________

GuiControlGet, UserValue
GuiControlGet, Increment

	;"==" is always case-sensitive

If (Sign <> 0) {
   UserValue += Sign * Increment
   GuiControl, , Sign, 0
}

	;Ternary operator, "? = If", ": = Else"
	;in the exmple below, Range is from -50 to 100

If UserValue is number
   UserValue := UserValue < -50 ? -50 : UserValue > 100 ? 100 : UserValue
Else If (UserValue != "" and UserValue != "-")
   UserValue := "0"

GuiControl, -g, UserValue		;remove gLabel from "UserValue" control in order to prevent infinite loop
GuiControl, , UserValue, %UserValue%
Send, {End}				;move the caret\cursor to the end of the Edit control
GuiControl, +gUpDown, UserValue		;Add gLabel back again to "UserValue" control

hh++				;just for test
ToolTip, % "loop test " hh	;If "GuiControl, -g, UserValue" above line was not in use, this ToolTip would go into an infinite loop

Return

GuiClose:	;_________________ Gui close _________________
ExitApp
Edit: Bugfix: replaced Change by Sign

You can even use it with a buddy control:

Code: Select all

; 0x02 = UDS_SETBUDDYINT -> docs.microsoft.com/en-us/windows/desktop/Controls/up-down-control-styles
; -0x02 prevents the UpDown control from updating its buddy control automatically
Gui, Add, Updown, -0x02 vSign Range-1-1 gUpDown, 0
Last edited by just me on 15 Sep 2018, 04:50, edited 1 time in total.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Alternative to UpDown control! (Simple Increment Implementation!)

04 Sep 2018, 11:51

just me wrote:I cannot see any advantage over the UpDown control:
Yes, you can! (It's easier to use!)

and by the way, If (Change <> 0) makes no sense in your code (Seems totally unnecessary)! (If (Sign <> 0) would make more sense!)

Anyway, I modified your code in case an user wants to manually type a number inside the edit control or wants to use "Up\Down" keyboard arrows to change edit control value (by using "-0x02" option instead "-16" which is very useful and it should be referenced in the Autohotkey documents):

[Edited]:
"-0x02 prevents the UpDown control from updating its buddy control", the otherwise is valid too, "-0x02 prevents the buddy control from updating its UpDown control"

Code: Select all

Gui, Add, Edit, w50 vUserValue Center gUpDown, 5

Gui, Add, Updown, x+0 hp vUDState Range-1-1 gUpDown -0x02, 0
;-0x02 prevents the UpDown control from updating its buddy control (and vice-versa) automatically! (should be referenced in the documents)
;"-0x02" is better than "-16" option because "Up\Down" keyboard arrows can still be used to change buddy control value! 

Gui, Add, Text, x+5 yp hp +0x0200, Increment=		; +0x0200 = SS_CENTERIMAGE (center vertically)
Gui, Add, Edit, x+5 w50 vIncrement Center, 1

Gui, Show
Return

UpDown:		;_________________ UpDown ____________________

GuiControlGet, UserValue
GuiControlGet, Increment
GuiControlGet, UDState
GuiControl, , UDState, 0

if (UDState = 1)
UserValue := UserValue + Increment
else if (UDState = -1)
UserValue := UserValue - Increment


	;Ternary operator, "? = If", ": = Else"
	;in the exmple below, Range is from -50 to 100

If UserValue is number
UserValue := UserValue < -50 ? -50 : UserValue > 100 ? 100 : UserValue
Else If (UserValue != "" and UserValue != "-")
UserValue := "0"

GuiControl, -g, UserValue		;remove gLabel from "UserValue" control in order to prevent infinite loop
GuiControl, , UserValue, %UserValue%
Send, {End}				;move the caret\cursor to the end of the Edit control
GuiControl, +gUpDown, UserValue		;Add gLabel back again to "UserValue" control

hh++				;just for test
ToolTip, % "loop test " hh	;If "GuiControl, -g, UserValue" above line was not in use, this ToolTip would go into an infinite loop

Return

GuiClose:	;_________________ Gui close _________________
ExitApp

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 161 guests