Phone: 905 409-1589
Email: info@penproductions.ca
RSS LinkedIn Twitter Twitter
Dotnet NumericUpDown
Navigate:
In this tutorial I'm going to show how to create a dot net numericUpDown, or spinner in Max terms, that will act as the Max spinners do with the ability to click and drag on the up down buttons to change the value and be able to right click on the spinner to reset the value to 0.
Creating Control:
Code:
form=dotNetObject "form"

nud=dotNetObject "System.Windows.Forms.NumericUpDown"
nud.DecimalPlaces=2
nud.Increment=.1
nud.maximum=10
nud.minimum=-10
nud.ReadOnly=false
enableAccelerators=false
showProperties nud

form.controls.add nud
form.show()
						
Controling Spinners:
Code:
--Create a dotNet form control.
form=dotNetObject "form"
form.bounds=(dotNetObject "system.drawing.rectangle" 10 10 150 100)

--Create a dotNet NumericUpDown control.
nud=dotNetObject "System.Windows.Forms.NumericUpDown"
nud.location=(dotNetObject "system.drawing.point" 10 10)
nud.DecimalPlaces=2
nud.Increment=.1
nud.maximum=100
nud.minimum=-10
nud.value=10
nud.ReadOnly=false

--This is needed so that we can type in the values as well as use the spinner. 
enableAccelerators=false

--Set variables to hold the mouse position and current value of the spinner. 
lastMousePos=0
curVal=0

--Mouse down function to set the variables above to the starting values. 
fn nudMouseDown sender arg=
(
	case arg.button of
	(
		--Check for the left mouse button
		(arg.button.left):
		(
			lastMousePos=(arg.location).y
			curVal=sender.value
		)
		--Check for the right mouse button
		(arg.button.right):
		(
			sender.value=0
		)
	)
)

--On mouseMove event
fn nudMouseMove sender arg=
(
	case arg.button of
	(
		--Check for the left mouse button. 
		(arg.button.left):
		(
			--Get the offset from the last position of the mouse
			mouseOffSetY=(lastMousePos-arg.location.y)*.1
			--Add that position to the curVal variable
			curVal=curVal+=mouseOffSetY
			
			--Check if the curVal is within limits of the spinner.
			if curVal>=sender.minimum and curVal<=sender.maximum do
			(
				--Set the value of the spinner
				sender.value=curVal
			)
			--Check if the value has gone above or below the min max values and set them.
				--This is needed if the value changes more rapidly then the spinner can react.
 			if curVal<=sender.minimum do sender.value=sender.minimum
 			if curVal>=sender.maximum do sender.value=sender.maximum
			
			--Set the lastMousePos value to the current value of the mouse. 
			lastMousePos=arg.location.y
		)
	)
)

--Create the event handlers
dotNet.addEventHandler nud "mouseDown" nudMouseDown
dotNet.addEventHandler nud "mouseMove" nudMouseMove
--Set the life time control of the spinner to be handled by dot net and not Max script. 
	--This stops garbage collections from deleting the event handlers. 
dotNet.setLifeTimeControl nud #dotNet

--Add the NumericUpDown to the form and show the form. 
form.controls.add nud
form.show()