I have used OpenFileDialog in PropertyGrid in .NET Framework. It worked fine:
public class FileSelectorTypeEditor : System.Drawing.Design.UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context == null || context.Instance == null)
return base.GetEditStyle(context);
return UITypeEditorEditStyle.Modal;
}
// etc
}
Now in WinForms on .NET 9, I cannot find the type System.Drawing.Design.UITypeEditor
.
What am I doing wrong?
I have used OpenFileDialog in PropertyGrid in .NET Framework. It worked fine:
public class FileSelectorTypeEditor : System.Drawing.Design.UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context == null || context.Instance == null)
return base.GetEditStyle(context);
return UITypeEditorEditStyle.Modal;
}
// etc
}
Now in WinForms on .NET 9, I cannot find the type System.Drawing.Design.UITypeEditor
.
What am I doing wrong?
Share Improve this question edited Feb 6 at 18:20 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 6 at 14:06 ZedZipZedZip 6,47017 gold badges82 silver badges148 bronze badges 3- Works just fine on my machine, VS2022 version 17.12.0, .NET9 version 9.0.0. Hard to imagine how this could fail, given that the types are defined in the System.Windows.Forms assembly. You'd have to forget to target Winforms at all. – Hans Passant Commented Feb 6 at 15:08
- I do not understand why it is. May be : I have an WinForms app and it depends on project library, The PropertyGrid is in app but class which is used in PropertyGrid is in the library. But how to get this type in the library? – ZedZip Commented Feb 6 at 16:11
- Works fine for me. You should share a simple reproducing project, at least the csproj – Simon Mourier Commented Feb 6 at 18:11
1 Answer
Reset to default 1It appears you have created this Library using the generic, cross-platform Class Library
template.
This template of course doesn't include the System.Windows.Forms assembly(ies).
You could have picked the Windows Forms Class Library
instead.
Anyway, you can edit the Project's configuration file, and add to the main <PropertyGroup>
:
<UseWindowsForms>true</UseWindowsForms>
At this point, when you try to build the Solution, you're also notified that, to include these Windows Desktop assemblies, you also need to retarget the framework definition, changing it into:
<TargetFramework>net9.0-windows</TargetFramework>
so the Project's configuration file will look like:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
Of course, you could also add <UseWPF>true</UseWPF>
, in case some PresentationFramework assemblies are needed in this Project.
The Library is now Windows only, and cannot be otherwise, since Windows Forms / Windows Presentation Framework (WPF) assemblies are included.
As a consequence, the class is going to change to something like this (at least in relation to the section that's been posted here):
using System.ComponentModel;
using System.Drawing.Design;
public class FileSelectorTypeEditor : UITypeEditor {
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext? context) {
if (context == null || context.Instance == null)
return base.GetEditStyle(context);
return UITypeEditorEditStyle.Modal;
}
// etc
}