# Error when using Windows Form Events with Python in Dynamo for Revit 2024

**URL:** <https://forum.dynamobim.com/t/error-when-using-windows-form-events-with-python-in-dynamo-for-revit-2024/96181>\
**Category:** Developers\
**Tags:** python\
**Created:** [December 7, 2023, 7:57pm UTC](https://forum.dynamobim.com/t/error-when-using-windows-form-events-with-python-in-dynamo-for-revit-2024/96181 "2023-12-07T19:57:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcus\_hev](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@marcus\_hev](https://forum.dynamobim.com/u/marcus_hev)\
**Post date:** [December 7, 2023, 7:57pm UTC](https://forum.dynamobim.com/t/error-when-using-windows-form-events-with-python-in-dynamo-for-revit-2024/96181/1 "2023-12-07T19:57:33Z")

</div>

Hi,  
I have created a couple of scripts where I use a Python node to display a Windows Form for a better user experience.

This has worked fine for Revit 2023 and earlier. However, when trying to run these scripts in Revit 2024, I get an error message saying:  
“ **System.Reflection.Emit.TypeBuilder not found**.” See the attached picture.

Through a lot of testing, I found out that it is different events like .Click etc., that cause the error.  
I have made a simple example with a form and a clickable label. See the code below.

If I remove the line:

self.label.Click += self.label\_clicked,

the form is displayed as expected.  
I tried to change .Click to other events but got the same error.

I also tried to change from a Windows Form to a WPF Form but got the same error for events then as well.

I guess this has something to do with the upgrade from .NET 4.8 in previous Dynamo versions to 6.0 in Dynamo for Revit 2024, but I am not skilled enough to figure out what the problem is and how to solve it.

Is there anyone here who knows how to solve this?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/e/3e1ac712c781f331ccca0c11f180ccc1f49cca73.png)  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/f/efd80954fd783a0359abfec4e362a9f665055577.png)

Code example:

```auto

import clr

clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

from System.Windows.Forms import Form, Label
from System.Drawing import Point

class SimpleForm(Form):
    def __init__ (self):
        self.label = Label()
        self.label.Text = "Label is not clicked"
        self.label.Location = Point(50, 50)
        self.label.Click += self.label_clicked # Click Event that cause error
        self.Controls.Add(self.label)

    def label_clicked(self, sender, args):
        self.label.Text = "Label is clicked"

form = SimpleForm()
form.Show()

```

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [December 7, 2023, 9:08pm UTC](https://forum.dynamobim.com/t/error-when-using-windows-form-events-with-python-in-dynamo-for-revit-2024/96181/2 "2023-12-07T21:08:35Z")

</div>

Hi,

for info, the issue has been reported

> <https://github.com/DynamoDS/Dynamo/issues/14612>
>
> \### Dynamo Version
> 
> Dynamo Core 2.19.3.6394
> Dynamo for Revit 2.19.3.10292
> (Rev…it 2024.2)
> 
> \### Host
> 
> \_No response\_
> 
> \### Operating System
> 
> Windows 11
> 
> \### What did you do?
> 
> Trying to create an event inside a Python node:
> 
> \`\`\`python
> from System import EventHandler, EventArgs
> 
> def my\_handler(obj, e):
> pass
> 
> handler = EventHandler\[EventArgs\](my\_handler)
> \`\`\`
> 
> \### What did you expect to see?
> 
> No errors, this worked in Dynamo 2.16 in Revit 2023.1 but not in the 2.19 build shipped with Revit 2024.2.
> 
> \### What did you see instead?
> 
> The following error:
> 
> PythonEvaluator.Evaluate operation failed. Constructor on type 'System.Reflection.Emit.TypeBuilder' not found.
> 
> \### What packages or external references (if any) were used?
> 
> \_No response\_
> 
> \### Stack Trace
> 
> \_No response\_
> 
> \### Details
> 
> The same error occurs when trying to add the handler to events with the += operation.

> <https://github.com/DynamoDS/DynamoRevit/issues/3008>
>
> \### Dynamo Version
> 
> Dynamo 2.19.3
> 
> \### Revit Version
> 
> Revit 2024.2
> 
> \### …Operating System
> 
> Windows 11
> 
> \### What did you do?
> 
> try to use events subscription with CPython3 
> 
> \### What did you expect to see?
> 
> No Error
> 
> \### What did you see instead?
> 
> a Python error
> \`\`\`
> Node Name: PythonScript
> Package: Core.Scripting
> Dynamo Version: 2.19.3.6394
> Host: Dynamo Revit
> Messages: PythonEvaluator.Evaluate operation failed.
> The constructor on type 'System.Reflection.Emit.TypeBuilder' was not found.
> Status: Warning
> \`\`\`
> !\[image\](https://github.com/DynamoDS/DynamoRevit/assets/53660624/1878fac8-906f-4e85-b521-516bdb669dd3)
> 
> Example 1
> 
> \`\`\`
> import clr
> import System
> from System import EventHandler
> 
> clr.AddReference("System.Drawing")
> clr.AddReference("System.Windows.Forms")
> import System.Drawing
> import System.Windows.Forms
> 
> from System.Drawing import \*
> from System.Windows.Forms import \*
> 
> class Form26(Form):
> def \_\_init\_\_(self):
> self.InitializeComponent()
> 	
> def InitializeComponent(self):
> self.\_button1 = System.Windows.Forms.Button()
> self.SuspendLayout()
> # 
> self.\_button1.Location = System.Drawing.Point(73, 103)
> self.\_button1.Name = "button1"
> self.\_button1.Size = System.Drawing.Size(142, 43)
> self.\_button1.TabIndex = 0
> self.\_button1.Text = "button1"
> self.\_button1.UseVisualStyleBackColor = True
> self.\_button1.Click += self.Button1Click
> # 
> self.ClientSize = System.Drawing.Size(284, 261)
> self.Controls.Add(self.\_button1)
> self.Name = "Form26"
> self.Text = "Form26"
> self.ResumeLayout(False)
> 
> def Button1Click(self, sender, e):
> pass
>   
> objForm = Form26()
> objForm.ShowDialog()
> \`\`\`
> 
> Example 2
> 
> \`\`\`
> import clr
> import System
> from System import EventHandler
> 
> clr.AddReference("RevitServices")
> import RevitServices
> from RevitServices.Persistence import DocumentManager
> from RevitServices.Transactions import TransactionManager
> 
> \# Import RevitAPI
> clr.AddReference("RevitAPI")
> import Autodesk
> from Autodesk.Revit.DB import \*
> 
> clr.AddReference("RevitAPIUI")
> from Autodesk.Revit.UI import \*
> from Autodesk.Revit.UI.Events import \*
> 
> doc = DocumentManager.Instance.CurrentDBDocument
> uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
> uiapp = uidoc.Application
> 
> def dismiss (sender, eventArgs):
> if isinstance(eventArgs, TaskDialogShowingEventArgs):
> eventArgs.OverrideResult(TaskDialogResult.No)
> return
> 
> dialogEventHandler = EventHandler\[DialogBoxShowingEventArgs\](dismiss)
> 
> uiapp.DialogBoxShowing += dialogEventHandler
> \`\`\`
> 
> \### Stacktrace/logs
> 
> \_No response\_
> 
> \### Details
> 
> the error seems related to the build/compilation of PythonNet 2.5.2 (Python.Runtime) with Net.Standard 2.0 as target
> 
> there are potentially others PythonNet 2.5.2 issues due to this change (Net Framework -\> Net.Standard 2.0), 
> \_will probably be resolved in PythonNet 3.x.x\_

---

<div class="post-metadata">

**Author:** ![marcus\_hev](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@marcus\_hev](https://forum.dynamobim.com/u/marcus_hev)\
**Post date:** [December 8, 2023, 8:28am UTC](https://forum.dynamobim.com/t/error-when-using-windows-form-events-with-python-in-dynamo-for-revit-2024/96181/3 "2023-12-08T08:28:37Z")

</div>

Thank you @c.poupin for pointing that out !

Then i have to wait for the bugg to be solved.

---

<div class="post-metadata">

**Author:** ![egdivad](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@egdivad](https://forum.dynamobim.com/u/egdivad)\
**Post date:** [April 24, 2024, 4:09am UTC](https://forum.dynamobim.com/t/error-when-using-windows-form-events-with-python-in-dynamo-for-revit-2024/96181/4 "2024-04-24T04:09:02Z")

</div>

Hi, do we have an update on this bug? My dyn script work in Revit 2023 but fails in 2024.

Thank you

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [April 24, 2024, 6:55am UTC](https://forum.dynamobim.com/t/error-when-using-windows-form-events-with-python-in-dynamo-for-revit-2024/96181/5 "2024-04-24T06:55:02Z")

</div>

@egdivad

the Dynamo team is starting to work on the integration of PythonNet 3 (which should solve several problems, including this one)

I can suggest you to use C# or IronPython3 in the meantime

---

<div class="post-metadata">

**Author:** ![NIRMAL](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nirmal/32/150135_2.png) [@NIRMAL](https://forum.dynamobim.com/u/NIRMAL)\
**Post date:** [August 21, 2024, 6:51am UTC](https://forum.dynamobim.com/t/error-when-using-windows-form-events-with-python-in-dynamo-for-revit-2024/96181/6 "2024-08-21T06:51:31Z")

</div>

Hi @c.poupin

Is that issue fixed?  
I tried searching for it on github but didn’t find a proper answer while everywhere it was mentioned that it would be fixed by June and July.

Evenet Handlers are working for me in Revit 2024.1 but not in 2024.2

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [August 21, 2024, 9:15am UTC](https://forum.dynamobim.com/t/error-when-using-windows-form-events-with-python-in-dynamo-for-revit-2024/96181/7 "2024-08-21T09:15:29Z")

</div>

Hi @NIRMAL

To Date, It’s fixed only in Revit 2025.2, you can use IronPython instead
