Skip to content

Using a ControlDesigner causes problems with DataBinding #12897

@Johmi

Description

@Johmi

Environment

Microsoft Visual Studio Professional 2022 (64-bit) - Preview
Version 17.13.0 Preview 5.0

.NET version

.NET 9

Did this work in a previous version of Visual Studio and/or previous .NET release?

Unknown

Issue description

I have a simple winforms program which contains a custom TextBox (MyTextBox) using a TextBoxDesigner (that inherits the ControlDesigner). The Designer just draws a rectangle on the MyTextBox for simplicity.

There is a Form with a MyTextbox as well as a normal Textbox.
My ViewModel contains just a Property "TextValue". Both textboxes have Databinding to this Property. When I enter anything in the normal Textbox, the Text appears also in the custom MyTextBox.

When I look at the Program in the Winforms Designer, everything is fine:
Image
I can also compile the program.

When I run the program I get this error:
Image

   at System.Reflection.TypeNameResolver.GetTypeFromDefaultAssemblies(String typeName, ReadOnlySpan`1 nestedTypeNames, TypeName parsedName)
   at System.Reflection.TypeNameResolver.GetType(String escapedTypeName, ReadOnlySpan`1 nestedTypeNames, TypeName parsedName)
   at System.Reflection.TypeNameResolver.GetSimpleType(TypeName typeName)
   at System.Reflection.TypeNameResolver.GetTypeHelper(Char* pTypeName, RuntimeAssembly requestingAssembly, Boolean throwOnError, Boolean requireAssemblyQualifiedName)
   at System.Reflection.CustomAttribute.CreateCustomAttributeInstance(RuntimeModule module, RuntimeType type, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.Attribute.GetCustomAttributes(MemberInfo element, Type attributeType, Boolean inherit)
   at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectGetAttributes(Type type)
   at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectedTypeData.GetAttributes()
   at System.ComponentModel.TypeDescriptor.DefaultTypeDescriptor.GetAttributes()
   at System.ComponentModel.TypeDescriptor.GetAttributes(Object component, Boolean noCustomTypeDesc)
   at System.Windows.Forms.Binding.CheckBinding()
   at System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value)
   at System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding)
   at System.Windows.Forms.BindingsCollection.Add(Binding binding)
   at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember, Boolean formattingEnabled, DataSourceUpdateMode updateMode, Object nullValue, String formatString, IFormatProvider formatInfo)
   at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember, Boolean formattingEnabled, DataSourceUpdateMode updateMode)
   at DesignerTestApp.Form1.InitDatabinding() in C:\Users\xxx\source\repos\xxx\DesignerTestApp\DesignerTestApp\DesignerTestApp\Views\Form1.vb:line 17

This error happens when I create the databinding for the custom MyTextBox.
Image

When I remove the databinding, then the program is starting without issues (aside from the missing databinding).

Is there any way to get rid of this problems?

Steps to reproduce

Here is the full code that causes the issues. The error also occures in my main program, but this small code is more easy to reproduce:

Form:

Public Class Form1

    Private _vm As FormViewModel

    Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        _vm = New FormViewModel

        Call InitDatabinding()
    End Sub

    Public Sub InitDatabinding()
        Try
            TxtInput.DataBindings.Add("Text", _vm, "TextValue", False, DataSourceUpdateMode.OnPropertyChanged)
            TxtShow.DataBindings.Add("Text", _vm, "TextValue", False, DataSourceUpdateMode.OnPropertyChanged)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

End Class

ViewModel:

Imports System.ComponentModel

Public Class FormViewModel
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private _textValue As String
    Public Property TextValue As String
        Get
            Return _textValue
        End Get
        Set(value As String)
            If _textValue <> value Then
                _textValue = value
                OnPropertyChanged(NameOf(TextValue))
            End If
        End Set
    End Property

    Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class

MyTextBox:

Imports System.ComponentModel
Imports System.ComponentModel.Design

<Designer(GetType(TextBoxDesigner))>
Partial Public Class MyTextBox
    Inherits TextBox

End Class

Designer:

Imports Microsoft.DotNet.DesignTools.Designers

Partial Public Class TextBoxDesigner
    Inherits ControlDesigner

    Protected Overrides Sub OnPaintAdornments(pe As PaintEventArgs)
        MyBase.OnPaintAdornments(pe)
        Dim rect As New Rectangle(New Point(1, 1), New Size(10, 10))
        Using brushRed As New SolidBrush(Color.Red)
            pe.Graphics.DrawRectangle(New Pen(Color.Red), rect)
            pe.Graphics.FillRectangle(brushRed, rect)
        End Using
    End Sub
End Class

Diagnostics

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions