C#自定义设计器——文件名选择编辑器

本文介绍了如何在C#中利用System.ComponentModel.Editor特性创建自定义设计器,实现文件名选择编辑器,并通过继承FileNameEditor类来添加文件过滤功能。通过重写FileNameEditor的虚函数,实现了弹出的文件选择对话框可以进行特定类型的文件过滤。
摘要由CSDN通过智能技术生成


C#自定义设计器——文件名选择编辑器

       暑期的时候用MCI封装了一个播放音乐的类,最近学习了GDI+,想着将这个类做成一个自定义的控件,那么在做的过程中有一个属性,如下:

		public string FileName
		{
			get { return fileName; }
			set { fileName = value; }
		}
		private string fileName;

编译之后,将它加到窗体上后,调到该控件的属性编辑框,可以看到,在杂项中有这个属性,我们可以在后面的框框中输入文件名,可是这个有点不人性化。我想能不能做成我一点某处就弹出一个文件选择框,通过谷歌上搜索了一下,查到了一个设计时特性——System.ComponentModel.Editor,实际上它就是定义好的一个类,其中一个构造函数如下:

自己的程序源码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; } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值