Notifications
Clear all

[Closed] Using DotNet.AddEventHandler(): Is it possible to add extra handler to a control?

Is it possible to add additional event handlers to a DotNetControl?

To do this, I tried to use dotNet.addEventHandler listviewReference “eventNameAsString” functionToBeCalled, but I got the error “Unable to convert: dotNetControl: listviewReference: System.Windows.Forms.ListView to type: dotNetObject”.

I think it’s basically saying that I’m trying to add an event to DotNetControl, but what it needs is a DotNetObject.

Is there a different way addEventHandler() should be used? Or some other way to add an event callback to a DotNet control?

Thanks!

3 Replies

DotNetControls are wrapped dotNet controls made to behave exactly like all default UI controls (button, spinner etc). So they also utilize the same event system as the regular maxscript controls:

(
	rollout DotNetTest "DotNet Test" width:80 height:40
	(
		dotNetControl DotNetButton "System.Windows.Forms.Button" width:60 height:30
	
		on DotNetTest open do
		(
			DotNetButton.Text = "Click Me!"
		)
		
		on DotNetButton Click do
		(
			print "Click!"
		)
	)
	
	createDialog DotNetTest
)

Instead of placing DotNetControls on a rollout, you can also build a DotNet Form yourself. In this case you need the addEventHandler method to catch the events of the controls (DotNetObjects):

(
	-- Create a form
	local DotNetForm = DotNetObject "System.Windows.Forms.Form"
	DotNetForm.Size = (DotNetObject "Drawing.Size" 200 80)
	
	-- Create a button
	local DotNetButton = DotNetObject "System.Windows.Forms.Button"
	DotNetButton.Text = "Click Me!"
	DotNetButton.Location = (DotNetObject "Drawing.Point" 10 10)
	
	-- Create another button
	local DotNetButton2 = DotNetObject "System.Windows.Forms.Button"
	DotNetButton2.Text = "Or Me!"
	DotNetButton2.Location = (DotNetObject "Drawing.Point" 100 10)

	-- The "Click" event calls this function (see below)
	fn OnButtonClick s e =
	(
		format "Click!
	Sender     : %
	Mouse Pos  : %
" s.Text [e.X, e.Y]
	)
	
	-- Setup an event handler for both buttons
	dotNet.addEventHandler DotNetButton "Click" OnButtonClick
	dotNet.addEventHandler DotNetButton2 "Click" OnButtonClick
	
	-- Add the buttons to the form
	DotNetForm.Controls.AddRange #(DotNetButton, DotNetButton2)
	
	-- Show the form
	DotNetForm.Show()
)

Cheers,
Martijn

Thanks, Martijn

So the take-home message I got from your post is that as long as I’m embedding a DotNet control in a maxscript rollout, I’m limited to just the event callbacks that are exposed through maxscript. However, if I was willing to build the whole form out of DotNet, then I could add as many additional callbacks as I wanted?

It’s not practical for me to go completely DotNet at this point, but I can it as a possibility in the future as I get more comfortable with more of the available controls.

Thanks!

1 Reply
(@magicm)
Joined: 2 years ago

Posts: 0

Yes that’s pretty much it

Cheers,
Martijn