原来是作这个啊,你先参考一下这个,回头帮你调试一下.
C# Validating TextBox Control
http://www.codeproject.com/cs/miscctrl/ValidatingTextBox.aspMasked C# TextBox Control
http://www.codeproject.com/cs/miscctrl/maskedcsedit.asp
C# Validating TextBox Control
http://www.codeproject.com/cs/miscctrl/ValidatingTextBox.aspMasked C# TextBox Control
http://www.codeproject.com/cs/miscctrl/maskedcsedit.asp
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;namespace WindowsApplication7
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private WindowsApplication7.UserControl1 userControl11;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.userControl11 = new WindowsApplication7.UserControl1();
this.SuspendLayout();
//
// userControl11
//
this.userControl11.GetNumb1 = 1;
this.userControl11.GetNumb2 = 1;
this.userControl11.Location = new System.Drawing.Point(96, 128);
this.userControl11.Name = "userControl11";
this.userControl11.Size = new System.Drawing.Size(216, 20);
this.userControl11.TabIndex = 3;
this.userControl11.Text = "?-?";
this.userControl11.UseStyle = true;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(472, 293);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.userControl11});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace WindowsApplication7
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class UserControl1 : System.Windows.Forms.TextBox
{
private bool isuseme = true;
private int num1 = 1;
private int num2 = 1;
private bool flag=false;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public UserControl1()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
SetStyle(ControlStyles.UserPaint,true);
InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化 } /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Component Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// UserControl1
//
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(336, 192); } protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
if((Convert.ToInt32 (e.KeyChar)<48)||(Convert.ToInt32 (e.KeyChar)>57))
{
flag=true;
System.Windows .Forms .MessageBox .Show ("请您输入一个数字!","提示");
}
} protected override void OnTextChanged(System.EventArgs e)
{
if(flag)
{
flag=false;
this.Text =this.Text.Remove (this.TextLength -1,1);
this.SelectionStart =0;
this.SelectionLength =this.Text .Length ;
this.Cut ();
this.Paste ();
}
} protected override void OnPaint(PaintEventArgs pe)
{
if(isuseme==true)
{
this.Text ="";
for(int i=0;i<num1;i++)
{
this.Text =this.Text +"?";
}
this.Text =this.Text +"-";
for(int i=0;i<num2;i++)
{
this.Text =this.Text +"?";
}
}
isuseme=false;
} public bool UseStyle
{
get
{
return isuseme;
}
set
{
isuseme=value;
}
}
public int GetNumb1
{
get
{
return num1;
}
set
{
num1=value;
}
}
public int GetNumb2
{
get
{
return num2;
}
set
{
num2=value;
}
}
#endregion
}
}
与namespace的命名的关吗?
你的代码没有注释,我也不知道你要什么效果.改成这个试验一下.
SetStyle(ControlStyles.AllPaintingInWmPaint, true);上面的两个连接,是实现验证的控件.我开始以为你是在作这个东西.如果看到,请详细说说,特别是你要什么效果,我才好知道什么是对错啊.