+ UIIPTextBox: 增加IP地址输入框

This commit is contained in:
Sunny 2022-01-29 16:30:00 +08:00
parent e63472dc5f
commit 95574c3f00
7 changed files with 530 additions and 29 deletions

Binary file not shown.

Binary file not shown.

View File

@ -48,6 +48,8 @@ namespace Sunny.UI.Demo
this.uiIntegerUpDown1 = new Sunny.UI.UIIntegerUpDown();
this.uiToolTip1 = new Sunny.UI.UIToolTip(this.components);
this.uiTextBox1 = new Sunny.UI.UITextBox();
this.uiLine6 = new Sunny.UI.UILine();
this.uiipTextBox1 = new Sunny.UI.UIIPTextBox();
this.SuspendLayout();
//
// uiTextBox6
@ -326,11 +328,39 @@ namespace Sunny.UI.Demo
this.uiTextBox1.Watermark = "水印文字";
this.uiTextBox1.ButtonClick += new System.EventHandler(this.uiTextBox1_ButtonClick);
//
// uiLine6
//
this.uiLine6.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine6.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine6.Location = new System.Drawing.Point(388, 211);
this.uiLine6.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine6.Name = "uiLine6";
this.uiLine6.Size = new System.Drawing.Size(306, 20);
this.uiLine6.TabIndex = 54;
this.uiLine6.Text = "UIIPTextBox";
this.uiLine6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// uiipTextBox1
//
this.uiipTextBox1.FillColor2 = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(243)))), ((int)(((byte)(255)))));
this.uiipTextBox1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiipTextBox1.Location = new System.Drawing.Point(388, 245);
this.uiipTextBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.uiipTextBox1.MinimumSize = new System.Drawing.Size(1, 1);
this.uiipTextBox1.Name = "uiipTextBox1";
this.uiipTextBox1.Padding = new System.Windows.Forms.Padding(1);
this.uiipTextBox1.Size = new System.Drawing.Size(150, 29);
this.uiipTextBox1.TabIndex = 55;
this.uiipTextBox1.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
this.uiipTextBox1.Value = null;
//
// FTextBox
//
this.AllowShowTitle = true;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(800, 562);
this.Controls.Add(this.uiipTextBox1);
this.Controls.Add(this.uiLine6);
this.Controls.Add(this.uiTextBox6);
this.Controls.Add(this.uiTextBox5);
this.Controls.Add(this.uiTextBox4);
@ -379,5 +409,7 @@ namespace Sunny.UI.Demo
private UIIntegerUpDown uiIntegerUpDown1;
private UIToolTip uiToolTip1;
private UITextBox uiTextBox1;
private UILine uiLine6;
private UIIPTextBox uiipTextBox1;
}
}

View File

@ -58,7 +58,7 @@ namespace Sunny.UI
public UIColorPicker()
{
InitializeComponent();
//ShowText = false;
ShowText = false;
Value = UIColor.Blue;
}

View File

@ -0,0 +1,426 @@
/******************************************************************************
* SunnyUI
* CopyRight (C) 2012-2022 ShenYongHua().
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 使
******************************************************************************
* : UIIPTextBox.cs
* : IP地址输入框
* : V3.1
* : 2022-01-29
*
* 2022-01-29: V3.1.0
******************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sunny.UI
{
public sealed partial class UIIPTextBox : UIPanel
{
private IPAddress _value;
public UIIPTextBox()
{
InitializeComponent();
SetStyleFlags();
ShowText = false;
UIIPTextBox_SizeChanged(null, null);
foreach (TextBox txt in Controls.OfType<TextBox>())
{
txt.AutoSize = false;
txt.PreviewKeyDown += Txt_PreviewKeyDown;
txt.KeyPress += Txt_KeyPress;
txt.TextChanged += Txt_TextChanged;
txt.Leave += Txt_Leave;
}
}
private void Txt_Leave(object sender, EventArgs e)
{
TextBox t = (TextBox)sender;
if (t.Text.IsNullOrEmpty()) return;
if (t.Text.ToInt() > 255) t.Text = "255";
if (t.Text.ToInt() < 0) t.Text = "0";
}
private void Txt_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox t = (TextBox)sender;
if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
{
e.Handled = true;
return;
}
if (t.SelectionStart == 2 && t.Text.Length == 2 && t.Text.ToInt() >= 26 && e.KeyChar != 8)
{
e.Handled = true;
return;
}
if (t.SelectionStart == 2 && t.Text.Length == 2 && t.Text.ToInt() == 25 && e.KeyChar != 8)
{
if (e.KeyChar > '5')
{
e.Handled = true;
return;
}
}
if (e.KeyChar == 8) return;
if (t.SelectionStart == 2) NextTextBox(t)?.Focus();
}
private void Txt_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
TextBox t = (TextBox)sender;
switch (e.KeyCode)
{
case Keys.OemPeriod:
case Keys.Decimal:
case Keys.Right:
if (t.Text.IsValid() && t.SelectionStart == t.TextLength) NextTextBox(t)?.Focus();
break;
case Keys.Left:
if (t.SelectionStart == 0) PrevTextBox(t)?.Focus();
break;
case Keys.Back:
if (t.SelectionStart == 0)
{
TextBox txtPrev = PrevTextBox(t);
if (txtPrev == null) return;
txtPrev.Focus();
if (txtPrev.TextLength > 0)
txtPrev.Text = txtPrev.Text.Remove(txtPrev.TextLength - 1, 1);
txtPrev.SelectionStart = txtPrev.TextLength;
}
break;
case Keys.Tab:
SelectNextControl(this, true, true, false, true);
break;
}
}
private TextBox PrevTextBox(TextBox box)
{
if (box == txt2) return txt1;
if (box == txt3) return txt2;
if (box == txt4) return txt3;
return null;
}
private TextBox NextTextBox(TextBox box)
{
if (box == txt1) return txt2;
if (box == txt2) return txt3;
if (box == txt3) return txt4;
return null;
}
/// <summary>
/// 填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
[Description("填充颜色,当值为背景色或透明色或空值则不填充"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "White")]
public new Color FillColor
{
get
{
return fillColor;
}
set
{
if (fillColor != value)
{
fillColor = value;
_style = UIStyle.Custom;
Invalidate();
}
AfterSetFillColor(value);
}
}
public new void Focus()
{
base.Focus();
txt1.Focus();
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
txt1.Focus();
}
public void Clear()
{
txt1.Clear();
txt2.Clear();
txt3.Clear();
txt4.Clear();
}
private void Txt_TextChanged(object sender, EventArgs e)
{
TextBox t = (TextBox)sender;
if (t.Text.IsNullOrEmpty()) return;
if (t.Text.ToInt() > 255) t.Text = "255";
if (t.Text.ToInt() < 0) t.Text = "0";
string strIp = $"{txt1.Text}.{txt2.Text}.{txt3.Text}.{txt4.Text}";
if (!IPAddress.TryParse(strIp, out IPAddress ip)) return;
_value = ip;
}
[Browsable(true)]
public new string Text
{
get => Value == null ? string.Empty : Value.ToString();
set
{
if (!IPAddress.TryParse(value, out IPAddress ip))
return;
Value = ip;
}
}
/// <summary>
/// 获取输入的IP地址
/// </summary>
public IPAddress Value
{
get => _value;
set
{
if (value == null)
return;
byte[] bytes = value.GetAddressBytes();
txt1.Text = bytes[0].ToString();
txt2.Text = bytes[1].ToString();
txt3.Text = bytes[2].ToString();
txt4.Text = bytes[3].ToString();
}
}
public override void SetStyleColor(UIBaseStyle uiColor)
{
base.SetStyleColor(uiColor);
if (uiColor.IsCustom()) return;
fillColor = uiColor.EditorBackColor;
foreColor = UIFontColor.Primary;
txt1.BackColor = Enabled ? fillColor : FillDisableColor;
txt1.ForeColor = UIFontColor.Primary;
txt2.BackColor = Enabled ? fillColor : FillDisableColor;
txt2.ForeColor = UIFontColor.Primary;
txt3.BackColor = Enabled ? fillColor : FillDisableColor;
txt3.ForeColor = UIFontColor.Primary;
txt4.BackColor = Enabled ? fillColor : FillDisableColor;
txt4.ForeColor = UIFontColor.Primary;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (TextBox txt in Controls.OfType<TextBox>().Where(t => t != txt1))
{
SizeF sf = e.Graphics.MeasureString(".", Font);
e.Graphics.DrawString(".", Font, ForeColor, txt.Left - 5 + 2.5f - sf.Width / 2.0f, txt.Top);
}
}
protected override void AfterSetForeColor(Color color)
{
base.AfterSetForeColor(color);
txt1.ForeColor = color;
txt2.ForeColor = color;
txt3.ForeColor = color;
txt4.ForeColor = color;
}
protected override void AfterSetFillColor(Color color)
{
base.AfterSetFillColor(color);
txt1.BackColor = Enabled ? color : FillDisableColor;
txt2.BackColor = Enabled ? color : FillDisableColor;
txt3.BackColor = Enabled ? color : FillDisableColor;
txt4.BackColor = Enabled ? color : FillDisableColor;
}
[DefaultValue(false)]
[Description("是否只读"), Category("SunnyUI")]
public bool ReadOnly
{
get => txt1.ReadOnly;
set
{
txt1.ReadOnly = value;
txt1.BackColor = Enabled ? FillColor : FillDisableColor;
txt2.ReadOnly = value;
txt2.BackColor = Enabled ? FillColor : FillDisableColor;
txt3.ReadOnly = value;
txt3.BackColor = Enabled ? FillColor : FillDisableColor;
txt4.ReadOnly = value;
txt4.BackColor = Enabled ? FillColor : FillDisableColor;
}
}
protected override void OnPaddingChanged(EventArgs e)
{
base.OnPaddingChanged(e);
UIIPTextBox_SizeChanged(null, null);
}
protected override void OnMouseDown(MouseEventArgs e)
{
ActiveControl = txt1;
}
private void InitializeComponent()
{
this.txt1 = new System.Windows.Forms.TextBox();
this.txt2 = new System.Windows.Forms.TextBox();
this.txt3 = new System.Windows.Forms.TextBox();
this.txt4 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txt1
//
this.txt1.BackColor = System.Drawing.Color.White;
this.txt1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txt1.ImeMode = System.Windows.Forms.ImeMode.Disable;
this.txt1.Location = new System.Drawing.Point(6, 4);
this.txt1.Margin = new System.Windows.Forms.Padding(3, 3, 10, 3);
this.txt1.MaxLength = 3;
this.txt1.Name = "txt1";
this.txt1.Size = new System.Drawing.Size(40, 22);
this.txt1.TabIndex = 0;
this.txt1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// txt2
//
this.txt2.BackColor = System.Drawing.Color.White;
this.txt2.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txt2.ImeMode = System.Windows.Forms.ImeMode.Disable;
this.txt2.Location = new System.Drawing.Point(58, 3);
this.txt2.Margin = new System.Windows.Forms.Padding(3, 3, 10, 3);
this.txt2.MaxLength = 3;
this.txt2.Name = "txt2";
this.txt2.Size = new System.Drawing.Size(40, 22);
this.txt2.TabIndex = 0;
this.txt2.TabStop = false;
this.txt2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// txt3
//
this.txt3.BackColor = System.Drawing.Color.White;
this.txt3.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txt3.ImeMode = System.Windows.Forms.ImeMode.Disable;
this.txt3.Location = new System.Drawing.Point(115, 3);
this.txt3.Margin = new System.Windows.Forms.Padding(3, 3, 10, 3);
this.txt3.MaxLength = 3;
this.txt3.Name = "txt3";
this.txt3.Size = new System.Drawing.Size(40, 22);
this.txt3.TabIndex = 0;
this.txt3.TabStop = false;
this.txt3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// txt4
//
this.txt4.BackColor = System.Drawing.Color.White;
this.txt4.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txt4.ImeMode = System.Windows.Forms.ImeMode.Disable;
this.txt4.Location = new System.Drawing.Point(163, 3);
this.txt4.Margin = new System.Windows.Forms.Padding(3, 3, 10, 3);
this.txt4.MaxLength = 3;
this.txt4.Name = "txt4";
this.txt4.Size = new System.Drawing.Size(40, 22);
this.txt4.TabIndex = 0;
this.txt4.TabStop = false;
this.txt4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// UIIPTextBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 21F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.txt4);
this.Controls.Add(this.txt3);
this.Controls.Add(this.txt2);
this.Controls.Add(this.txt1);
this.FillColor2 = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(243)))), ((int)(((byte)(255)))));
this.Name = "UIIPTextBox";
this.Padding = new System.Windows.Forms.Padding(1);
this.Size = new System.Drawing.Size(150, 29);
this.SizeChanged += new System.EventHandler(this.UIIPTextBox_SizeChanged);
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox txt1;
private System.Windows.Forms.TextBox txt2;
private System.Windows.Forms.TextBox txt3;
private System.Windows.Forms.TextBox txt4;
private void UIIPTextBox_SizeChanged(object sender, EventArgs e)
{
int w = (Width - RectSize * 2 - 28) / 4;
txt1.Width = txt2.Width = txt3.Width = txt4.Width = w;
txt1.Left = 8;
txt2.Left = txt1.Right + 5;
txt3.Left = txt2.Right + 5;
txt4.Left = txt3.Right + 5;
int MinHeight = txt1.PreferredHeight + RectSize * 2;
int MaxHeight = txt1.PreferredHeight * 2;
if (Height < MinHeight) Height = MinHeight;
if (Height > MaxHeight) Height = MaxHeight;
txt1.Height = Math.Min(Height - RectSize * 2, txt1.PreferredHeight);
txt2.Height = Math.Min(Height - RectSize * 2, txt2.PreferredHeight);
txt3.Height = Math.Min(Height - RectSize * 2, txt3.PreferredHeight);
txt4.Height = Math.Min(Height - RectSize * 2, txt4.PreferredHeight);
txt1.Top = txt2.Top = txt3.Top = txt4.Top = (Height - txt1.Height) / 2;
}
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
if (txt1 == null || txt2 == null || txt3 == null || txt4 == null) return;
txt1.Font = txt2.Font = txt3.Font = txt4.Font = Font;
UIIPTextBox_SizeChanged(null, null);
Invalidate();
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -53,13 +53,13 @@ namespace Sunny.UI
{
InitializeComponent();
SetStyleFlags();
CalcEditHeight();
Height = MinHeight;
ShowText = false;
Font = UIFontColor.Font();
Padding = new Padding(0);
MinimumSize = new Size(1, 16);
edit.AutoSize = false;
edit.Top = (Height - edit.Height) / 2;
edit.Left = 4;
edit.Width = Width - 8;
@ -98,6 +98,7 @@ namespace Sunny.UI
Controls.Add(edit);
fillColor = Color.White;
Width = 150;
Height = 29;
bar.Parent = this;
bar.Dock = DockStyle.None;
@ -467,7 +468,6 @@ namespace Sunny.UI
base.OnFontChanged(e);
edit.IsScaled = true;
edit.Font = Font;
CalcEditHeight();
SizeChange();
Invalidate();
}
@ -502,18 +502,8 @@ namespace Sunny.UI
}
}
private int MinHeight;
private int MaxHeight;
private void CalcEditHeight()
{
TextBox edt = new TextBox();
edt.Font = edit.Font.DPIScaleFont();
MinHeight = edt.PreferredHeight;
edt.BorderStyle = BorderStyle.None;
MaxHeight = edt.PreferredHeight * 2 + MinHeight - edt.PreferredHeight;
edt.Dispose();
}
int MinHeight;
int MaxHeight;
private void SizeChange()
{
@ -522,21 +512,14 @@ namespace Sunny.UI
if (!multiline)
{
if (Height < 12) Height = MinHeight;
MinHeight = edit.PreferredHeight + RectSize * 2;
MaxHeight = edit.PreferredHeight * 2;
if (Height < MinHeight) Height = MinHeight;
if (Height > MaxHeight) Height = MaxHeight;
if (Height < MinHeight)
{
edit.AutoSize = false;
edit.Height = Height - 3;
edit.Top = 2;
}
else
{
edit.AutoSize = true;
edit.Height = MinHeight;
edit.Top = (Height - edit.Height) / 2 + 1;
}
edit.Height = Math.Min(Height - RectSize * 2, edit.PreferredHeight);
edit.Top = (Height - edit.Height) / 2;
if (icon == null && Symbol == 0)
{