using ryCommon; using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace ryControls { /// /// 美化文本框控件 /// [DefaultEvent("TextChanged2")] public partial class TextBoxEx2 : UserControl { /// /// 文本变化时激发 /// [Description("文本变化时激发")] public event EventHandler TextChanged2; /// /// 文本变化时激发 /// [Description("文本变化时激发")] public new event EventHandler TextChanged; /// /// 双击时激发 /// [Description("双击时激发")] public new event EventHandler DoubleClick; /// /// /// public TextBoxEx2() { InitializeComponent(); ChangeSize(); txtInfo.Font = Font; } private void ChangeSize() { txtInfo.Left = 4; txtInfo.Width = base.Width - 8; if(Multiline) { txtInfo.Height = Height - 8; } if (txtInfo.Height < Height) { txtInfo.Top = (Height - txtInfo.Height) / 2; } else { txtInfo.Top = 1; if (!Multiline) { Height = txtInfo.Height + 2; } } this.Refresh(); } private void TextBoxEx2_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawRectangle(new Pen(SkinHelp.DefalutBorderColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1)); } private void TextBoxEx2_Resize(object sender, EventArgs e) { ChangeSize(); } private bool _OnlyNumeric = false; /// /// 是否只能输入数字 /// public bool OnlyNumeric { get { return _OnlyNumeric; } set { _OnlyNumeric = value;if (value) { txtInfo.Text = txtInfo.Text.ToDouble().ToString(); } } } /// /// 是否多行 /// public bool Multiline { get { return txtInfo.Multiline; } set { txtInfo.Multiline = value; txtInfo.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; } } /// /// 是否在显示不下时换行 /// public bool WordWrap { get { return txtInfo.WordWrap; } set { txtInfo.WordWrap = value; } } /// /// 最大长度 /// public int MaxLength { get { return txtInfo.MaxLength; } set { txtInfo.MaxLength = value; } } /// /// /// public System.Windows.Forms.HorizontalAlignment TextAlign { get { return txtInfo.TextAlign; } set { txtInfo.TextAlign = value; } } /// /// 背景颜色 /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public override Color BackColor { get { return txtInfo.BackColor; } set { txtInfo.BackColor = value; base.BackColor = value; } } /// /// 右键菜单 /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public override ContextMenuStrip ContextMenuStrip { get { return txtInfo.ContextMenuStrip; } set { txtInfo.ContextMenuStrip = value; base.ContextMenuStrip = value; } } /// /// 字体颜色 /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public override Color ForeColor { get { return txtInfo.ForeColor; } set { txtInfo.ForeColor = value; base.ForeColor = value; } } /// /// 选择的开始位置 /// [Browsable(false)] public int SelectionStart { get { return txtInfo.SelectionStart; } set { txtInfo.SelectionStart = value; } } /// /// 选择的长度 /// [Browsable(false)] public int SelectionLength { get { return txtInfo.SelectionLength; } set { txtInfo.SelectionLength = value; } } /// /// 选择的文本 /// [Browsable(false)] public string SelectedText { get { return txtInfo.SelectedText; } set { isProcUse = true; if (_text == "") { txtInfo.Text = value; } else { txtInfo.SelectedText = value; } _text = txtInfo.Text; if (_text == "") { EmptyShow(); } else { HaveTextShow(); txtInfo.Text = _text; } isProcUse = false; } } private string _ToolTip = ""; /// /// 提示文本 /// [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.Drawing.Design.UITypeEditor))] public string ToolTip { get { return _ToolTip; } set { _ToolTip = value;toolTip1.SetToolTip(this, _ToolTip); toolTip1.SetToolTip(txtInfo, _ToolTip); } } /// /// 选择全部 /// [Browsable(false)] public void SelectAll() { txtInfo.SelectAll(); } /// /// 密码 /// public char PasswordChar { get { return txtInfo.PasswordChar; } set { txtInfo.PasswordChar = value; } } /// /// 是否只读 /// public bool ReadOnly { get { return txtInfo.ReadOnly; } set { txtInfo.ReadOnly = value; } } private void TextBoxEx2_FontChanged(object sender, EventArgs e) { txtInfo.Font = this.Font; } /// /// /// /// protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); } private string _EmptyText = ""; /// /// 文本为空时的显示效果 /// public void EmptyShow() { if (_text == "") { if (!txtInfo.Focused) { txtInfo.Font = Font; txtInfo.ForeColor = Color.FromArgb(175, 185, 200); isProcUse = true; txtInfo.Text = EmptyText; isProcUse = false; } else { HaveTextShow(); txtInfo.Text = ""; } } } private Font _font = new Font("宋体", 9); /// /// 字体 /// public override Font Font { get { return _font; } set { _font = value; txtInfo.Font = value; } } /// /// 有内容时的显示效果 /// private void HaveTextShow() { txtInfo.Font = Font; txtInfo.ForeColor = Color.Black; } /// /// 当文本框为空时,显示的内容。 /// [Description("当文本框为空时,显示的内容。")] public string EmptyText { get { return _EmptyText; } set { _EmptyText = value; EmptyShow(); } } private string _text = ""; /// /// /// [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public override string Text { get { return _text; } set { _text = value; if (_text == "") { EmptyShow(); } else { HaveTextShow(); txtInfo.Text = _text; } //txtInfo.SelectionStart = 0; txtInfo.SelectionLength = 0; } } private void TxtInfo_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode ==Keys.A) { txtInfo.SelectAll(); } base.OnKeyDown(e); } /// /// 选择文本框中的文本范围 /// /// 文本框中当前选定文本的第一个字符的位置。 /// 要选择的字符数 public void Select(int start, int length) { txtInfo.Select(start, length); } private void TxtInfo_DoubleClick(object sender, EventArgs e) { DoubleClick?.Invoke(this, new EventArgs()); } private void TxtInfo_TextChanged(object sender, EventArgs e) { if (isProcUse) { return; } Text = txtInfo.Text; TextChanged2?.Invoke(this, new EventArgs()); TextChanged?.Invoke(this, new EventArgs()); base.OnTextChanged(e); } private void TxtInfo_Click(object sender, EventArgs e) { if (OnlyNumeric) { txtInfo.SelectAll(); return; } base.OnClick(e); } private void TxtInfo_MouseClick(object sender, MouseEventArgs e) { base.OnMouseClick(e); } private void TxtInfo_KeyPress(object sender, KeyPressEventArgs e) { if (OnlyNumeric) { if ((e.KeyChar <48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 46) { e.Handled = true; return; } if (e.KeyChar == 46 && txtInfo.Text.IndexOf(".") >= 0) { e.Handled = true;return; } } base.OnKeyPress(e); } private void TxtInfo_KeyUp(object sender, KeyEventArgs e) { base.OnKeyUp(e); } private bool isProcUse = false; private void TxtInfo_Enter(object sender, EventArgs e) { if (OnlyNumeric) { txtInfo.SelectAll(); return; } HaveTextShow(); if (Text == "") { txtInfo.Text = ""; } base.OnEnter(e); } private void TextBoxEx2_Enter(object sender, EventArgs e) { txtInfo.Focus(); } private void TxtInfo_MouseDown(object sender, MouseEventArgs e) { base.OnMouseDown(e); } private void TextBoxEx2_Load(object sender, EventArgs e) { EmptyShow(); } private void TxtInfo_Leave(object sender, EventArgs e) { if (Text == "") { EmptyShow(); } else { txtInfo.Font = Font; } } } }