控 件 的 包 装 控件的包装 控件的包装
1.控件的包装
控件的包装,就是把标准控件包装一层,是复合控件的一种特殊形式
比如,比较常见的是对TextBox的包装
public MjTextBox : UserControl
{
public TextBox edit;
}
比如:TextBox不可调整高度、不可设置Padding,所以在布局时会产生困难。
2.MjTextBox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm控件的包装
{
public partial class MjTextBox : UserControl
{
public MjTextBox()
{
InitializeComponent();
}
protected override void OnLayout(LayoutEventArgs e)
{
base.OnLayout(e);
// 获取子控件
if (this.Controls.Count == 0) return;
Control c = this.Controls[0];
// 父窗口参数
Padding p = this.Padding;
int x = 0, y = 0;
int w = this.Width, h = this.Height;
w -= (p.Left + p.Right);
x += p.Left;
// 计算文本框的高度,使其显示在中间
int h2 = c.PreferredSize.Height;
if (h2 > h) h2 = h;
y = (h - h2) / 2;
c.Location = new Point(x, y);
c.Size = new Size(w, h2);
}
}
}
之后重新生成项目
重新打开Form.cs
3.单文件定义
可以在一个单独的文件中定义,以方便重复
1.手工定义一个类MjTextBox : UserControl
2.双击打开设计器,设计器会把界面代码写在同一个文件里
3.添加构造方法,调用InitializeComponent
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm控件的包装
{
class MjTextBox:UserControl
{
private TextBox edit;
private void InitializeComponent()
{
this.edit = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// edit
//
this.edit.Location = new System.Drawing.Point(136, 59);
this.edit.Name = "edit";
this.edit.Size = new System.Drawing.Size(180, 25);
this.edit.TabIndex = 0;
//
// MjTextBox
//
this.Controls.Add(this.edit);
this.Name = "MjTextBox";
this.Size = new System.Drawing.Size(443, 153);
this.ResumeLayout(false);
this.PerformLayout();
}
// 手写
public MjTextBox()
{
InitializeComponent();
}
protected override void OnLayout(LayoutEventArgs e)
{
base.OnLayout(e);
// 获取子控件
if (this.Controls.Count == 0) return;
Control c = this.Controls[0];
// 父窗口参数
Padding p = this.Padding;
int x = 0, y = 0;
int w = this.Width, h = this.Height;
w -= (p.Left + p.Right);
x += p.Left;
// 计算文本框的高度,使其显示在中间
int h2 = c.PreferredSize.Height;
if (h2 > h) h2 = h;
y = (h - h2) / 2;
c.Location = new Point(x, y);
c.Size = new Size(w, h2);
}
}
}
4.添加属性
演示:给MjTextBox添加一些属性,使其和TextBox用法类似
- Text 文本
- Font 字体
- BackColor 背景色
- ForeColor 前景色
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm控件的包装
{
class MjTextBox:UserControl
{
private TextBox edit;
private void InitializeComponent()
{
this.edit = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// edit
//
this.edit.Location = new System.Drawing.Point(136, 59);
this.edit.Name = "edit";
this.edit.Size = new System.Drawing.Size(180, 25);
this.edit.TabIndex = 0;
//
// MjTextBox
//
this.Controls.Add(this.edit);
this.Name = "MjTextBox";
this.Size = new System.Drawing.Size(443, 153);
this.ResumeLayout(false);
this.PerformLayout();
}
// 手写
public MjTextBox()
{
InitializeComponent();
}
protected override void OnLayout(LayoutEventArgs e)
{
base.OnLayout(e);
// 获取子控件
if (this.Controls.Count == 0) return;
Control c = this.Controls[0];
// 父窗口参数
Padding p = this.Padding;
int x = 0, y = 0;
int w = this.Width, h = this.Height;
w -= (p.Left + p.Right);
x += p.Left;
// 计算文本框的高度,使其显示在中间
int h2 = c.PreferredSize.Height;
if (h2 > h) h2 = h;
y = (h - h2) / 2;
c.Location = new Point(x, y);
c.Size = new Size(w, h2);
}
// 添加属性
[Browsable(true)] // 编译器是否可见此属性
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get
{
return edit.Text;
}
set
{
edit.Text = value;
}
}
[Browsable(true)] // 编译器是否可见此属性
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Color BackColor {
get
{
return edit.BackColor;
}
set
{
base.BackColor = value;
edit.BackColor = value;
}
}
}
}
5.添加事件
给MjTextBox添加一个回车事件ReturnPressed
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm控件的包装
{
class MjTextBox:UserControl
{
private TextBox edit;
private void InitializeComponent()
{
this.edit = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// edit
//
this.edit.Location = new System.Drawing.Point(136, 59);
this.edit.Name = "edit";
this.edit.Size = new System.Drawing.Size(180, 25);
this.edit.TabIndex = 0;
this.edit.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.press);
//
// MjTextBox
//
this.Controls.Add(this.edit);
this.Name = "MjTextBox";
this.Size = new System.Drawing.Size(443, 153);
this.ResumeLayout(false);
this.PerformLayout();
}
// 手写
public MjTextBox()
{
InitializeComponent();
}
protected override void OnLayout(LayoutEventArgs e)
{
base.OnLayout(e);
// 获取子控件
if (this.Controls.Count == 0) return;
Control c = this.Controls[0];
// 父窗口参数
Padding p = this.Padding;
int x = 0, y = 0;
int w = this.Width, h = this.Height;
w -= (p.Left + p.Right);
x += p.Left;
// 计算文本框的高度,使其显示在中间
int h2 = c.PreferredSize.Height;
if (h2 > h) h2 = h;
y = (h - h2) / 2;
c.Location = new Point(x, y);
c.Size = new Size(w, h2);
}
// 添加属性
[Browsable(true)] // 编译器是否可见此属性
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get
{
return edit.Text;
}
set
{
edit.Text = value;
}
}
[Browsable(true)] // 编译器是否可见此属性
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Color BackColor {
get
{
return edit.BackColor;
}
set
{
base.BackColor = value;
edit.BackColor = value;
}
}
// 自定义回车事件:
public event EventHandler ReturnPressed;
private void press(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if(ch == '\r')
{
ReturnPressed?.Invoke(this, e);
}
}
}
}
public event EventHandler ReturnPressed;
private void press(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if(ch == '\r')
{
ReturnPressed?.Invoke(this, e);
}
}