Pyrevit Not sending the Data to Area Parameter

I am Trying to Sent the Selected Data from the Dropdown combobox Area01_selector or Modeless.XAML to Area Comment Parameter but when Clicking the Button it closes the Window

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Metadata
""
from pyrevit import script #
# from pyrevit import revit #

# Import standard libraries
import sys
import traceback

# Import required libraries
from Autodesk.Revit.UI import IExternalEventHandler, ExternalEvent
from Autodesk.Revit.Exceptions import InvalidOperationException
from pyrevit.forms import WPFWindow
from pyrevit import script
from Autodesk.Revit import DB, UI

# Add custom modules path



# Access the active document
uidoc = __revit__.ActiveUIDocument
doc = uidoc.Document

# Define Area names
Area01_Names = ["Market Residential", "Market Rental", "Affordable Rental", "Social Housing", "Hotel", "Office", "Commercial"]

MarketResidential_Names = ["Unit", "Common", "Services"]
MarketResidential_Unit_Names = ["Jr.Studio", "Studio", "Jr.1 Bedroom","1 Bedroom","1 Bedroom & Den","Jr. 2 Bedroom","2 Bedroom","2 Bedroom & Den","3 Bedroom","3 Bedroom & Den","Townhouse","Exclusion"]
MarketResidential_Unit_JrStudio_Names = ["M Unit - A1a", "M Unit - A2a", "M Unit - A3a","M Unit - A4a","M Unit - A5a"]
MarketResidential_Unit_Studio_Names = ["M Unit - A1a", "M Unit - A2a", "M Unit - A3a","M Unit - A4a","M Unit - A5a"]
MarketResidential_Unit_Jr1Bedroom_Names = ["M Unit - B1a", "M Unit - B2a", "M Unit - B3a","M Unit - B4a","M Unit - B5a"]
MarketResidential_Unit_1Bedroom_Names = ["M Unit - B1a", "M Unit - B2a", "M Unit - B3a","M Unit - B4a","M Unit - B5a"]
MarketResidential_Unit_1BedroomDen_Names = ["M Unit - B1a", "M Unit - B2a", "M Unit - B3a","M Unit - B4a","M Unit - B5a"]
MarketResidential_Unit_Jr2Bedroom_Names = ["M Unit - C1a", "M Unit - C2a", "M Unit - C3a","M Unit - C4a","M Unit - C5a"]
MarketResidential_Unit_2Bedroom_Names = ["M Unit - C1a", "M Unit - C2a", "M Unit - C3a","M Unit - C4a","M Unit - C5a"]
MarketResidential_Unit_2BedroomDen_Names = ["M Unit - C1a", "M Unit - C2a", "M Unit - C3a","M Unit - C4a","M Unit - C5a"]
MarketResidential_Unit_3Bedroom_Names = ["M Unit - D1a", "M Unit - D2a", "M Unit - D3a","M Unit - D4a","M Unit - D5a"]
MarketResidential_Unit_3BedroomDen_Names = ["M Unit - D1a", "M Unit - D2a", "M Unit - A3a","M Unit - A4a","M Unit - A5a"]
MarketResidential_Unit_Townhouse_Names = ["M Unit - E1a", "M Unit - E2a", "M Unit - E3a","M Unit - E4a","M Unit - E5a"]
MarketResidential_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby","Amenity"]
MarketResidential_Common_Core_Names = ["M Core-1", "M Core-2", "M Core-3","M Core-4","M Core-5"]
MarketResidential_Common_Corridor_Names = ["Cr-1", "Cr-2", "Cr-3","Cr-4","Cr-5"]
MarketResidential_Common_ExistStair_Names = ["Stair – 1", "Stair – 2", "Stair – 3","Stair – 4","Stair – 5"]
MarketResidential_Common_Lobby_Names = ["M Lobby – 1", "M Lobby – 2", "M Lobby – 3","M Lobby – 4","M Lobby – 5"]
MarketResidential_Common_Amenity_Names = ["M Amenity – 1", "M Amenity – 2", "M Amenity – 3","M Amenity – 4","M Amenity – 5"]
MarketResidential_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]

MarketRental_Names = ["Unit", "Common", "Services"]
MarketRental_Unit_Names = ["Jr.Studio", "Studio", "Jr.1 Bedroom","1 Bedroom","1 Bedroom & Den","Jr. 2 Bedroom","2 Bedroom","2 Bedroom & Den","3 Bedroom","3 Bedroom & Den","Townhouse","Exclusion"]
MarketRental_Unit_JrStudio_Names = ["MR Unit - A1a", "MR Unit - A2a", "MR Unit - A3a","MR Unit - A4a","MR Unit - A5a"]
MarketRental_Unit_Studio_Names = ["MR Unit - A1a", "MR Unit - A2a", "MR Unit - A3a","MR Unit - A4a","MR Unit - A5a"]
MarketRental_Unit_Jr1Bedroom_Names = ["MR Unit - B1a", "MR Unit - B2a", "MR Unit - B3a","MR Unit - B4a","MR Unit - B5a"]
MarketRental_Unit_1Bedroom_Names = ["MR Unit - B1a", "MR Unit - B2a", "MR Unit - B3a","MR Unit - B4a","MR Unit - B5a"]
MarketRental_Unit_1BedroomDen_Names = ["MR Unit - B1a", "MR Unit - B2a", "MR Unit - B3a","MR Unit - B4a","MR Unit - B5a"]
MarketRental_Unit_Jr2Bedroom_Names = ["MR Unit - C1a", "MR Unit - C2a", "MR Unit - C3a","MR Unit - C4a","MR Unit - C5a"]
MarketRental_Unit_2Bedroom_Names = ["MR Unit - C1a", "MR Unit - C2a", "MR Unit - C3a","MR Unit - C4a","MR Unit - C5a"]
MarketRental_Unit_2BedroomDen_Names = ["MR Unit - C1a", "MR Unit - C2a", "MR Unit - C3a","MR Unit - C4a","MR Unit - C5a"]
MarketRental_Unit_3Bedroom_Names = ["MR Unit - D1a", "MR Unit - D2a", "MR Unit - D3a","MR Unit - D4a","MR Unit - D5a"]
MarketRental_Unit_3BedroomDen_Names = ["MR Unit - D1a", "MR Unit - D2a", "MR Unit - A3a","MR Unit - A4a","MR Unit - A5a"]
MarketRental_Unit_Townhouse_Names = ["MR Unit - E1a", "MR Unit - E2a", "MR Unit - E3a","MR Unit - E4a","MR Unit - E5a"]
MarketRental_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby","Amenity"]
MarketRental_Common_Core_Names = ["MR Core-1", "MR Core-2", "MR Core-3","MR Core-4","MR Core-5"]
MarketRental_Common_Corridor_Names = ["Cr-1", "Cr-2", "Cr-3","Cr-4","Cr-5"]
MarketRental_Common_ExistStair_Names = ["Stair – 1", "Stair – 2", "Stair – 3","Stair – 4","Stair – 5"]
MarketRental_Common_Lobby_Names = ["MR Lobby – 1", "MR Lobby – 2", "MR Lobby – 3","MR Lobby – 4","MR Lobby – 5"]
MarketRental_Common_Amenity_Names = ["MR Amenity – 1", "MR Amenity – 2", "MR Amenity – 3","MR Amenity – 4","MR Amenity – 5"]
MarketRental_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]

AffordableRental_Names = ["Unit", "Common", "Services"]
AffordableRental_Unit_Names = ["Jr.Studio", "Studio", "Jr.1 Bedroom","1 Bedroom","1 Bedroom & Den","Jr. 2 Bedroom","2 Bedroom","2 Bedroom & Den","3 Bedroom","3 Bedroom & Den","Townhouse","Exclusion"]
AffordableRental_Unit_JrStudio_Names = ["AR Unit - A1a", "AR Unit - A2a", "AR Unit - A3a","AR Unit - A4a","AR Unit - A5a"]
AffordableRental_Unit_Studio_Names = ["AR Unit - A1a", "AR Unit - A2a", "AR Unit - A3a","AR Unit - A4a","AR Unit - A5a"]
AffordableRental_Unit_Jr1Bedroom_Names = ["AR Unit - B1a", "AR Unit - B2a", "AR Unit - B3a","AR Unit - B4a","AR Unit - B5a"]
AffordableRental_Unit_1Bedroom_Names = ["AR Unit - B1a", "AR Unit - B2a", "AR Unit - B3a","AR Unit - B4a","AR Unit - B5a"]
AffordableRental_Unit_1BedroomDen_Names = ["AR Unit - B1a", "AR Unit - B2a", "AR Unit - B3a","AR Unit - B4a","AR Unit - B5a"]
AffordableRental_Unit_Jr2Bedroom_Names = ["AR Unit - C1a", "AR Unit - C2a", "AR Unit - C3a","AR Unit - C4a","AR Unit - C5a"]
AffordableRental_Unit_2Bedroom_Names = ["AR Unit - C1a", "AR Unit - C2a", "AR Unit - C3a","AR Unit - C4a","AR Unit - C5a"]
AffordableRental_Unit_2BedroomDen_Names = ["AR Unit - C1a", "AR Unit - C2a", "AR Unit - C3a","AR Unit - C4a","AR Unit - C5a"]
AffordableRental_Unit_3Bedroom_Names = ["AR Unit - D1a", "AR Unit - D2a", "AR Unit - D3a","AR Unit - D4a","AR Unit - D5a"]
AffordableRental_Unit_3BedroomDen_Names = ["AR Unit - D1a", "AR Unit - D2a", "AR Unit - A3a","AR Unit - A4a","AR Unit - A5a"]
AffordableRental_Unit_Townhouse_Names = ["AR Unit - E1a", "AR Unit - E2a", "AR Unit - E3a","AR Unit - E4a","AR Unit - E5a"]
AffordableRental_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby","Amenity"]
AffordableRental_Common_Core_Names = ["AR Core-1", "AR Core-2", "AR Core-3","AR Core-4","AR Core-5"]
AffordableRental_Common_Corridor_Names = ["Cr-1", "Cr-2", "Cr-3","Cr-4","Cr-5"]
AffordableRental_Common_ExistStair_Names = ["Stair – 1", "Stair – 2", "Stair – 3","Stair – 4","Stair – 5"]
AffordableRental_Common_Lobby_Names = ["AR Lobby – 1", "AR Lobby – 2", "AR Lobby – 3","AR Lobby – 4","AR Lobby – 5"]
AffordableRental_Common_Amenity_Names = ["AR Amenity – 1", "AR Amenity – 2", "AR Amenity – 3","AR Amenity – 4","AR Amenity – 5"]
AffordableRental_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]


SocialHousing_Names = ["Unit", "Common", "Services"]
SocialHousing_Unit_Names = ["Jr.Studio", "Studio", "Jr.1 Bedroom","1 Bedroom","1 Bedroom & Den","Jr. 2 Bedroom","2 Bedroom","2 Bedroom & Den","3 Bedroom","3 Bedroom & Den","Townhouse","Exclusion"]
SocialHousing_Unit_JrStudio_Names = ["SH Unit - A1a", "SH Unit - A2a", "SH Unit - A3a","SH Unit - A4a","SH Unit - A5a"]
SocialHousing_Unit_Studio_Names = ["SH Unit - A1a", "SH Unit - A2a", "SH Unit - A3a","SH Unit - A4a","SH Unit - A5a"]
SocialHousing_Unit_Jr1Bedroom_Names = ["SH Unit - B1a", "SH Unit - B2a", "SH Unit - B3a","SH Unit - B4a","SH Unit - B5a"]
SocialHousing_Unit_1Bedroom_Names = ["SH Unit - B1a", "SH Unit - B2a", "SH Unit - B3a","SH Unit - B4a","SH Unit - B5a"]
SocialHousing_Unit_1BedroomDen_Names = ["SH Unit - B1a", "SH Unit - B2a", "SH Unit - B3a","SH Unit - B4a","SH Unit - B5a"]
SocialHousing_Unit_Jr2Bedroom_Names = ["SH Unit - C1a", "SH Unit - C2a", "SH Unit - C3a","SH Unit - C4a","SH Unit - C5a"]
SocialHousing_Unit_2Bedroom_Names = ["SH Unit - C1a", "SH Unit - C2a", "SH Unit - C3a","SH Unit - C4a","SH Unit - C5a"]
SocialHousing_Unit_2BedroomDen_Names = ["SH Unit - C1a", "SH Unit - C2a", "SH Unit - C3a","SH Unit - C4a","SH Unit - C5a"]
SocialHousing_Unit_3Bedroom_Names = ["SH Unit - D1a", "SH Unit - D2a", "SH Unit - D3a","SH Unit - D4a","SH Unit - D5a"]
SocialHousing_Unit_3BedroomDen_Names = ["SH Unit - D1a", "SH Unit - D2a", "SH Unit - A3a","SH Unit - A4a","SH Unit - A5a"]
SocialHousing_Unit_Townhouse_Names = ["SH Unit - E1a", "SH Unit - E2a", "SH Unit - E3a","SH Unit - E4a","SH Unit - E5a"]
SocialHousing_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby","Amenity"]
SocialHousing_Common_Core_Names = ["SH Core-1", "SH Core-2", "SH Core-3","SH Core-4","SH Core-5"]
SocialHousing_Common_Corridor_Names = ["Cr-1", "Cr-2", "Cr-3","Cr-4","Cr-5"]
SocialHousing_Common_ExistStair_Names = ["Stair – 1", "Stair – 2", "Stair – 3","Stair – 4","Stair – 5"]
SocialHousing_Common_Lobby_Names = ["SH Lobby – 1", "SH Lobby – 2", "SH Lobby – 3","SH Lobby – 4","SH Lobby – 5"]
SocialHousing_Common_Amenity_Names = ["SH Amenity – 1", "SH Amenity – 2", "SH Amenity – 3","SH Amenity – 4","SH Amenity – 5"]
SocialHousing_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]

Hotel_Names = ["Unit", "Common", "Services"]
Hotel_Unit_Names = ["Suite King", "Standard King", "Accessible King","Suite Queen","Standard Queen","Accessible Queen","Standard King/Queen","Accessible King/Queen","Standard Queen/Queen","Accessible Queen/Queen","Standard Double/Double","Accessible Double/Double"]
Hotel_Unit_SuiteKing_Names = ["H Unit - A1a", "H Unit - A2a", "H Unit - A3a","H Unit - A4a","H Unit - A5a"]
Hotel_Unit_StandardKing_Names = ["H Unit - A1a", "H Unit - A2a", "H Unit - A3a","H Unit - A4a","H Unit - A5a"]
Hotel_Unit_AccessibleKing_Names = ["H Unit - B1a", "H Unit - B2a", "H Unit - B3a","H Unit - B4a","H Unit - B5a"]
Hotel_Unit_SuiteQueen_Names = ["H Unit - B1a", "H Unit - B2a", "H Unit - B3a","H Unit - B4a","H Unit - B5a"]
Hotel_Unit_StandardQueen_Names = ["H Unit - B1a", "H Unit - B2a", "H Unit - B3a","H Unit - B4a","H Unit - B5a"]
Hotel_Unit_AccessibleQueen_Names = ["H Unit - C1a", "H Unit - C2a", "H Unit - C3a","H Unit - C4a","H Unit - C5a"]
Hotel_Unit_StandardKingQueen_Names = ["H Unit - C1a", "H Unit - C2a", "H Unit - C3a","H Unit - C4a","H Unit - C5a"]
Hotel_Unit_AccessibleKingQueen_Names = ["H Unit - C1a", "H Unit - C2a", "H Unit - C3a","H Unit - C4a","H Unit - C5a"]
Hotel_Unit_StandardQueenQueen_Names = ["H Unit - D1a", "H Unit - D2a", "H Unit - D3a","H Unit - D4a","H Unit - D5a"]
Hotel_Unit_AccessibleQueenQueen_Names = ["H Unit - D1a", "H Unit - D2a", "H Unit - A3a","H Unit - A4a","H Unit - A5a"]
Hotel_Unit_StandardDoubleDouble_Names = ["H Unit - E1a", "H Unit - E2a", "H Unit - E3a","H Unit - E4a","H Unit - E5a"]
Hotel_Unit_AccessibleDoubleDouble_Names = ["H Unit - E1a", "H Unit - E2a", "H Unit - E3a","H Unit - E4a","H Unit - E5a"]
Hotel_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby","Amenity"]
Hotel_Common_Core_Names = ["H Core-1", "H Core-2", "H Core-3","H Core-4","H Core-5"]
Hotel_Common_Corridor_Names = ["Cr-1", "Cr-2", "Cr-3","Cr-4","Cr-5"]
Hotel_Common_ExistStair_Names = ["Stair – 1", "Stair – 2", "Stair – 3","Stair – 4","Stair – 5"]
Hotel_Common_Lobby_Names = ["H Lobby – 1", "H Lobby – 2", "H Lobby – 3","H Lobby – 4","H Lobby – 5"]
Hotel_Common_Amenity_Names = ["H Amenity – 1", "H Amenity – 2", "H Amenity – 3","H Amenity – 4","H Amenity – 5"]
Hotel_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]

Hotel_Common_Names = ["Core", "Corridor", "Exit Stair", "Lobby", "Amenity"]
Hotel_Services_Names = ["Mechanical", "Electrical", "Garbage","Storage","Loading"]

Office_Names = ["Unit", "Common", "Services"]
Office_Unit_Names = ["CRU1", "CRU2", "CRU3", "CRU4", "CRU5"]
Office_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby", "Amenity", "Washroom"]
Office_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]

Commercial_Names = ["CRU", "Common", "Services"]
Commercial_CRU_Names = ["Restaurant", "Retail"]
Commercial_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby", "Amenity", "Washroom"]
Commercial_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]



# Function to mark Area parameter
def mark_Area_parameter(area_type_01, area_type_02):
    selection_ids = uidoc.Selection.GetElementIds()
    if len(selection_ids) == 0:
        return "Nothing selected."

    # Filter selected elements by category "Area"
    selection = [doc.GetElement(x) for x in selection_ids]
    selection = filter(lambda x: x.Category.Name == "Area", selection)

    t = DB.Transaction(doc, "Area Parameter")
    t.Start()
    count = 0
    for area in selection:
           # Set "AREA TYPE 01" and "AREA TYPE 02" parameters
            area.LookupParameter("Area Type 01").Set(area_type_01)
            area.LookupParameter("Area Type 02").Set(area_type_02)
            count += 1
    t.Commit()
    return "{} Parameter text added.".format(count)

def populate_comments_parameter(area_type_01):
    selection_ids = uidoc.Selection.GetElementIds()
    if len(selection_ids) == 0:
        return "Nothing selected."

    # Filter selected elements by category "Area"
    selection = [doc.GetElement(x) for x in selection_ids]
    selection = filter(lambda x: x.Category.Name == "Area", selection)

    t = DB.Transaction(doc, "Area Parameter")
    t.Start()
    count = 0
    for area in selection:
        # Set "Comments" parameter
        area.LookupParameter("Comments").Set(area_type_01)
        count += 1
    t.Commit()
    return "{} Comments parameter populated.".format(count)


# Create a subclass of IExternalEventHandler for event handling
class area_text_SimpleEventHandler(IExternalEventHandler):
    def __init__(self, do_this):
        self.do_this = do_this
        self.kwargs = None
        self.OUT = None

    def Execute(self, uiapp):
        try:
            try:
                self.OUT = self.do_this(*self.kwargs)
            except:
                print("Failed")
                print(traceback.format_exc())
        except InvalidOperationException:
            print("InvalidOperationException caught")

    def GetName(self):
        return "Simple function executed by an IExternalEventHandler in a Form"

# Define a modeless WPF form
class Areatool_ModelessForm(WPFWindow):
    def pre_actions(self):
        self.simple_event_handler = area_text_SimpleEventHandler(mark_Area_parameter)
        

    def __init__(self):
        self.pre_actions()

        xaml_file_name = "Areatool_ModelessForm.xaml"
        WPFWindow.__init__(self, xaml_file_name)

        # Add an event handler for the Area01_selector
        self.Area01_selector.SelectionChanged += self.Area01_selector_SelectionChanged
        self.Area02_selector.SelectionChanged += self.Area02_selector_SelectionChanged
        self.Area03_selector.SelectionChanged += self.Area03_selector_SelectionChanged

        self.ext_event = ExternalEvent.Create(self.simple_event_handler)
        self.Show()
        
        Area01_Names_selector = self.Area01_selector
        Area01_Names_selector.Items.Clear()

        for name in Area01_Names:
            Area01_Names_selector.Items.Add(name)

        self.ext_event = ExternalEvent.Create(self.simple_event_handler)
        self.Show()


    def close_Click(self, sender, e):
        self.Close()

     def sent_Revit(self, sender, e):
        # Handle the "Sent to Revit" button click event
        self.populate_comments_parameter(Area01_selector)



    def Area01_selector_SelectionChanged(self, sender, e):
        # Handle the selection changed event for Area01_selector
        selected_area = self.Area01_selector.SelectedItem
        if selected_area == "Market Residential":
            # If "Market Residential" is selected, populate Area02_selector with MarketResidential_Names
            self.Area02_selector.Items.Clear()
            for name in MarketResidential_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Market Rental":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in MarketRental_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Affordable Rental":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in MarketRental_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Social Housing":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in SocialHousing_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Hotel":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in Hotel_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Office":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in Office_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Commercial":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in Commercial_Names:
                self.Area02_selector.Items.Add(name)
        # Add more conditions for other area types as needed
        
    def Area02_selector_SelectionChanged(self, sender, e):
        # Handle the selection changed event for Area02_selector
        selected_area = self.Area02_selector.SelectedItem
        self.Area03_selector.Items.Clear()
        
        if selected_area == "Unit":
            # If "Unit" is selected in Area02_selector, populate Area03_selector with MarketResidential_Unit_Names
            for name in MarketResidential_Unit_Names:
                self.Area03_selector.Items.Add(name)
        elif selected_area == "Common":
            # If "Common" is selected in Area02_selector, populate Area03_selector with MarketResidential_Common_Names
            for name in MarketResidential_Common_Names:
                self.Area03_selector.Items.Add(name)
        elif selected_area == "Services":
            # If "Common" is selected in Area02_selector, populate Area03_selector with MarketResidential_Common_Names
            for name in MarketResidential_Services_Names:
                self.Area03_selector.Items.Add(name)
                
    def Area03_selector_SelectionChanged(self, sender, e):
        # Handle the selection changed event for Area02_selector
        selected_area = self.Area03_selector.SelectedItem
        self.Area04_selector.Items.Clear()
        
        if selected_area == "Jr.Studio":
            # If "Jr.Studio" is selected in Area03_selector, populate Area04_selector with MarketResidential_Unit_JrStudio_Names
            for name in MarketResidential_Unit_JrStudio_Names:
                self.Area04_selector.Items.Add(name)
        elif selected_area == "Studio":
            # If "Common" is selected in Area02_selector, populate Area03_selector with MarketResidential_Unit_Studio_Names
            for name in MarketResidential_Unit_Studio_Names:
                self.Area04_selector.Items.Add(name)
        elif selected_area == "Services":
            # If "Common" is selected in Area02_selector, populate Area03_selector with MarketResidential_Common_Names
            for name in MarketResidential_Services_Names:
                self.Area04_selector.Items.Add(name)
                

# Customizable event handling classes
class CustomizableEvent:
    def __init__(self):
        custom_handler = _CustomHandler()
        custom_handler.customizable_event = self
        self.custom_event = UI.ExternalEvent.Create(custom_handler)
        self.function_or_method = None
        self.args = ()
        self.kwargs = {}

    def _raised_method(self):
        self.function_or_method(*self.args, **self.kwargs)

    def raise_event(self, function_or_method, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        self.function_or_method = function_or_method
        self.custom_event.Raise()

class _CustomHandler(UI.IExternalEventHandler):
    def __init__(self):
        self.customizable_event = None

    def Execute(self, application):
        try:
            self.customizable_event._raised_method()
        except InvalidOperationException:
            print("InvalidOperationException caught")

    def GetName(self):
        return "Execute a function or method in a IExternalHandler"

# Main function

def main():
    modeless_form = Areatool_ModelessForm()

# Run the main function if this script is executed
if __name__ == "__main__":
    main()

the way to run this is

if __name__ == '__main__':
    main()

Script is running issue is it is not updating the Parameter Comment or somewhat it is not calling the populate_comments_parameter function

Hi @saurabhsjha,
Remember to wrap your code in triple backticks (```) for better readability.

I see that the sent_Revit method is not indented properly, you need to be consistent with tab and/ or spaces since python is picky about it!

Also: in the Areatool_ModelessForm.__init__ method you have 2 duplicate lines, and thus you are calling self.Swoh() twice. I don’t know if this could lead to problems, but it surely not necessary.
I prefer to call the show method outside of the init, to let the code that calls the class to be in control of the window.

Random suggestion: move all those lists inside dictionaries with the possible selected area values as keys, so that you just access the dictionary instead of having to do all that if/elifs…

1 Like

Thank you for your recommendation. I have just started experimenting with WPF, as I was using forms for a simple function before. I apologize for my mistake. Now that I have resolved the Sent_revit function and will try to make the dictionary load all the data (in fact, I will create the data in Excel and read from there), I am facing a new issue that was not there before. The issue is that if I select another dropdown item from Area type 01, then the function Area01_selector_SelectionChanged says that MarketRental_Names are not defined, but they are global parameters.

# -*- coding: utf-8 -*-


from pyrevit import script #
# from pyrevit import revit #

# Import standard libraries
import sys
import traceback

# Import required libraries





from Autodesk.Revit.UI import IExternalEventHandler, ExternalEvent
from Autodesk.Revit.Exceptions import InvalidOperationException
from pyrevit.forms import WPFWindow
from pyrevit import script
from Autodesk.Revit import DB, UI

# Access the active document
uidoc = __revit__.ActiveUIDocument
doc = uidoc.Document

# Define the shared parameter names
shared_parameter01_name = "Area Type 01"
shared_parameter02_name = "Area Type 02"


# Define Area names
Area01_Names = ["Market Residential", "Market Rental", "Affordable Rental", "Social Housing", "Hotel", "Office", "Commercial"]

MarketResidential_Names = ["Unit", "Common", "Services"]
MarketResidential_Unit_Names = ["Jr.Studio", "Studio", "Jr.1 Bedroom","1 Bedroom","1 Bedroom & Den","Jr. 2 Bedroom","2 Bedroom","2 Bedroom & Den","3 Bedroom","3 Bedroom & Den","Townhouse","Exclusion"]
MarketResidential_Unit_JrStudio_Names = ["M Unit - A1a", "M Unit - A2a", "M Unit - A3a","M Unit - A4a","M Unit - A5a"]
MarketResidential_Unit_Studio_Names = ["M Unit - A1a", "M Unit - A2a", "M Unit - A3a","M Unit - A4a","M Unit - A5a"]
MarketResidential_Unit_Jr1Bedroom_Names = ["M Unit - B1a", "M Unit - B2a", "M Unit - B3a","M Unit - B4a","M Unit - B5a"]
MarketResidential_Unit_1Bedroom_Names = ["M Unit - B1a", "M Unit - B2a", "M Unit - B3a","M Unit - B4a","M Unit - B5a"]
MarketResidential_Unit_1BedroomDen_Names = ["M Unit - B1a", "M Unit - B2a", "M Unit - B3a","M Unit - B4a","M Unit - B5a"]
MarketResidential_Unit_Jr2Bedroom_Names = ["M Unit - C1a", "M Unit - C2a", "M Unit - C3a","M Unit - C4a","M Unit - C5a"]
MarketResidential_Unit_2Bedroom_Names = ["M Unit - C1a", "M Unit - C2a", "M Unit - C3a","M Unit - C4a","M Unit - C5a"]
MarketResidential_Unit_2BedroomDen_Names = ["M Unit - C1a", "M Unit - C2a", "M Unit - C3a","M Unit - C4a","M Unit - C5a"]
MarketResidential_Unit_3Bedroom_Names = ["M Unit - D1a", "M Unit - D2a", "M Unit - D3a","M Unit - D4a","M Unit - D5a"]
MarketResidential_Unit_3BedroomDen_Names = ["M Unit - D1a", "M Unit - D2a", "M Unit - A3a","M Unit - A4a","M Unit - A5a"]
MarketResidential_Unit_Townhouse_Names = ["M Unit - E1a", "M Unit - E2a", "M Unit - E3a","M Unit - E4a","M Unit - E5a"]
MarketResidential_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby","Amenity"]
MarketResidential_Common_Core_Names = ["M Core-1", "M Core-2", "M Core-3","M Core-4","M Core-5"]
MarketResidential_Common_Corridor_Names = ["Cr-1", "Cr-2", "Cr-3","Cr-4","Cr-5"]
MarketResidential_Common_ExistStair_Names = ["Stair – 1", "Stair – 2", "Stair – 3","Stair – 4","Stair – 5"]
MarketResidential_Common_Lobby_Names = ["M Lobby – 1", "M Lobby – 2", "M Lobby – 3","M Lobby – 4","M Lobby – 5"]
MarketResidential_Common_Amenity_Names = ["M Amenity – 1", "M Amenity – 2", "M Amenity – 3","M Amenity – 4","M Amenity – 5"]
MarketResidential_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]

MarketRental_Names = ["Unit", "Common", "Services"]
MarketRental_Unit_Names = ["Jr.Studio", "Studio", "Jr.1 Bedroom","1 Bedroom","1 Bedroom & Den","Jr. 2 Bedroom","2 Bedroom","2 Bedroom & Den","3 Bedroom","3 Bedroom & Den","Townhouse","Exclusion"]
MarketRental_Unit_JrStudio_Names = ["MR Unit - A1a", "MR Unit - A2a", "MR Unit - A3a","MR Unit - A4a","MR Unit - A5a"]
MarketRental_Unit_Studio_Names = ["MR Unit - A1a", "MR Unit - A2a", "MR Unit - A3a","MR Unit - A4a","MR Unit - A5a"]
MarketRental_Unit_Jr1Bedroom_Names = ["MR Unit - B1a", "MR Unit - B2a", "MR Unit - B3a","MR Unit - B4a","MR Unit - B5a"]
MarketRental_Unit_1Bedroom_Names = ["MR Unit - B1a", "MR Unit - B2a", "MR Unit - B3a","MR Unit - B4a","MR Unit - B5a"]
MarketRental_Unit_1BedroomDen_Names = ["MR Unit - B1a", "MR Unit - B2a", "MR Unit - B3a","MR Unit - B4a","MR Unit - B5a"]
MarketRental_Unit_Jr2Bedroom_Names = ["MR Unit - C1a", "MR Unit - C2a", "MR Unit - C3a","MR Unit - C4a","MR Unit - C5a"]
MarketRental_Unit_2Bedroom_Names = ["MR Unit - C1a", "MR Unit - C2a", "MR Unit - C3a","MR Unit - C4a","MR Unit - C5a"]
MarketRental_Unit_2BedroomDen_Names = ["MR Unit - C1a", "MR Unit - C2a", "MR Unit - C3a","MR Unit - C4a","MR Unit - C5a"]
MarketRental_Unit_3Bedroom_Names = ["MR Unit - D1a", "MR Unit - D2a", "MR Unit - D3a","MR Unit - D4a","MR Unit - D5a"]
MarketRental_Unit_3BedroomDen_Names = ["MR Unit - D1a", "MR Unit - D2a", "MR Unit - A3a","MR Unit - A4a","MR Unit - A5a"]
MarketRental_Unit_Townhouse_Names = ["MR Unit - E1a", "MR Unit - E2a", "MR Unit - E3a","MR Unit - E4a","MR Unit - E5a"]
MarketRental_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby","Amenity"]
MarketRental_Common_Core_Names = ["MR Core-1", "MR Core-2", "MR Core-3","MR Core-4","MR Core-5"]
MarketRental_Common_Corridor_Names = ["Cr-1", "Cr-2", "Cr-3","Cr-4","Cr-5"]
MarketRental_Common_ExistStair_Names = ["Stair – 1", "Stair – 2", "Stair – 3","Stair – 4","Stair – 5"]
MarketRental_Common_Lobby_Names = ["MR Lobby – 1", "MR Lobby – 2", "MR Lobby – 3","MR Lobby – 4","MR Lobby – 5"]
MarketRental_Common_Amenity_Names = ["MR Amenity – 1", "MR Amenity – 2", "MR Amenity – 3","MR Amenity – 4","MR Amenity – 5"]
MarketRental_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]

AffordableRental_Names = ["Unit", "Common", "Services"]
AffordableRental_Unit_Names = ["Jr.Studio", "Studio", "Jr.1 Bedroom","1 Bedroom","1 Bedroom & Den","Jr. 2 Bedroom","2 Bedroom","2 Bedroom & Den","3 Bedroom","3 Bedroom & Den","Townhouse","Exclusion"]
AffordableRental_Unit_JrStudio_Names = ["AR Unit - A1a", "AR Unit - A2a", "AR Unit - A3a","AR Unit - A4a","AR Unit - A5a"]
AffordableRental_Unit_Studio_Names = ["AR Unit - A1a", "AR Unit - A2a", "AR Unit - A3a","AR Unit - A4a","AR Unit - A5a"]
AffordableRental_Unit_Jr1Bedroom_Names = ["AR Unit - B1a", "AR Unit - B2a", "AR Unit - B3a","AR Unit - B4a","AR Unit - B5a"]
AffordableRental_Unit_1Bedroom_Names = ["AR Unit - B1a", "AR Unit - B2a", "AR Unit - B3a","AR Unit - B4a","AR Unit - B5a"]
AffordableRental_Unit_1BedroomDen_Names = ["AR Unit - B1a", "AR Unit - B2a", "AR Unit - B3a","AR Unit - B4a","AR Unit - B5a"]
AffordableRental_Unit_Jr2Bedroom_Names = ["AR Unit - C1a", "AR Unit - C2a", "AR Unit - C3a","AR Unit - C4a","AR Unit - C5a"]
AffordableRental_Unit_2Bedroom_Names = ["AR Unit - C1a", "AR Unit - C2a", "AR Unit - C3a","AR Unit - C4a","AR Unit - C5a"]
AffordableRental_Unit_2BedroomDen_Names = ["AR Unit - C1a", "AR Unit - C2a", "AR Unit - C3a","AR Unit - C4a","AR Unit - C5a"]
AffordableRental_Unit_3Bedroom_Names = ["AR Unit - D1a", "AR Unit - D2a", "AR Unit - D3a","AR Unit - D4a","AR Unit - D5a"]
AffordableRental_Unit_3BedroomDen_Names = ["AR Unit - D1a", "AR Unit - D2a", "AR Unit - A3a","AR Unit - A4a","AR Unit - A5a"]
AffordableRental_Unit_Townhouse_Names = ["AR Unit - E1a", "AR Unit - E2a", "AR Unit - E3a","AR Unit - E4a","AR Unit - E5a"]
AffordableRental_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby","Amenity"]
AffordableRental_Common_Core_Names = ["AR Core-1", "AR Core-2", "AR Core-3","AR Core-4","AR Core-5"]
AffordableRental_Common_Corridor_Names = ["Cr-1", "Cr-2", "Cr-3","Cr-4","Cr-5"]
AffordableRental_Common_ExistStair_Names = ["Stair – 1", "Stair – 2", "Stair – 3","Stair – 4","Stair – 5"]
AffordableRental_Common_Lobby_Names = ["AR Lobby – 1", "AR Lobby – 2", "AR Lobby – 3","AR Lobby – 4","AR Lobby – 5"]
AffordableRental_Common_Amenity_Names = ["AR Amenity – 1", "AR Amenity – 2", "AR Amenity – 3","AR Amenity – 4","AR Amenity – 5"]
AffordableRental_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]


SocialHousing_Names = ["Unit", "Common", "Services"]
SocialHousing_Unit_Names = ["Jr.Studio", "Studio", "Jr.1 Bedroom","1 Bedroom","1 Bedroom & Den","Jr. 2 Bedroom","2 Bedroom","2 Bedroom & Den","3 Bedroom","3 Bedroom & Den","Townhouse","Exclusion"]
SocialHousing_Unit_JrStudio_Names = ["SH Unit - A1a", "SH Unit - A2a", "SH Unit - A3a","SH Unit - A4a","SH Unit - A5a"]
SocialHousing_Unit_Studio_Names = ["SH Unit - A1a", "SH Unit - A2a", "SH Unit - A3a","SH Unit - A4a","SH Unit - A5a"]
SocialHousing_Unit_Jr1Bedroom_Names = ["SH Unit - B1a", "SH Unit - B2a", "SH Unit - B3a","SH Unit - B4a","SH Unit - B5a"]
SocialHousing_Unit_1Bedroom_Names = ["SH Unit - B1a", "SH Unit - B2a", "SH Unit - B3a","SH Unit - B4a","SH Unit - B5a"]
SocialHousing_Unit_1BedroomDen_Names = ["SH Unit - B1a", "SH Unit - B2a", "SH Unit - B3a","SH Unit - B4a","SH Unit - B5a"]
SocialHousing_Unit_Jr2Bedroom_Names = ["SH Unit - C1a", "SH Unit - C2a", "SH Unit - C3a","SH Unit - C4a","SH Unit - C5a"]
SocialHousing_Unit_2Bedroom_Names = ["SH Unit - C1a", "SH Unit - C2a", "SH Unit - C3a","SH Unit - C4a","SH Unit - C5a"]
SocialHousing_Unit_2BedroomDen_Names = ["SH Unit - C1a", "SH Unit - C2a", "SH Unit - C3a","SH Unit - C4a","SH Unit - C5a"]
SocialHousing_Unit_3Bedroom_Names = ["SH Unit - D1a", "SH Unit - D2a", "SH Unit - D3a","SH Unit - D4a","SH Unit - D5a"]
SocialHousing_Unit_3BedroomDen_Names = ["SH Unit - D1a", "SH Unit - D2a", "SH Unit - A3a","SH Unit - A4a","SH Unit - A5a"]
SocialHousing_Unit_Townhouse_Names = ["SH Unit - E1a", "SH Unit - E2a", "SH Unit - E3a","SH Unit - E4a","SH Unit - E5a"]
SocialHousing_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby","Amenity"]
SocialHousing_Common_Core_Names = ["SH Core-1", "SH Core-2", "SH Core-3","SH Core-4","SH Core-5"]
SocialHousing_Common_Corridor_Names = ["Cr-1", "Cr-2", "Cr-3","Cr-4","Cr-5"]
SocialHousing_Common_ExistStair_Names = ["Stair – 1", "Stair – 2", "Stair – 3","Stair – 4","Stair – 5"]
SocialHousing_Common_Lobby_Names = ["SH Lobby – 1", "SH Lobby – 2", "SH Lobby – 3","SH Lobby – 4","SH Lobby – 5"]
SocialHousing_Common_Amenity_Names = ["SH Amenity – 1", "SH Amenity – 2", "SH Amenity – 3","SH Amenity – 4","SH Amenity – 5"]
SocialHousing_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]

Hotel_Names = ["Unit", "Common", "Services"]
Hotel_Unit_Names = ["Suite King", "Standard King", "Accessible King","Suite Queen","Standard Queen","Accessible Queen","Standard King/Queen","Accessible King/Queen","Standard Queen/Queen","Accessible Queen/Queen","Standard Double/Double","Accessible Double/Double"]
Hotel_Unit_SuiteKing_Names = ["H Unit - A1a", "H Unit - A2a", "H Unit - A3a","H Unit - A4a","H Unit - A5a"]
Hotel_Unit_StandardKing_Names = ["H Unit - A1a", "H Unit - A2a", "H Unit - A3a","H Unit - A4a","H Unit - A5a"]
Hotel_Unit_AccessibleKing_Names = ["H Unit - B1a", "H Unit - B2a", "H Unit - B3a","H Unit - B4a","H Unit - B5a"]
Hotel_Unit_SuiteQueen_Names = ["H Unit - B1a", "H Unit - B2a", "H Unit - B3a","H Unit - B4a","H Unit - B5a"]
Hotel_Unit_StandardQueen_Names = ["H Unit - B1a", "H Unit - B2a", "H Unit - B3a","H Unit - B4a","H Unit - B5a"]
Hotel_Unit_AccessibleQueen_Names = ["H Unit - C1a", "H Unit - C2a", "H Unit - C3a","H Unit - C4a","H Unit - C5a"]
Hotel_Unit_StandardKingQueen_Names = ["H Unit - C1a", "H Unit - C2a", "H Unit - C3a","H Unit - C4a","H Unit - C5a"]
Hotel_Unit_AccessibleKingQueen_Names = ["H Unit - C1a", "H Unit - C2a", "H Unit - C3a","H Unit - C4a","H Unit - C5a"]
Hotel_Unit_StandardQueenQueen_Names = ["H Unit - D1a", "H Unit - D2a", "H Unit - D3a","H Unit - D4a","H Unit - D5a"]
Hotel_Unit_AccessibleQueenQueen_Names = ["H Unit - D1a", "H Unit - D2a", "H Unit - A3a","H Unit - A4a","H Unit - A5a"]
Hotel_Unit_StandardDoubleDouble_Names = ["H Unit - E1a", "H Unit - E2a", "H Unit - E3a","H Unit - E4a","H Unit - E5a"]
Hotel_Unit_AccessibleDoubleDouble_Names = ["H Unit - E1a", "H Unit - E2a", "H Unit - E3a","H Unit - E4a","H Unit - E5a"]
Hotel_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby","Amenity"]
Hotel_Common_Core_Names = ["H Core-1", "H Core-2", "H Core-3","H Core-4","H Core-5"]
Hotel_Common_Corridor_Names = ["Cr-1", "Cr-2", "Cr-3","Cr-4","Cr-5"]
Hotel_Common_ExistStair_Names = ["Stair – 1", "Stair – 2", "Stair – 3","Stair – 4","Stair – 5"]
Hotel_Common_Lobby_Names = ["H Lobby – 1", "H Lobby – 2", "H Lobby – 3","H Lobby – 4","H Lobby – 5"]
Hotel_Common_Amenity_Names = ["H Amenity – 1", "H Amenity – 2", "H Amenity – 3","H Amenity – 4","H Amenity – 5"]
Hotel_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]

Hotel_Common_Names = ["Core", "Corridor", "Exit Stair", "Lobby", "Amenity"]
Hotel_Services_Names = ["Mechanical", "Electrical", "Garbage","Storage","Loading"]

Office_Names = ["Unit", "Common", "Services"]
Office_Unit_Names = ["CRU1", "CRU2", "CRU3", "CRU4", "CRU5"]
Office_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby", "Amenity", "Washroom"]
Office_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]

Commercial_Names = ["CRU", "Common", "Services"]
Commercial_CRU_Names = ["Restaurant", "Retail"]
Commercial_Common_Names = ["Core", "Corridor", "Exit Stair","Lobby", "Amenity", "Washroom"]
Commercial_Services_Names = ["Mechanical", "Electrical", "Garbage", "Storage", "Loading"]



# Function to mark Area parameter



@cdatab.ERROR_HANDLE.try_catch_error
def mark_area_parameter(area_type_01, area_type_02):
    selection_ids = script.get_selected_elements()
    if not selection_ids:
        return "Nothing selected."

    doc = script.get_active_document()
    t = DB.Transaction(doc, "Area Parameter")
    t.Start()
    count = 0
    for element_id in selection_ids:
        area = doc.GetElement(element_id)
        if area.Category.Name == "Area":
            area.LookupParameter("Area Type 01").Set(area_type_01)
            area.LookupParameter("Area Type 02").Set(area_type_02)
            count += 1
    t.Commit()
    return "{} Parameter text added.".format(count)

@cdatab.ERROR_HANDLE.try_catch_error
# Create a subclass of IExternalEventHandler for event handling
class area_text_SimpleEventHandler(IExternalEventHandler):
    def __init__(self, do_this):
        self.do_this = do_this
        self.kwargs = None
        self.OUT = None

    def Execute(self, uiapp):
        try:
            try:
                self.OUT = self.do_this(*self.kwargs)
            except:
                print("Failed")
                print(traceback.format_exc())
        except InvalidOperationException:
            print("InvalidOperationException caught")

    def GetName(self):
        return "Simple function executed by an IExternalEventHandler in a Form"

# Define a modeless WPF form
class Areatool_ModelessForm(WPFWindow):

    """
    Simple modeless form sample
    """
    @cdatab.ERROR_HANDLE.try_catch_error
    def pre_actions(self):
        #print "doing preaction"
        # Now we need to make an instance of this handler. Moreover, it shows that the same class could be used to for
        # different functions using different handler class instances
        self.simple_event_handler = area_text_SimpleEventHandler(mark_area_parameter)
        # We now need to create the ExternalEvent
        self.ext_event = ExternalEvent.Create(self.simple_event_handler)
        #print "preaction done"
        #print self.simple_event_handler
        #print self.simple_event_handler.kwargs
        #print self.ext_event
        #print "-------"
        return
        
    @cdatab.ERROR_HANDLE.try_catch_error
    def __init__(self):
        self.pre_actions()

        xaml_file_name = "Areatool_ModelessForm.xaml"
        WPFWindow.__init__(self, xaml_file_name)

        self.title_text.Text = "CDA Area Tool"
        self.sub_text.Text = "Define the Area Parameter for CDA Standard making Scheduling Standardize and Easy"
        self.set_image_source(self.logo_img, "CDA.bmp")
        # Add an event handler for the Area01_selector
        self.Area01_selector.SelectionChanged += self.Area01_selector_SelectionChanged
        self.Area02_selector.SelectionChanged += self.Area02_selector_SelectionChanged
        self.Area03_selector.SelectionChanged += self.Area03_selector_SelectionChanged

        

        self.Show()
        
        Area01_Names_selector = self.Area01_selector
        Area01_Names_selector.Items.Clear()

        for name in Area01_Names:
            Area01_Names_selector.Items.Add(name)

        self.ext_event = ExternalEvent.Create(self.simple_event_handler)
        self.Show()


    def clear_mark_user_1_Click(self, sender, e):
        self.user_define_textbox_1.Text = ""

    def clear_mark_user_2_Click(self, sender, e):
        self.user_define_textbox_2.Text = ""


    def close_Click(self, sender, e):
        self.Close()



    @cdatab.ERROR_HANDLE.try_catch_error
    def send_to_area(self, sender, e):
        shared_parameter01_name = "Area Type 01"
        shared_parameter02_name = "Area Type 02"
        selected_area01 = self.Area01_selector.SelectedItem
        selected_area02 = self.Area02_selector.SelectedItem
        uidoc =  __revit__.ActiveUIDocument
        selected_areas = get_selected_areas(uidoc)
        for area in selected_areas:
           area.LookupParameter(shared_parameter01_name).Set(selected_area01)
           area.LookupParameter(shared_parameter02_name).Set(selected_area02)


    def mouse_down_main_panel(self, sender, args):
        sender.DragMove()

    def position_changed(self, sender, args):
        pass
        
    @cdatab.ERROR_HANDLE.try_catch_error
    def Area01_selector_SelectionChanged(self, sender, e):
        # Handle the selection changed event for Area01_selector
        selected_area = self.Area01_selector.SelectedItem
        if selected_area == "Market Residential":
            # If "Market Residential" is selected, populate Area02_selector with MarketResidential_Names
            self.Area02_selector.Items.Clear()
            for name in MarketResidential_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Market Rental":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in MarketRental_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Affordable Rental":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in MarketRental_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Social Housing":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in SocialHousing_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Hotel":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in Hotel_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Office":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in Office_Names:
                self.Area02_selector.Items.Add(name)
        elif selected_area == "Commercial":
            # If "Market Rental" is selected, populate Area02_selector with MarketRental_Names
            self.Area02_selector.Items.Clear()
            for name in Commercial_Names:
                self.Area02_selector.Items.Add(name)
        # Add more conditions for other area types as needed
        
    @cdatab.ERROR_HANDLE.try_catch_error    
    def Area02_selector_SelectionChanged(self, sender, e):
        # Handle the selection changed event for Area02_selector
        selected_area = self.Area02_selector.SelectedItem
        self.Area03_selector.Items.Clear()
        
        if selected_area == "Unit":
            # If "Unit" is selected in Area02_selector, populate Area03_selector with MarketResidential_Unit_Names
            for name in MarketResidential_Unit_Names:
                self.Area03_selector.Items.Add(name)
        elif selected_area == "Common":
            # If "Common" is selected in Area02_selector, populate Area03_selector with MarketResidential_Common_Names
            for name in MarketResidential_Common_Names:
                self.Area03_selector.Items.Add(name)
        elif selected_area == "Services":
            # If "Common" is selected in Area02_selector, populate Area03_selector with MarketResidential_Common_Names
            for name in MarketResidential_Services_Names:
                self.Area03_selector.Items.Add(name)
                
    @cdatab.ERROR_HANDLE.try_catch_error
    def Area03_selector_SelectionChanged(self, sender, e):
        # Handle the selection changed event for Area02_selector
        selected_area = self.Area03_selector.SelectedItem
        self.Area04_selector.Items.Clear()
        
        if selected_area == "Jr.Studio":
            # If "Jr.Studio" is selected in Area03_selector, populate Area04_selector with MarketResidential_Unit_JrStudio_Names
            for name in MarketResidential_Unit_JrStudio_Names:
                self.Area04_selector.Items.Add(name)
        elif selected_area == "Studio":
            # If "Common" is selected in Area02_selector, populate Area03_selector with MarketResidential_Unit_Studio_Names
            for name in MarketResidential_Unit_Studio_Names:
                self.Area04_selector.Items.Add(name)
        elif selected_area == "Services":
            # If "Common" is selected in Area02_selector, populate Area03_selector with MarketResidential_Common_Names
            for name in MarketResidential_Services_Names:
                self.Area04_selector.Items.Add(name)
                

# Customizable event handling classes
class CustomizableEvent:
    def __init__(self):
        custom_handler = _CustomHandler()
        custom_handler.customizable_event = self
        self.custom_event = UI.ExternalEvent.Create(custom_handler)
        self.function_or_method = None
        self.args = ()
        self.kwargs = {}

    def _raised_method(self):
        self.function_or_method(*self.args, **self.kwargs)

    def raise_event(self, function_or_method, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        self.function_or_method = function_or_method
        self.custom_event.Raise()

class _CustomHandler(UI.IExternalEventHandler):
    def __init__(self):
        self.customizable_event = None

    def Execute(self, application):
        try:
            self.customizable_event._raised_method()
        except InvalidOperationException:
            print("InvalidOperationException caught")

    def GetName(self):
        return "Execute a function or method in a IExternalHandler"






def get_selected_areas(uidoc):
    # Get the active selection
    selection = uidoc.Selection

    # Create an empty list to store selected areas
    selected_areas = []

    # Check if there is a valid selection
    if selection is not None and selection.GetElementIds():
        for element_id in selection.GetElementIds():
            element = uidoc.Document.GetElement(element_id)
            if isinstance(element, DB.Area):
                selected_areas.append(element)

    return selected_areas


# Main function
@cdatab.ERROR_HANDLE.try_catch_error
def main():
    # Get selected areas
    
    modeless_form = Areatool_ModelessForm()        

   
    
    

# Run the main function if this script is executed
if __name__ == "__main__":
    main()
    CDA_LOG.use_cdatab(coin_change=20, tool_used=__title__.replace("\n", " "), show_toast=True)

My Error Catcher gives this Traceback

Traceback (most recent call last):
  File "F:\Office_Standards\BIM\06_Automation\Pydesign\pyCDA.extension\lib\cdatab\ERROR_HANDLE.py", line 16, in wrapper
    out = func(*args, **kwargs)
  File "F:\Office_Standards\BIM\06_Automation\Pydesign\pyCDA.extension\CDA_Tools.tab\Area.panel\AreaPlanner.pushbutton\Area_script.py", line 325, in Area01_selector_SelectionChanged
    for name in MarketRental_Names:
NameError: name 'MarketRental_Names' is not defined ```

Hi, I would suggest to switch right away to a dictionary of dictionaries, like so:

# reverse the order of the list, putting the lowesl level at the top...
MarketResidential_Services_Names = [...]
# ... so that higher levels can use them...
MarketResidential_Names = {
    "Unit": MarketResidential_Unit_Names,
    "Common": MarketResidential_Common_Names,
    "Services": MarketResidential_Services_Names,
}
# ... and so on until the top level
Area01_Names = {
    "Market Residential": MarketResidential_Names,
    "Market Rental": MarketRental_Names,
    "Affordable Rental": MarketRental_Names,
    "Social Housing": SocialHousing_Names,
    "Hotel": Hotel_Names,
    "Office": Office_Names,
    "Commercial": Commercial_Names,
}

You then have only one variable that can be passed to the class in the __init__ method, instead of using globals (with the added benefit that you can reuse the for with different data).
The selection changed methods greatly simplifies to a level that they can call the same function with different arguments:

class Areatool_ModelessForm(WPFWindow):
    def pre_actions(self):
        self.simple_event_handler = area_text_SimpleEventHandler(mark_area_parameter)
        self.ext_event = ExternalEvent.Create(self.simple_event_handler)

    def __init__(self, area01_names):
        self.pre_actions()

        xaml_file_name = "Areatool_ModelessForm.xaml"
        WPFWindow.__init__(self, xaml_file_name)

        self.title_text.Text = "CDA Area Tool"
        self.sub_text.Text = "Define the Area Parameter for CDA Standard making Scheduling Standardize and Easy"
        self.set_image_source(self.logo_img, "CDA.bmp")
        # Add an event handler for the Area01_selector
        self.Area01_selector.SelectionChanged += self.Area01_selector_SelectionChanged
        self.Area02_selector.SelectionChanged += self.Area02_selector_SelectionChanged
        self.Area03_selector.SelectionChanged += self.Area03_selector_SelectionChanged

        # here we store the input data and prepare the attribute to store the chosen levels names
        self.area01_names = area01_names
        self.area02_names = {}
        self.area03_names = {}
        self.area04_names = []
        self._refresh_selector(self.Area01_selector, self.area01_names)

    @staticmethod
    def _refresh_selector(selector, names):
        selector.Items.Clear()
        for name in names:
            selector.Items.Add(name)

    def Area01_selector_SelectionChanged(self, sender, e):
        # storing the area02 names of the selected area01, then calling the refresh
        self.area02_names = self.area01_names[self.Area01_selector.SelectedItem]
        self._refresh_selector(self.Area02_selector, self.area02_names)

    def Area02_selector_SelectionChanged(self, sender, e):
        # Handle the selection changed event for Area02_selector
        self.area03_names = self.area02_names[self.Area02_selector.SelectedItem]
        self._refresh_selector(self.Area03_selector, self.area03_names)

    def Area03_selector_SelectionChanged(self, sender, e):
        # Handle the selection changed event for Area02_selector
        self.area04_names = self.area03_names[self.Area03_selector.SelectedItem]
        self._refresh_selector(self.Area04_selector, self.area04_names)

# ...

def main():
    areas_form = Areatool_ModelessForm(Area01_Names)
    areas_form.Show()

# ...

I left off some things, such as when you select a different name in the Area01 selector, the Area03 and Area04 lists (and the related area0X_names attributes) should be emptied.

Thank you so Much @sanzoghenzo i was struck on it from last week dictionary made it simpler