c#Forms:一些UI元素有2个文件(.cs& .resx),有些只有(.resx)。如何将.cs文件添加到用户控件?

时间:2012-02-16 21:47:39

标签: c# winforms

我正在查看其他人的代码并注意到某些用户控件包含Class.Designer.csClass.resx个文件,而其他用户只有.resx文件。有没有办法可以将.cs文件添加到只有.resx文件的控件中?

为什么/如何发生这种情况?

这两者有什么区别吗?看起来设计师代码在Designer.cs文件中,而只有.rexs的UC在单个.cs文件中包含所有代码。

谢谢!

1 个答案:

答案 0 :(得分:2)

有些人在创建用户控件和表单时会自动删除Class.Designer.cs文件。如果他们这样做,那么管理Form上控件放置的代码就不再分成一个部分类,而是包含在Class.cs文件后面的代码中。

例如,这里是Form 1.designer.cs。

namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <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.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Location = new System.Drawing.Point(49, 87);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(200, 100);
            this.panel1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
    }
}

如果我创建另一个表单,但删除了设计器文件,您会看到这种代码将进入Form1.cs代码文件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Panel panel1;

        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Location = new System.Drawing.Point(101, 62);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(200, 100);
            this.panel1.TabIndex = 0;
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.ResumeLayout(false);

        }
    }
}

这不会造成任何伤害,但是让代码处理不同文件中的表单控件可以使代码看起来更精简,更清晰,因此更易于导航和管理。