Powered By Blogger

Thursday, July 20, 2023

Windows Forms deprecated controls usage in .NET Core 3.1/.NET 5 and later versions

Although the Windows Forms controls Menu, ToolBar, DataGrid, StatusBar, etc. have been removed from System.Windows.Forms assembly in .NET Core 3.1/.NET 5 and later versions, you can use them by adding the NuGet package System.Windows.Forms.Controls to your projects.

This package includes types and controls that were removed from System.Windows.Forms in .NET Core 3.1/.NET 5 and later versions.

According to Microsoft documentation, starting with .NET Core 3.1, various Windows Forms controls are no longer available:

  • ContextMenu
  • DataGrid
  • DataGrid.HitTestType
  • DataGridBoolColumn
  • DataGridCell
  • DataGridColumnStyle
  • DataGridLineStyle
  • DataGridParentRowsLabelStyle
  • DataGridPreferredColumnWidthTypeConverter
  • DataGridTableStyle
  • DataGridTextBox
  • DataGridTextBoxColumn
  • GridColumnStylesCollection
  • GridTablesFactory
  • GridTableStylesCollection
  • IDataGridEditingService
  • IMenuEditorService
  • MainMenu
  • Menu
  • Menu.MenuItemCollection
  • MenuItem
  • ToolBar
  • ToolBarAppearance
  • ToolBarButton
  • ToolBar.ToolBarButtonCollection
  • ToolBarButtonClickEventArgs
  • ToolBarButtonStyle
  • ToolBarTextAlign
  • StatusBar
  • StatusBarDrawItemEventArgs
  • StatusBarDrawItemEventHandler
  • StatusBarPanel
  • StatusBarPanelAutoSize
  • StatusBarPanelBorderStyle
  • StatusBarPanelClickEventArgs
  • StatusBarPanelClickEventHandler
  • StatusBarPanelStyle

It's suggested you upgrade your code to replace legacy controls, but if case your projects include many Windows Forms containing deprecated controls you can use the controls with minimal code changes.

Since it is impossible to use extension properties in .NET, extension methods should be used instead of properties:

Control type:

  • ContextMenu ContextMenu { get; set; } replace to GetContextMenu(), SetContextMenu(ContextMenu value) methods

Form type:

  • MainMenu Menu { get; set; } replace to GetMenu(), SetMenu(MainMenu value) methods
  • MainMenu MergedMenu { get; } replace to GetMergedMenu(MainMenu value) methods

The source code is based on Microsoft .NET code written in C# and is available on the GitHub repository System.Windows.Forms.Controls

No comments:

Post a Comment