Loading...
 

History: UnexpectedUseOfResources

Preview of version: 5

Unexpected use of resources by the forms designer

Over a long period, some users have reported problems with code generated in the function InitializeComponent(), looking something like this:

this.labelX9.BackgroundStyle.Class = global::YourProject.MultiLang._668;

The reference to MultiLang obviously implicates the Multi-Language Add-In, but the code in InitializeComponent() is neither generated nor modified by the Add-In. This effect has always baffled me.

Now I think I know what is happening, and I can illustrate it with a sample program. This sample contains several parts, which I will try to describe in detail

Sample Program

This sample defines

  • a Class
  • a UserControl which exposes a property of that class
  • a Form which uses the UserControl
  • a resource file

I have made the sample in VB, but it could just as easily be in C#. I am sure that there is no difference.

The class

Class MyObject
Imports System.ComponentModel

<TypeConverterAttribute(GetType(ExpandableObjectConverter))> _
Public Class MyObject

  Private mEmpty      As String = ""
  Private mOne        As String = "One"
  Private mTwo        As String = "Two"

  <Localizable(True)> _
  Public Property Empty as String
    Get
      Return mEmpty
    End Get
    Set(ByVal value As String)
      mEmpty = value
    End Set
  End Property

  <Localizable(True)> _
  Public Property One as String
    Get
      Return mOne
    End Get
    Set(ByVal value As String)
      mOne = value
    End Set
  End Property

  Public Property Two as String
    Get
      Return mTwo
    End Get
    Set(ByVal value As String)
      mTwo = value
    End Set
  End Property

End Class

This class contains three properties:

NameAttributesInitial value
EmptyLocalizable""
OneLocalizable"One"
TwoNone"Two"

The class itself has the attribute <TypeConverterAttribute(GetType(ExpandableObjectConverter))>, which means that it is will be expandable in the properties grid.

The UserControl

This is the user defined code in the UserControl. Obviously there is more code in the .designer.vb file, but that does not particularly interest us.

UserControl ControlWithObject
Imports System.ComponentModel

Public Class ControlWithObject

  Private mObject As New MyObject

  <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
  Public ReadOnly Property ChildObject as MyObject
    Get
      Return mObject
    End Get
  End Property

End Class


This UserControl has a single property named ChildObject, of type MyObject.

The form

I have placed a single instance of the UserControl ControlWithObject on the form.
Image
As you can see, the ChildObject property can be expanded in the properties window.

This form contains no user defined code.

The resource file

I have added three resource strings to the project, containing the same strings as the properties in the MyObject class.
Image
I have named the resources Empty, One and Two, corresponding to their contents, but the names are unimportant.

History

Information Version
Wed 15 of Sep, 2010 19:12 GMT Phil 20
Fri 04 of Jun, 2010 20:19 GMT Phil 19
Fri 04 of Jun, 2010 19:25 GMT Phil 18
Fri 04 of Jun, 2010 19:04 GMT Phil 17
Fri 04 of Jun, 2010 18:47 GMT Phil 16
Fri 04 of Jun, 2010 18:46 GMT Phil 15
Fri 04 of Jun, 2010 18:41 GMT Phil 14
Fri 04 of Jun, 2010 18:33 GMT Phil 13
Fri 04 of Jun, 2010 18:32 GMT Phil 12
Fri 04 of Jun, 2010 17:40 GMT Phil 11
Fri 04 of Jun, 2010 17:36 GMT Phil 10
Fri 04 of Jun, 2010 17:28 GMT Phil 9
Fri 04 of Jun, 2010 17:26 GMT Phil 8
Fri 04 of Jun, 2010 17:19 GMT Phil 7
Fri 04 of Jun, 2010 16:58 GMT Phil 6
Fri 04 of Jun, 2010 16:53 GMT Phil 5
Fri 04 of Jun, 2010 16:43 GMT Phil 4
Fri 04 of Jun, 2010 16:35 GMT Phil 3
Fri 04 of Jun, 2010 16:24 GMT Phil 2
Fri 04 of Jun, 2010 13:06 GMT Phil 1