类型'System.Windows.Forms.Form'没有名为'textBox1'的属性

时间:2015-03-11 09:55:54

标签: c# visual-studio-2013 properties windows-forms-designer

正如人们在评论中指出的那样,你永远不应该为真正的应用程序做这件事,因为它是不好的做法。我只是想了解属性实际上如何与Visual Studio一起使用,并且绝不会在真正的应用程序中执行此操作。

每当我尝试查看我创建的表单的设计时,我都会收到此错误。我进入代码并将textBox1字段设置为public并添加了getter和setter:

namespace WindowsFormsApplication2
{
    partial class Form2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(85, 82);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 0;
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.textBox1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #endregion
        public System.Windows.Forms.TextBox textBox1 { get; private set; }
    }
}

当我运行程序时,它编译并运行没有错误,并且显然有一个textBox1属性被声明。当我使用属性而不是普通字段时,为什么Visual Studio会中断?

编辑:感谢您修复代码块,我自己就是这样做了

1 个答案:

答案 0 :(得分:2)

您收到此错误是因为您正在手动更改设计器生成的代码。 如果您单击设计器中的文本框,则可以通过设计器本身将其公开,然后您就不会遇到此问题。