Notifications
Clear all

[Closed] custAttr launching a dotNet form

I’m trying to make a dotnet form launch from a custom attribute, and running into some weird issues.
When I use the maxscript dotNet.setLifetimeControl() function on the buttons I’m creating, it’s giving me an error:

>> MAXScript Rollout Handler Exception: -- Unable to convert: dotNetObject:System.Windows.Forms.Label to type: System.Windows.Forms.Control <<

When you inspect the controls held in variables, classof() returns the correct class, and comparing it with a control that DOES work returns true (they’re the same class), but one can be added to the form’s control list and the other can’t. The ones that can’t also show as having no properties. Are they somehow being converted to pointers or something?

I get different and inconsistent results with the script below when I comment/uncomment the setLifetimeControl() and addEventHandler() calls in the rkMakeButton function and the rkMakeForm function. If it seems to work (or at least launch) the first time you try it, have no fear, click it again and it may not.


 struct rkUIstruct (window,buttons)
 fn rkui_btnEvent a b =
 (
 	format "clicked button %
" a.tag
 )
 fn rkMakeButton t i =
 (
 	local lbl = (dotNetObject "System.Windows.Forms.Label")
 	lbl.text = t
 	lbl.tag = i
 	lbl.location = (dotNetObject "System.Drawing.Point" (i*20) 10)
 	lbl.foreColor = (dotNetClass "System.Drawing.Color").FromARGB 255 255 255 255
 	lbl.backColor = (dotNetClass "System.Drawing.Color").FromARGB 64 32 32 32
 	local lfont = (dotNetObject "System.Drawing.Font" (dotNetObject "System.Drawing.FontFamily" "Microsoft Sans Serif") 7 (dotNetClass "System.Drawing.FontStyle").regular )
 	lbl.font = lfont
 	lbl.flatStyle = lbl.flatStyle.flat
 	lbl.maximumSize = (dotNetObject "System.Drawing.Size" 10 16)
 	lbl.padding = lbl.margin = (dotNetObject "system.windows.forms.padding" 0)
 	dotNet.setLifetimeControl lbl #dotnet
 	dotNet.addEventHandler lbl "click" ::rkui_btnEvent 
 	lbl
 )
 fn rkMakeForm = 
 (
 	try (rkUI.window.close()) catch()
 	global rkUI = rkUIstruct()
 	rkUI.buttons = #()
 	rkUI.window = dotnetobject "MaxCustomControls.Maxform"
 	rkUI.window.backColor = (dotNetClass "System.Drawing.Color").FromARGB 255 125 125 125
 	rkUI.window.MinimumSize = dotnetobject "System.Drawing.Size" 200 100
 	rkUI.window.MaximumSize = dotnetobject "System.Drawing.Size" 200 100
 	for i = 1 to 5 do (
 		local v = (rkMakeButton (i as string) i)
 		--dotNet.setLifetimeControl v #dotnet
 		--dotNet.addEventHandler v "click" ::rkui_btnEvent 
 		append rkUI.buttons v
 	)
 	rkUI.window.controls.addRange rkUI.buttons
 	rkUI.window.showmodeless()
 )
 
 if (rkLauncherCA!=undefined) then (
 	try (custAttributes.deleteDef rkLauncherCA) catch()
 )
 global rkLauncherCA = attributes rkLaunch
 (
 	parameters main rollout:rklro
 	(
 		sound type:#string 
 	)
 	rollout rklro "launch"
 	(
 		button launchBut "open window"
 		on launchBut pressed do (
 			rkMakeForm()
 		)
 	)
 )
 
 if ($launchHlpr!=undefined) then delete $launchHlpr
 p = point name:"launchHlpr" pos:[0,0,0] box:true wirecolor:(color 255 0 0)
 custAttributes.Add p rkLauncherCA
 

I’m using Max 2010 x64 btw.