dotnet.addEventHandler and structs

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
  12 December 2011
dotnet.addEventHandler and structs

Trying to access a struct's member by way of a dotnet button using dotnet.addEventHandler, but I'm running into an exception trying to access it's value:

--Runtime Error: Struct member access requires instance: storedNodes

Here's my test code:

clearListener()
try (destroydialog testRollout ) catch()
   
rollout testRollout "testRollout" width:664 height:60
(		   
	struct charData 
	(
		b = dotnetobject "button",		
		storedNodes,
	   		
		fn printAry sender arg =
		(
			print storedNodes
		),
				
		on create do
		(		
			dotnet.addEventHandler b "mouseDown" printAry
		)
	)
	
   	fn AddFlowLayoutNodes control =
   	(		
   		for i = 1 to 6 do
   		(
			p = dotnetobject "flowlayoutpanel"
			p.size = (dotNetObject "System.Drawing.Size" 100 40)
			p.backcolor = (dotnetclass "system.drawing.color").DarkGray
			control.Controls.add p
			
			newLimb = charData storedNodes:i
			print newLimb.storedNodes
			newLimb.b.text = ("Button: " + (i as string))
			
			p.controls.add newLimb.b
			
			if i == 6 do (return p)
   		)	
   	)	
   	
   	dotNetControl flp "system.windows.forms.flowlayoutpanel" pos:[2,4] width:654 height:50
   
   	on testRollout open do
   	(
		flp.borderstyle=  (dotNetClass "system.windows.forms.borderstyle").fixedsingle	
		flp.autoscroll = true
		
		flp.SuspendLayout()		
		flp.setflowbreak (AddFlowLayoutNodes flp) true			
		flp.resumelayout()
	)
)

createdialog testRollout


I did find this thread, but I still don't understand what's going on. Any help would be greatly appreciated, thank you.
 
  12 December 2011
If you're using a dotnetcontrol in a maxscript rollout it might save a lot of trouble to use the "standard" maxscript way of catching events. E.g. :
on b MouseDown s e do .....

You can catch any .net control event this way.

Last edited by Pjanssen : 12 December 2011 at 11:47 PM.
 
  12 December 2011
I've done that before for other things, but in this case I'm trying to build a UI within a struct, including a listview and buttons to add and delete objects out of it.

You can't create a dotNetControl inside of a struct, correct? The code I posted seems like it should work.
 
  12 December 2011
Aha sorry I missed that struct. I think this kind of construct is a little odd...having a struct inside a rollout. Why would you want to do that?
 
  12 December 2011
I'm using a struct to build UI elements, a listview and some buttons, and then dynamically add them all to a flowlayoutpanel.

I'm open to suggestions if anyone has a better way.
 
  12 December 2011
Originally Posted by e-x: I'm using a struct to build UI elements, a listview and some buttons, and then dynamically add them all to a flowlayoutpanel.

I'm open to suggestions if anyone has a better way.

Just quick guess but looks like a variable scope issue.
You made struct instance inside AddFlowLayoutNodes()
but is that instance is visible outside the function?
__________________
* everyone can win previous war
 
  12 December 2011
Originally Posted by e-x: Trying to access a struct's member by way of a dotnet button using dotnet.addEventHandler, but I'm running into an exception trying to access it's value:

--Runtime Error: Struct member access requires instance: storedNodes

Here's my test code:

clearListener()
 try (destroydialog testRollout ) catch()
    
 rollout testRollout "testRollout" width:664 height:60
 (		   
 	struct charData 
 	(
 		b = dotnetobject "button",		
 		storedNodes,
 	   		
 		fn printAry sender arg =
 		(
 			print storedNodes
 		),
 				
 		on create do
 		(		
 			dotnet.addEventHandler b "mouseDown" printAry
 		)
 	)
 	
    	fn AddFlowLayoutNodes control =
    	(		
    		for i = 1 to 6 do
    		(
 			p = dotnetobject "flowlayoutpanel"
 			p.size = (dotNetObject "System.Drawing.Size" 100 40)
 			p.backcolor = (dotnetclass "system.drawing.color").DarkGray
 			control.Controls.add p
 			
 			newLimb = charData storedNodes:i
 			print newLimb.storedNodes
 			newLimb.b.text = ("Button: " + (i as string))
 			
 			p.controls.add newLimb.b
 			
 			if i == 6 do (return p)
    		)	
    	)	
    	
    	dotNetControl flp "system.windows.forms.flowlayoutpanel" pos:[2,4] width:654 height:50
    
    	on testRollout open do
    	(
 		flp.borderstyle=  (dotNetClass "system.windows.forms.borderstyle").fixedsingle	
 		flp.autoscroll = true
 		
 		flp.SuspendLayout()		
 		flp.setflowbreak (AddFlowLayoutNodes flp) true			
 		flp.resumelayout()
 	)
 )
 
 createdialog testRollout


I did find this thread, but I still don't understand what's going on. Any help would be greatly appreciated, thank you.


you code is almost correct, i just made a little modification to make it work:

try (destroydialog testRollout ) catch()
   
rollout testRollout "testRollout" width:664 height:60
(		   
	struct charData 
	(
		b = dotnetobject "button",		
		storedNodes,
			   
		fn printAry sender arg =
		(
			print sender.tag.value.storedNodes
		),
				
		on create do
		(		
			b.tag = dotnetmxsvalue this
			dotnet.addEventHandler b "mouseDown" printAry
		)
	)
	
	   fn AddFlowLayoutNodes control =
	   (		
		   for i = 1 to 6 do
		   (
			p = dotnetobject "flowlayoutpanel"
			p.size = dotnetobject "System.Drawing.Size" 100 40
			p.backcolor = p.backcolor.DarkGray
			control.Controls.add p
			
			newLimb = charData storedNodes:i
			print newLimb.storedNodes
			newLimb.b.text = ("Button: " + (i as string))
			
			p.controls.add newLimb.b
			
			if i == 6 do (return p)
		   )	
	   )	
	   
	   dotNetControl flp "flowlayoutpanel" pos:[2,4] width:654 height:50
   
	   on testRollout open do
	   (
		flp.borderstyle =  flp.borderstyle.fixedsingle	
		flp.autoscroll = true
		
		flp.SuspendLayout()		
		flp.setflowbreak (AddFlowLayoutNodes flp) true			
		flp.resumelayout()
	)
)

createdialog testRollout


the event function defined in structure before the structure created. so it knows nothing about the members. using button's tag i pass the instance of the structure to the event function...
 
  12 December 2011
here is a little more elegant way how to define the structure above:

	struct charData 
	(
		storedNodes,
		b = 
		(
			local printAry
			fn printAry sender arg =
			(
				print sender.tag.value.storedNodes
			)
			b = dotnetobject "Button"
			dotnet.addEventHandler b "MouseDown" printAry
			b
		),		
		on create do
		(		
			b.tag = dotnetmxsvalue this
		)
	)
 
  12 December 2011
Originally Posted by denisT: here is a little more elegant way how to define the structure above:

 	struct charData 
 	(
 		storedNodes,
 		b = 
 		(
 			local printAry
 			fn printAry sender arg =
 			(
 				print sender.tag.value.storedNodes
 			)
 			b = dotnetobject "Button"
 			dotnet.addEventHandler b "MouseDown" printAry
 			b
 		),		
 		on create do
 		(		
 			b.tag = dotnetmxsvalue this
 		)
 	)
 
 


Hi Denis,

Why do you forward declare the printAry function name?

J.
 
  12 December 2011
Originally Posted by JasonB: Why do you forward declare the printAry function name?

it's one of my finds how to trick the script compiler... try to evaluate the same code without the declaration.
 
  12 December 2011
Works like a charm, thanks Denis.
 
  12 December 2011
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright ©2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 02:45 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.