<C#>_使用代码编辑控件属性的一些小技巧

对于像我一样的C#窗体应用的初学者来说,要想实现某个控件的属性,一定是在软件右下角那里进行选择


这里可以实现控件的所有属性设置,还有涉及控件的一些触发事件,如按钮的点击事件,都可以在这里设置。


可是有时候,对于一个控件的某些属性,我们可能要在代码中实现出来,如果我们不熟悉控件属性的代码编辑方式,就很难解决问题。

在这里我来分享我今天发现的一个小技巧:

比如我们要改变一个按钮的背景颜色,我们只知道在上面属性栏里设置,但是项目过程中我们要用代码编辑出来,当我们不知道如何用代码实现时,可以先在属性栏里设置出来:


如上图,我们给一个按钮设置了背景颜色,如果我们想要看它代码怎么实现的,可以进入这个文件里看,这个Designer.cs是这个窗体实例化运行的代码,我们点击进去看看关于这个按钮的属性信息:


我们可以看到这个按钮的属性与其他没有设置背景颜色的属性相比,多了一行代码:


而且,这里变成了false,就是不适用虚拟原始的背景颜色风格。


所以我们已经达到我们的目的,知道如何用代码编写控件的属性了。


其实,在每个窗体被实例化的时候都会执行一个函数,是在它的构造函数里面实现的:


就是这个InitialzeComponent();

这个方法里实现窗体内部所有控件的属性,还有其他的一些。


                                                                                                                                                                                               Kyrie Irving___全.

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己的程序源码namespace zdm { partial class formFindReplace { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.buttonFind = new System.Windows.Forms.Button(); this.buttonReplace = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(35, 15); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(65, 12); this.label1.TabIndex = 0; this.label1.Text = "查找字符串"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(35, 60); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(65, 12); this.label2.TabIndex = 1; this.label2.Text = "替换字符串"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(135, 12); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(120, 21); this.textBox1.TabIndex = 2; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(135, 51); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(120, 21); this.textBox2.TabIndex = 3; // // buttonFind // this.buttonFind.Location = new System.Drawing.Point(49, 110); this.buttonFind.Name = "buttonFind"; this.buttonFind.Size = new System.Drawing.Size(75, 23); this.buttonFind.TabIndex = 4; this.buttonFind.Text = "查找下一处"; this.buttonFind.UseVisualStyleBackColor = true; this.buttonFind.Click += new System.EventHandler(this.buttonFind_Click); // // buttonReplace // this.buttonReplace.Location = new System.Drawing.Point(164, 110); this.buttonReplace.Name = "buttonReplace"; this.buttonReplace.Size = new System.Drawing.Size(91, 23); this.buttonReplace.TabIndex = 5; this.buttonReplace.Text = "替换查到字符"; this.buttonReplace.UseVisualStyleBackColor = true; this.buttonReplace.Click += new System.EventHandler(this.buttonReplace_Click); // // formFindReplace // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 169); this.Controls.Add(this.buttonReplace); this.Controls.Add(this.buttonFind); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "formFindReplace"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "查找和替换"; this.TopMost = true; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button buttonFind; private System.Windows.Forms.Button buttonReplace; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值