C# Winform编程自定义控件(UserControl)可自适应大小的TextBox复合控件

一个C# Winform自定义控件(UserControl)MyTextBox,实现功能:

  1. 窗体大小变化时重新计算自身位置,始终保持在居中状态。
  2. 自定义键盘回车事件,回车键触发时,调用用户传递的回调。
  3. 不使用*.Designer.cs文件,直接放在*.cs中。
  4. 重写控件属性Text与BackColor

直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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 BubbleMessage
{
public class MyTextBox : UserControl
{
public MyTextBox()
{
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);
}


public event EventHandler ReturnPressed;

/// <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 组件设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.edit = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// edit
//
this.edit.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.edit.BackColor = System.Drawing.Color.White;
this.edit.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.edit.Location = new System.Drawing.Point(3, 17);
this.edit.Name = "edit";
this.edit.Size = new System.Drawing.Size(336, 18);
this.edit.TabIndex = 0;
this.edit.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.edit_KeyPress);
//
// MyTextBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.edit);
this.Name = "MyTextBox";
this.Size = new System.Drawing.Size(344, 53);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox edit;

private void edit_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
ReturnPressed?.Invoke(this, e);
}
}

public override string Text
{
get { return edit.Text; }
set
{
edit.Text = value;
}
}


public override Color BackColor
{
get { return base.BackColor; }
set
{
edit.BackColor = value;
base.BackColor = value;
}
}
}
}