当前位置 博文首页 > 文章内容

    C# 控件属性和InitializeComponent()关系案例详解

    作者:shunshunshun18 栏目:未分类 时间:2021-08-28 14:43:12

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    namespace Test22
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
                ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
                this.SuspendLayout();
                //
                // numericUpDown1
                //
                this.numericUpDown1.DecimalPlaces = 4;//属性里对应!!!!!
                this.numericUpDown1.Location = new System.Drawing.Point(12, 12);
                this.numericUpDown1.Name = "numericUpDown1";
                this.numericUpDown1.Size = new System.Drawing.Size(120, 21);
                this.numericUpDown1.TabIndex = 0;
                //
                // Form1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(153, 53);
                this.Controls.Add(this.numericUpDown1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.NumericUpDown numericUpDown1;
        }
    }

    控件中小数点位数和InitializeComponent()里面的代码相呼应,而下面的代码又设置了2,所以覆盖掉了,代码和运行结果如下:

    using System;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    namespace Test22
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                numericUpDown1.Maximum = 20;
                numericUpDown1.Minimum = 1;
                numericUpDown1.DecimalPlaces = 2;
            }
        }
    }