dotnet.addEventHandler not works in Structs in 3ds Max 2019.


#1

In 3ds Max 2019 dotnet.addEventHandler not works when it is inside Struct.
Here is my test code:


(
    struct miauuRCMenu
    (
        function GrpNameColor s e = 
        (
            print "GrpNameColor"
            
            local bgcolor = ((dotNetClass "System.Drawing.Color").FromArgb 86 86 86)

            bb = dotnetobject "System.Drawing.SolidBrush" (s.Backcolor = bgcolor)
            fb = dotnetobject "System.Drawing.SolidBrush" s.Forecolor.white

            dnFontStyle = dotNetClass "System.Drawing.FontStyle"
            myFontStyle = dotnet.combineEnums dnFontStyle.regular
            font = dotNetObject "System.Drawing.Font" "Arial" 9 myFontStyle
            
            r = e.ClipRectangle
            e.Graphics.FillRectangle bb r
            sf = dotnetobject "System.Drawing.StringFormat"
            sf.LineAlignment = (dotnetclass "System.Drawing.StringAlignment").Center
            rect = dotnetobject "System.Drawing.RectangleF" (r.x+23) r.y (r.width-34) r.height
            e.Graphics.DrawString s.Text font fb rect sf
        ),
        
        function RCEntry_01 sender args =
        (
            print "RCEntry_01"
        ),
        
        function RCEntry_02 sender args =
        (
            print "RCEntry_02"
        ),
        
        function RCEntry_03 sender args =
        (
            print "RCEntry_03"
        ),
            
        function Closed e =
        (
            print "Closed"
        ),    
            
        dotNetRCSeparator01 = (dotNetObject "System.Windows.Forms.ToolStripSeparator"),        
            
        function DotNetRCMenu =
        (
            print "DotNetRCMenu"
            
            rcMenuPos = mouse.screenpos 
            
            contextMenu = dotNetObject "System.Windows.Forms.ContextMenuStrip"            
            
            contextMenu.ForeColor = (dotNetClass "System.Drawing.Color").white
            contextMenu.BackColor = (dotNetClass "System.Drawing.Color").gray            
            
            dotnet.addEventHandler contextMenu "Closed" Closed
            
            cmGrpName01 = contextMenu.Items.Add "NEW GROUP 01"
            dotnet.addEventHandler cmGrpName01 "Paint" GrpNameColor            
            
            cmRCEntry_01 = contextMenu.Items.Add "RCEntry_01"
            dotnet.addEventHandler cmRCEntry_01 "Click" RCEntry_01
            
            cmRCEntry_02 = contextMenu.Items.Add "RCEntry_02"
            dotnet.addEventHandler cmRCEntry_02 "Click" RCEntry_02
            
            contextMenu.Items.Add dotNetRCSeparator01
            
            cmGrpName02 = contextMenu.Items.Add "NEW GROUP 02"
            dotnet.addEventHandler cmGrpName02 "Paint" GrpNameColor    
            
            cmRCEntry_03 = contextMenu.Items.Add "RCEntry_03"
            dotnet.addEventHandler cmRCEntry_03 "Click" RCEntry_03    
            
            contextMenu.Show rcMenuPos[1] rcMenuPos[2]
        )        
    )
    global miauuStrRCMenu = miauuRCMenu()
    miauuStrRCMenu.DotNetRCMenu()
)

When I run it in 3ds Max 2014 the RC menu looks this way:

When I run it in 3ds Max 2019 it looks this way:

Click on RCEntry_01, _02, _03 must print text in the MaxScript Listener, but this not happens in 3ds max 2019, althou it works properly in 3ds Max 2014.

I have found this thread and the code from denisT also not works in 3ds Max 2019, but works in previous versions of 3ds Max.


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


#2

I reported the problem to Autodesk, no easy work-around I’m afraid! Might have to wait until 2019.1


#3

Thank you.
My work-around is to move all code from the struct outside the sctruct, which is not the best solution but is the only one that works.


#4

Does it work in other versions of max? I am still using 2015 for the moment and it doesn’t work either.

The workaround in my case was to move the event handler inside the function that calls addEventHandler rather than outside the struct.
Far from ideal but it was good enough for my particular case.


#5

Works fine for me in 2014 and 2016. Can’t test other versions.


#6

It works for all versions from 2009 to 2018. Not works in 2019.

If you solution is like this:


(
    struct miauuRCMenu
    (
        dotNetRCSeparator01 = (dotNetObject "System.Windows.Forms.ToolStripSeparator"),

        function DotNetRCMenu =
        (
            print "DotNetRCMenu"
            
            function GrpNameColor s e =
            (
                print "GrpNameColor"

                local bgcolor = ((dotNetClass "System.Drawing.Color").FromArgb 86 86 86)

                bb = dotnetobject "System.Drawing.SolidBrush" (s.Backcolor = bgcolor)
                fb = dotnetobject "System.Drawing.SolidBrush" s.Forecolor.white

                dnFontStyle = dotNetClass "System.Drawing.FontStyle"
                myFontStyle = dotnet.combineEnums dnFontStyle.regular
                font = dotNetObject "System.Drawing.Font" "Arial" 9 myFontStyle

                r = e.ClipRectangle
                e.Graphics.FillRectangle bb r
                sf = dotnetobject "System.Drawing.StringFormat"
                sf.LineAlignment = (dotnetclass "System.Drawing.StringAlignment").Center
                rect = dotnetobject "System.Drawing.RectangleF" (r.x+23) r.y (r.width-34) r.height
                e.Graphics.DrawString s.Text font fb rect sf
            )

            function RCEntry_01 sender args =
            (
                print "RCEntry_01"
            )

            function RCEntry_02 sender args =
            (
                print "RCEntry_02"
            )

            function RCEntry_03 sender args =
            (
                print "RCEntry_03"
            )

            function Closed e =
            (
                print "Closed"
            )

            rcMenuPos = mouse.screenpos

            contextMenu = dotNetObject "System.Windows.Forms.ContextMenuStrip"

            contextMenu.ForeColor = (dotNetClass "System.Drawing.Color").white
            contextMenu.BackColor = (dotNetClass "System.Drawing.Color").gray

            dotnet.addEventHandler contextMenu "Closed" Closed

            cmGrpName01 = contextMenu.Items.Add "NEW GROUP 01"
            dotnet.addEventHandler cmGrpName01 "Paint" GrpNameColor

            cmRCEntry_01 = contextMenu.Items.Add "RCEntry_01"
            dotnet.addEventHandler cmRCEntry_01 "Click" RCEntry_01

            cmRCEntry_02 = contextMenu.Items.Add "RCEntry_02"
            dotnet.addEventHandler cmRCEntry_02 "Click" RCEntry_02

            contextMenu.Items.Add dotNetRCSeparator01

            cmGrpName02 = contextMenu.Items.Add "NEW GROUP 02"
            dotnet.addEventHandler cmGrpName02 "Paint" GrpNameColor

            cmRCEntry_03 = contextMenu.Items.Add "RCEntry_03"
            dotnet.addEventHandler cmRCEntry_03 "Click" RCEntry_03

            contextMenu.Show rcMenuPos[1] rcMenuPos[2]
        )
    )
    global miauuStrRCMenu = miauuRCMenu()
    miauuStrRCMenu.DotNetRCMenu()
)

it not works in 3ds Max 2019, but works in previous versions.


#7

I’m still interested if someone as a workaround
–> OK 3dsmax 2019.1update is a solution
https://forums.autodesk.com/t5/3ds-max-forum/3ds-max-2019-update-1/td-p/8066548


#8

I don’t have a workaround, but installing 2019.1 fixes the issue.