Loading...
 

History: UnexpectedUseOfResources

Preview of version: 14

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 = ""

  Public Property Empty as String
    Get
      Return mEmpty
    End Get
    Set(ByVal value As String)
      mEmpty = value
    End Set
  End Property

End Class

This class contains a single String property, which is initialized with an empty string.

This property can have the <Localizable(True)> attribute, but this is not necessary.

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 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 a single resource strings to the project, named EmptyString and containing an empty string.
Image

InitializeComponent

Before going any further, I want to take a look at some of the code in InitializeComponent(), in the .designer.vb file.

from InitializeComponent (with
'
'ControlWithObject1
'
Me.ControlWithObject1.BackColor = System.Drawing.Color.DarkRed
Me.ControlWithObject1.ChildObject.Empty = ""
Me.ControlWithObject1.Location = New System.Drawing.Point(12, 12)
Me.ControlWithObject1.Name = "ControlWithObject1"
Me.ControlWithObject1.Size = New System.Drawing.Size(150, 150)
Me.ControlWithObject1.TabIndex = 0

What happens when we set Localizable=True

Now lets look at what happens when we set the Localizable property of the form to True.
Image
As you probably know, this modifies the code generated in InitializeComponent. Lets take a look at the now code.

from InitializeComponent (with
'
'ControlWithObject1
'
Me.ControlWithObject1.BackColor = System.Drawing.Color.DarkRed
Me.ControlWithObject1.ChildObject.Empty = Global.EmptyStringTest2.My.Resources.Resources.EmptyString
resources.ApplyResources(Me.ControlWithObject1, "ControlWithObject1")
Me.ControlWithObject1.Name = "ControlWithObject1"

As we would expect, a call to resources.ApplyResources() has been added, which loads the localizable properties from a resource file. The actual values of these properties have been moved into the resource file Form1.resx.

However, this does not apply to the properties of ChildObject.

Here something rather strange has happened. The property ChildObject.Empty, which contains an empty string, is now loaded from the resource string EmptyString.

This is strange for two reasons:

  1. The property Empty is does not have the localizable attribute
  2. The resource is read from the global resource file resources.resx and not the local resource file form1.resx

Summary

The effect is, that the forms designer decides to load a property from a resource string, for no apparent reason.

So far as I can tell:

  • It only happens for string properties of child objects
  • It only happens for empty strings
  • It does not matter whether the string property has the localizable attribute or not
  • The forms designer uses an existing resource containing an empty string
  • The resource string comes from a global resource file. This might be
    • the default resource file Resources.resx
    • the resource file MultiLang.resx
  • The resource string does not come from a local resource file (e.g. Form1.resx)
  • The effect can be provoked by setting the Localizable property of the Form to True.
    There may be other ways to provoke it.

What does this mean for the Multi-Language Add-In

If:

  • the resource file MultiLang.resx contains an empty resource string
  • a control contains an object property
  • that object contains a string property
  • that string property contains an empty string

then the forms designer may decide to load that string property from the empty resource string in MultiLang.resx.

Is this a problem?

This effect may lead to a compilation error. In fact, you will probably only notice it if it does cause a compilation error.

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