RaUI/Source/ryControls/Controls/TextBoxEx2.cs
zilinsoft 3262955f2f ### 2023-11-07更新
------
#### RaUIV4    V4.0.2311.0701
- *.[全新]整合了MyDb、ryControls、MyDb_MySQL等dll文件到RaUI一个项目。
- *.[新增]新增ApkOp类,可以轻松获取APK信息。
- *.[新增]新增JsonExt扩展类,让Json操作更简单。
- *.[新增]新增WebP类,可以支持webp格式的图片。
- *.[改进]ryQuickSQL中的AddField方法改为自动替换已存在的同名值。
- *.[修复]ryQuickSQL中的AddFieldCalc方法无法正常计算的BUG。
2023-11-07 16:37:53 +08:00

421 lines
12 KiB
C#

using ryCommon;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ryControls
{
/// <summary>
/// 美化文本框控件
/// </summary>
[DefaultEvent("TextChanged2")]
public partial class TextBoxEx2 : UserControl
{
/// <summary>
/// 文本变化时激发
/// </summary>
[Description("文本变化时激发")]
public event EventHandler TextChanged2;
/// <summary>
/// 文本变化时激发
/// </summary>
[Description("文本变化时激发")]
public new event EventHandler TextChanged;
/// <summary>
/// 双击时激发
/// </summary>
[Description("双击时激发")]
public new event EventHandler DoubleClick;
/// <summary>
///
/// </summary>
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;
/// <summary>
/// 是否只能输入数字
/// </summary>
public bool OnlyNumeric
{
get { return _OnlyNumeric; }
set { _OnlyNumeric = value;if (value) { txtInfo.Text = txtInfo.Text.ToDouble().ToString(); } }
}
/// <summary>
/// 是否多行
/// </summary>
public bool Multiline
{
get { return txtInfo.Multiline; }
set { txtInfo.Multiline = value; txtInfo.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; }
}
/// <summary>
/// 是否在显示不下时换行
/// </summary>
public bool WordWrap
{
get { return txtInfo.WordWrap; }
set { txtInfo.WordWrap = value; }
}
/// <summary>
/// 最大长度
/// </summary>
public int MaxLength
{
get { return txtInfo.MaxLength; }
set { txtInfo.MaxLength = value; }
}
/// <summary>
///
/// </summary>
public System.Windows.Forms.HorizontalAlignment TextAlign
{
get { return txtInfo.TextAlign; }
set { txtInfo.TextAlign = value; }
}
/// <summary>
/// 背景颜色
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Color BackColor
{
get { return txtInfo.BackColor; }
set { txtInfo.BackColor = value; base.BackColor = value; }
}
/// <summary>
/// 右键菜单
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override ContextMenuStrip ContextMenuStrip
{
get { return txtInfo.ContextMenuStrip; }
set { txtInfo.ContextMenuStrip = value; base.ContextMenuStrip = value; }
}
/// <summary>
/// 字体颜色
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Color ForeColor
{
get { return txtInfo.ForeColor; }
set { txtInfo.ForeColor = value; base.ForeColor = value; }
}
/// <summary>
/// 选择的开始位置
/// </summary>
[Browsable(false)]
public int SelectionStart
{
get { return txtInfo.SelectionStart; }
set { txtInfo.SelectionStart = value; }
}
/// <summary>
/// 选择的长度
/// </summary>
[Browsable(false)]
public int SelectionLength
{
get { return txtInfo.SelectionLength; }
set { txtInfo.SelectionLength = value; }
}
/// <summary>
/// 选择的文本
/// </summary>
[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 = "";
/// <summary>
/// 提示文本
/// </summary>
[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); }
}
/// <summary>
/// 选择全部
/// </summary>
[Browsable(false)]
public void SelectAll()
{
txtInfo.SelectAll();
}
/// <summary>
/// 密码
/// </summary>
public char PasswordChar
{
get { return txtInfo.PasswordChar; }
set { txtInfo.PasswordChar = value; }
}
/// <summary>
/// 是否只读
/// </summary>
public bool ReadOnly
{
get { return txtInfo.ReadOnly; }
set { txtInfo.ReadOnly = value; }
}
private void TextBoxEx2_FontChanged(object sender, EventArgs e)
{
txtInfo.Font = this.Font;
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
}
private string _EmptyText = "";
/// <summary>
/// 文本为空时的显示效果
/// </summary>
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);
/// <summary>
/// 字体
/// </summary>
public override Font Font
{
get
{
return _font;
}
set
{
_font = value;
txtInfo.Font = value;
}
}
/// <summary>
/// 有内容时的显示效果
/// </summary>
private void HaveTextShow()
{
txtInfo.Font = Font;
txtInfo.ForeColor = Color.Black;
}
/// <summary>
/// 当文本框为空时,显示的内容。
/// </summary>
[Description("当文本框为空时,显示的内容。")]
public string EmptyText
{
get { return _EmptyText; }
set
{
_EmptyText = value;
EmptyShow();
}
}
private string _text = "";
/// <summary>
///
/// </summary>
[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);
}
/// <summary>
/// 选择文本框中的文本范围
/// </summary>
/// <param name="start">文本框中当前选定文本的第一个字符的位置。</param>
/// <param name="length">要选择的字符数</param>
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; }
}
}
}