Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Pay attention to "#Region " Windows Form Designer generated code "

We are building a windows form app.  We have approx 30 controls on one of our forms.  One of these controls is a custom ComboBox which has about 10 custom properties that can be set at design time.  The main ones allow one ComboBox to be linked to another one.  

One of the properties does  a check against 2 other properties before it assigns the value.  In the designer, I kept setting the property.  However, at runtime the property was always empty.  As a test, i set the property in the designer, closed the form, re-opened the form and the property was empty!

Why?  This took a few minutes to figure out, but take a look at the following which is auto-generated when the control is 'dropped' onto the form:

'cboType
                '
                Me.cboType.AddItems = False
                Me.cboType.BindingColumn = "ty"
                Me.cboType.BindingTable = "names"
                Me.cboType.ChildColumnName = "misc1"
                Me.cboType.ChildDataTable = Nothing
                Me.cboType.ChildDataTableName = Nothing
                Me.cboType.CodeDescription = Nothing
                Me.cboType.CodeDescriptionMaxLength = 0
                Me.cboType.CodeLength = 0
                Me.cboType.DataMember = "ty"
                Me.cboType.DataTableType = CustomDataSystems.CustomControls.CDSCombobox.CboDataTableType.Child
                Me.cboType.IsLinked = True
                Me.cboType.LinkedTo = Me.cboStatus
                Me.cboType.Location = New System.Drawing.Point(368, 128)
                Me.cboType.Name = "cboType"
                Me.cboType.ParentColumnName = Nothing
                Me.cboType.ParentDataTable = Nothing
                Me.cboType.ParentDataTableName = Nothing
                Me.cboType.Size = New System.Drawing.Size(256, 22)
                Me.cboType.TabIndex = 35

The properties are auto-generated in alphabetical order.  The property that would never save in the designer was:

Me.cboType.ChildColumnName = "misc1"

Why did it not set?  because, in the actual property code, I checked IsLinked and DataTableType first before assigning the private variable.  Well, these 2 are set AFTER ChildColumnName, so, the IDE could not stored the property value set in the designer.  How to resolve this?  Move the property assignment down the list so that it is after the other properties that need to be set:

                'cboType
                '
                Me.cboType.AddItems = False
                Me.cboType.BindingColumn = "ty"
                Me.cboType.BindingTable = "names"
                Me.cboType.ChildDataTable = Nothing
                Me.cboType.ChildDataTableName = Nothing
                Me.cboType.CodeDescription = Nothing
                Me.cboType.CodeDescriptionMaxLength = 0
                Me.cboType.CodeLength = 0
                Me.cboType.DataMember = "ty"
                Me.cboType.DataTableType = CustomDataSystems.CustomControls.CDSCombobox.CboDataTableType.Child
                Me.cboType.IsLinked = True
                Me.cboType.LinkedTo = Me.cboStatus
                Me.cboType.Location = New System.Drawing.Point(368, 128)
                Me.cboType.Name = "cboType"
                Me.cboType.ParentColumnName = Nothing
                Me.cboType.ParentDataTable = Nothing
                Me.cboType.ParentDataTableName = Nothing
                Me.cboType.ChildColumnName = "misc1"
                Me.cboType.Size = New System.Drawing.Size(256, 22)
                Me.cboType.TabIndex = 35

Now. ChildColumnName is set AFTER IsLinked and DataTableType.  Now, I can set the property a design time with the designer.

 

 

 

 

 

3 Comments

Comments have been disabled for this content.