我正在尝试创建一个屏蔽的TextBox,用于获取类似于IP地址的输入。我不想使用第三方控件,并希望开发自己的。有人可以指导我朝正确的方向发展吗?
我设法将键盘输入限制为数字。如何添加(..)蒙版?
答案 0 :(得分:1)
如果你非常热衷于开发自己的控制器。您可能必须处理键事件并根据指定的掩码
呈现值这是wpf toolkit中类似的蒙版文本框
此链接提供完整的实现,您可以查看并开发自己的
答案 1 :(得分:0)
我很惊讶没有最新的东西,或者至少有一半不适合使用c#中的Windows Forms来解决这个问题,甚至可以在没有返回到Windows 3.1语法,cmds,msgs的情况下将其打造成可维护的东西。 lpcstrs。导入dll等。
我用4个文本框,3个位图(点)MSDN应用笔记和一些关键事件处理程序创建了一个控件。拒绝非数字并不难,在插入模式下将文本字符串截断为三个字符并不难,但需要一些试验和错误才能找到允许在输入和制表键按下的文本框之间平滑过渡的组合。获取最后一个文本框以将焦点转移到表单中的下一个控件时有点令人沮丧。
与往常一样,tab和enter的处理方式不同,并且在key down和keypress之间做了一些事情 - 我不得不猜测发生了什么,因为我没有任何花哨的步骤源库。
无论如何,这是我的解决方案。我昨天写了它,今天进行了测试。我明天有一个演示,祝我好运。
using System;
using System.Drawing;
using System.Net;
using System.Windows.Forms;
using Framework.UserApplication.Utility;
namespace Framework.UserApplication.CommonDialogs
{
/**************************************************************************************************
* A TCP IP input mask.
* sealed because of virtual member calls in constructor
* By having a virtual call in an object's constructor you are introducing the possibility that
* inheriting objects will execute code before they have been fully initialized.
**************************************************************************************************/
public sealed partial class TcpIpInputMaskType : UserControl
{
private bool _hasFocus; // True if this TcpIpInputMaskType has focus
private IPAddress _ipAddress; // The IP address
private bool _showFocusRect; // True to show, false to hide the focus rectangle
/**************************************************************************************************
* Default constructor.
**************************************************************************************************/
public TcpIpInputMaskType()
{
_showFocusRect = ShowFocusCues;
_hasFocus = false;
InitializeComponent();
ChangeUICues += IpTextBoxes_ChangeUiCues;
BackColor = Color.Transparent;
AutoSize = true;
}
/**************************************************************************************************
* Gets the IP address.
*
* @return The IP address.
**************************************************************************************************/
public IPAddress IpAddress
{
get
{
try
{
_ipAddress = new IPAddress(new[]
{
Convert.ToByte(IP1.Text), Convert.ToByte(IP2.Text),
Convert.ToByte(IP3.Text), Convert.ToByte(IP4.Text)
});
}
catch (Exception e)
{
_ipAddress = null;
Console.WriteLine(e);
}
return _ipAddress;
}
}
/**************************************************************************************************
* Gets the IP address hex string.
*
* @return The IP hex address string.
**************************************************************************************************/
public string IpAddressHexString
{
get
{
if (IpAddress == null)
return "";
var barry = new[]
{
Convert.ToByte(IP1.Text),
Convert.ToByte(IP2.Text),
Convert.ToByte(IP3.Text),
Convert.ToByte(IP4.Text)
};
return AsciiHexConversionType.ByteArrayToHexWithSpaces(barry);
}
}
/**************************************************************************************************
* Gets the dotted IP address string.
*
* @return The dotted IP address string.
**************************************************************************************************/
public string DottedIpAddressString
{
get
{
if (_ipAddress == null)
return "";
return IpAddress.ToString();
}
}
/**************************************************************************************************
* Gets the ulong IP address.
*
* @return The ulong IP address.
**************************************************************************************************/
public ulong UlongIpAddress
{
get
{
if (IpAddress == null)
return 0;
var address = (ulong) (Convert.ToByte(IP1.Text) * (256 ^ 3)) +
(ulong) (Convert.ToByte(IP2.Text) * (256 ^ 2)) +
(ulong) (Convert.ToByte(IP3.Text) * 256) + Convert.ToByte(IP4.Text);
return address;
}
}
/**************************************************************************************************
* Change focus.
*
* @param ipTextBox The IP control.
**************************************************************************************************/
private void ChangeFocus(TextBox ipTextBox)
{
switch (ipTextBox.Name)
{
case "IP1":
IP2.Select();
break;
case "IP2":
IP3.Select();
break;
case "IP3":
IP4.Select();
break;
case "IP4":
ipTextBox.SelectNextControl(ActiveControl, true, true, true, true);
break;
}
}
/**************************************************************************************************
* Event handler. Called by IP1 for key press events.
*
* @param sender Source of the event.
* @param e Key press event information.
**************************************************************************************************/
private void IP1_KeyPress(object sender, KeyPressEventArgs e)
{
var ipTextBox = sender as TextBox;
switch (e.KeyChar)
{
case '\r':
SendKeys.Send("{TAB}");
e.Handled = true;
return;
case '\t':
if (ipTextBox != null)
ChangeFocus(ipTextBox);
e.Handled = true;
break;
default:
if (!char.IsDigit(e.KeyChar))
e.Handled = true;
break;
}
}
/**************************************************************************************************
* Event handler. Called by IP1 for text changed events.
*
* @param sender Source of the event.
* @param e Event information.
**************************************************************************************************/
private void IP1_TextChanged(object sender, EventArgs e)
{
var ipTextBox = sender as TextBox;
if (ipTextBox != null && ipTextBox.TextLength == 4)
{
ipTextBox.Text = ipTextBox.Text.Substring(0, 3);
return;
}
if (ipTextBox != null && ipTextBox.TextLength == 3)
ChangeFocus(ipTextBox);
}
/**************************************************************************************************
* Event handler. Called by IP4 for key down events.
*
* @param sender Source of the event.
* @param e Key event information.
**************************************************************************************************/
private void IP4_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{TAB}");
e.Handled = true;
e.SuppressKeyPress = true;
}
}
/**************************************************************************************************
* Event handler. Called by IpTextBoxes for change user interface cues events.
*
* @param sender Source of the event.
* @param e User interface cues event information.
**************************************************************************************************/
private void IpTextBoxes_ChangeUiCues(object sender, UICuesEventArgs e)
{
_showFocusRect = e.ShowFocus;
}
/**************************************************************************************************
* Raises the <see cref="E:System.Windows.Forms.Control.Enter" />
* event.
*
* @param e An <see cref="T:System.EventArgs" />
* that contains the event data.
**************************************************************************************************/
protected override void OnEnter(EventArgs e)
{
Invalidate();
}
/**************************************************************************************************
* Raises the <see cref="E:System.Windows.Forms.Control.Leave" />
* event.
*
* @param e An <see cref="T:System.EventArgs" />
* that contains the event data.
**************************************************************************************************/
protected override void OnLeave(EventArgs e)
{
Invalidate();
}
/**************************************************************************************************
* Raises the <see cref="E:System.Windows.Forms.Control.LostFocus" />
* event.
*
* @param e An <see cref="T:System.EventArgs" />
* that contains the event data.
**************************************************************************************************/
protected override void OnLostFocus(EventArgs e)
{
_hasFocus = false;
}
/**************************************************************************************************
* Raises the <see cref="E:System.Windows.Forms.Control.Paint" />
* event.
*
* @param e A <see cref="T:System.Windows.Forms.PaintEventArgs" />
* that contains the event data.
**************************************************************************************************/
protected override void OnPaint(PaintEventArgs e)
{
var focusRect = ClientRectangle;
focusRect.Inflate(-2, -2);
if (_hasFocus && _showFocusRect)
ControlPaint.DrawFocusRectangle(e.Graphics, focusRect, ForeColor, BackColor);
else
e.Graphics.DrawRectangle(new Pen(BackColor, 1), focusRect);
base.OnPaint(e);
}
/**************************************************************************************************
* Sets default IP address.
*
* @param ip1 The first IP.
* @param ip2 The second IP.
* @param ip3 The third IP.
* @param ip4 The fourth IP.
*
* @return The IPAddress.
**************************************************************************************************/
public IPAddress SetDefaultIpAddress(string ip1, string ip2, string ip3, string ip4)
{
IP1.Text = ip1;
IP2.Text = ip2;
IP3.Text = ip3;
IP4.Text = ip4;
return IpAddress;
}
}
}
这是设计师代码
namespace Framework.UserApplication.CommonDialogs
{
sealed partial class TcpIpInputMaskType
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox5 = new System.Windows.Forms.PictureBox();
this.pictureBox4 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.IP2 = new System.Windows.Forms.TextBox();
this.IP3 = new System.Windows.Forms.TextBox();
this.IP4 = new System.Windows.Forms.TextBox();
this.IP1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox5)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
this.SuspendLayout();
//
// pictureBox5
//
this.pictureBox5.Image = global::Framework.Properties.Resources.dot1;
this.pictureBox5.Location = new System.Drawing.Point(87, 26);
this.pictureBox5.Name = "pictureBox5";
this.pictureBox5.Size = new System.Drawing.Size(5, 5);
this.pictureBox5.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox5.TabIndex = 91;
this.pictureBox5.TabStop = false;
//
// pictureBox4
//
this.pictureBox4.Image = global::Framework.Properties.Resources.dot1;
this.pictureBox4.Location = new System.Drawing.Point(57, 25);
this.pictureBox4.Name = "pictureBox4";
this.pictureBox4.Size = new System.Drawing.Size(5, 5);
this.pictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox4.TabIndex = 90;
this.pictureBox4.TabStop = false;
//
// pictureBox3
//
this.pictureBox3.Image = global::Framework.Properties.Resources.dot1;
this.pictureBox3.Location = new System.Drawing.Point(29, 25);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(5, 5);
this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox3.TabIndex = 89;
this.pictureBox3.TabStop = false;
//
// IP2
//
this.IP2.Location = new System.Drawing.Point(32, 5);
this.IP2.Name = "IP2";
this.IP2.Size = new System.Drawing.Size(27, 20);
this.IP2.TabIndex = 86;
this.IP2.TextChanged += new System.EventHandler(this.IP1_TextChanged);
this.IP2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IP1_KeyPress);
//
// IP3
//
this.IP3.Location = new System.Drawing.Point(61, 5);
this.IP3.Name = "IP3";
this.IP3.Size = new System.Drawing.Size(27, 20);
this.IP3.TabIndex = 87;
this.IP3.TextChanged += new System.EventHandler(this.IP1_TextChanged);
this.IP3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IP1_KeyPress);
//
// IP4
//
this.IP4.Location = new System.Drawing.Point(90, 5);
this.IP4.Name = "IP4";
this.IP4.Size = new System.Drawing.Size(27, 20);
this.IP4.TabIndex = 88;
this.IP4.TextChanged += new System.EventHandler(this.IP1_TextChanged);
this.IP4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IP1_KeyPress);
//
// IP1
//
this.IP1.Location = new System.Drawing.Point(3, 5);
this.IP1.Name = "IP1";
this.IP1.Size = new System.Drawing.Size(27, 20);
this.IP1.TabIndex = 85;
this.IP1.TextChanged += new System.EventHandler(this.IP1_TextChanged);
this.IP1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.IP4_KeyDown);
this.IP1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IP1_KeyPress);
//
// TcpIpInputMaskType
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.BackColor = System.Drawing.Color.Transparent;
this.Controls.Add(this.pictureBox5);
this.Controls.Add(this.pictureBox4);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.IP2);
this.Controls.Add(this.IP3);
this.Controls.Add(this.IP4);
this.Controls.Add(this.IP1);
this.Name = "TcpIpInputMaskType";
this.Size = new System.Drawing.Size(126, 31);
((System.ComponentModel.ISupportInitialize)(this.pictureBox5)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox pictureBox5;
private System.Windows.Forms.PictureBox pictureBox4;
private System.Windows.Forms.PictureBox pictureBox3;
private System.Windows.Forms.TextBox IP2;
private System.Windows.Forms.TextBox IP3;
private System.Windows.Forms.TextBox IP4;
private System.Windows.Forms.TextBox IP1;
}
}
这是我项目中的“dot1”,背景是透明的。当它背后有多个东西时,它有点暗淡,但你可以匹配设计师的颜色,没问题。
如果我碰到任何东西,我会回来编辑这篇文章。
答案 2 :(得分:0)
****这是完美的方法。** 有一个本地Win32控件支持IP地址的处理
public class IPTextBox : TextBox
{
public IPTextBox() : base()
{
}
protected override CreateParams CreateParams
{
get
{
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
CreateParams cp = base.CreateParams;
cp.ClassName = "SysIPAddress32";
return cp;
}
}
}
答案 3 :(得分:-1)
Windows WPF已包含此功能。无需重新发明轮子。
需要进口:
System.Net.IPAddress
System.Windows.Forms.MaskedTextBox
现在,您需要设置属性:
MaskedTextBox.Mask = ###.###.###.###
MaskedTextBox.ValidatingType = typeof(System.Net.IPAddress);