CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

VB -> C#: Winforms controls' fields converted to properties broke Windows Forms Visual Studio Designer

Open ezin82 opened this issue 7 years ago • 1 comments

I have create a simple windows form with a button and tried to convert it to c#

Input code

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
    Public Partial Class Form1
        Inherits System.Windows.Forms.Form

        <System.Diagnostics.DebuggerNonUserCode()>
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then components.Dispose()
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

        Private components As System.ComponentModel.IContainer

        <System.Diagnostics.DebuggerStepThrough()>
        Private Sub InitializeComponent()
            Me.Button1 = New System.Windows.Forms.Button()
            Me.SuspendLayout()
            '
            'Button1
            '
            Me.Button1.Location = New System.Drawing.Point(34, 40)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(186, 79)
            Me.Button1.TabIndex = 0
            Me.Button1.Text = "Button1"
            Me.Button1.UseVisualStyleBackColor = True
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(247, 160)
            Me.Controls.Add(Me.Button1)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)

        End Sub

        Friend WithEvents Button1 As Button
    End Class

Erroneous output

[global::Microsoft.VisualBasic.CompilerServices.DesignerGenerated()]
public partial class Form1 : System.Windows.Forms.Form
{
    [System.Diagnostics.DebuggerNonUserCode()]
    protected override void Dispose(bool disposing)
    {
        try
        {
            if (disposing && components != null)
                components.Dispose();
        }
        finally
        {
            base.Dispose(disposing);
        }
    }

    private System.ComponentModel.IContainer components;

    [System.Diagnostics.DebuggerStepThrough()]
    private void InitializeComponent()
    {
        this.Button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // Button1
        // 
        this.Button1.Location = new System.Drawing.Point(34, 40);
        this.Button1.Name = "Button1";
        this.Button1.Size = new System.Drawing.Size(186, 79);
        this.Button1.TabIndex = 0;
        this.Button1.Text = "Button1";
        this.Button1.UseVisualStyleBackColor = true;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6.0F, 13.0F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(247, 160);
        this.Controls.Add(this.Button1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }

    private Button _Button1;

    internal Button Button1
    {
        [MethodImpl(MethodImplOptions.Synchronized)]
        get
        {
            return _Button1;
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        set
        {
            if (_Button1 != null)
            {
                _Button1.Click -= Button1_Click_1;
            }

            _Button1 = value;
            if (_Button1 != null)
            {
                _Button1.Click += Button1_Click_1;
            }
        }
    }
}

Expected output

        [global::Microsoft.VisualBasic.CompilerServices.DesignerGenerated()]
        public partial class Form1 : System.Windows.Forms.Form
        {
            [System.Diagnostics.DebuggerNonUserCode()]
            protected override void Dispose(bool disposing)
            {
                try
                {
                    if (disposing && components != null)
                        components.Dispose();
                }
                finally
                {
                    base.Dispose(disposing);
                }
            }

            private System.ComponentModel.IContainer components;

            [System.Diagnostics.DebuggerStepThrough()]
            private void InitializeComponent()
            {
                this.Button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // Button1
                // 
                this.Button1.Location = new System.Drawing.Point(34, 40);
                this.Button1.Name = "Button1";
                this.Button1.Size = new System.Drawing.Size(186, 79);
                this.Button1.TabIndex = 0;
                this.Button1.Text = "Button1";
                this.Button1.UseVisualStyleBackColor = true;
                this.Button1.Click += new System.EventHandler(this.Button1_Click_1);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6.0F, 13.0F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(247, 160);
                this.Controls.Add(this.Button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
            }

            private Button Button1;
        }

Details

Product in use: VS extension Version in use: 6.8.0.0 The generated code compiles but when I try to open the converted form with the Visual Studio Designer it fails giving this error multiple times:

The type 'System.Windows.Forms.Form' has no property named 'Button1'.

It seems that Windows Forms Visual Studio Designer looks for a field and does not accept that Button1 is now modified in a property

ezin82 avatar Jun 10 '19 09:06 ezin82

Thanks for the report. My primary focus has been getting it to compile so I didn't think to check the designer after recent changes to this. I'll see if I can make it work. It might need a special case where references within InitializeComponent directly access the field, but ideally we'll find something that works in this and the more general case.

GrahamTheCoder avatar Jun 11 '19 08:06 GrahamTheCoder