如果操作者输入错误可以报错!谢谢!
解决方案 »
- treeview使用OwnerDrawText,drawNode后,修改节点闪烁厉害
- 怎么来存放和组织常量?
- 求助,关于connectionstring的头疼问题。
- 请问timer组件是怎么用的
- 在sql数据库中,以缩略图的方式将多张图片显示在一个容器中(winform)
- 怎样记录C#进程退出的原因??
- 关于C/S结构做网站后台的几个问题?
- .NET 多线程发送WebRequest访问失败,异常信息:"WebException,基础连接已关闭:连接意外关闭。"(ServicePointManager最大并发数已修改) ,请做过这方面的专家指点一二。
- 怎样才能完全退出程序
- 问一个关于字符串命名的问题?
- 怎么样获取文件图标
- 问一下在的各位!
DateTimePicker不行的,相当于给文本框做个输入时间的掩码格式!
{
Test()
{
DateTimePicker dtp = new DateTimePicker();
dtp.Parent = this;
dtp.Width = 140;
dtp.CustomFormat = "yyyy.MM.dd HH:mm:ss";
dtp.Format = DateTimePickerFormat.Custom;
dtp.ShowUpDown = true;
} static void Main()
{
Application.Run(new Test());
}
}
在网上挺多得!搜索日期控件!
2.将textbox设置成定长就是上面这个字符串得长度.
3.在textbox得keypress事件里处理
屏蔽backspace键,屏蔽字母键.
当光标不在格式字符位置时,可以输入数字,在格式符上时,光标再往后移动.
输入的字符插入在原来的text里面.哦,这个方法要能获取光标的位置,不知能否实现,先试试去.
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;namespace NullableDateControls
{
/// <summary>
/// 类名:MaskDateEdit
/// 功能:可按掩码输入日期,提供下拉选择
/// 作者:魏孙鼎
/// </summary>
public class MaskDateEdit: TextBox
{
private string _Mask = "yyyy年MM月dd日hh时mm分ss秒";
private Button button;
private MonthCalendar mc; public MaskDateEdit()
{
button = new Button();
BindButton();
} /// <summary>
/// 设置相应的输入掩码,如:yyyy年MM月dd日
/// </summary>
public string Mask
{
get { return _Mask;}
set { _Mask = value; this.GetMaskChars();}
} private void BindButton()
{
if (!this.Contains(button))
{
this.Controls.Add(button);
button.TabStop = false;
button.Click += new EventHandler(button_Click);
} button.BringToFront(); button.Text = "6";
button.Font = new Font("Webdings",10);
button.BackColor = Color.FromKnownColor(System.Drawing.KnownColor.ControlLightLight);
button.TextAlign = ContentAlignment.MiddleCenter;
button.FlatStyle = FlatStyle.System;
button.Width = 16;
button.Height = this.Height - 4;
button.Left = this.Width - button.Width - 4;
} private string GetMaskChars()
{
return this.Mask.Replace("y"," ")
.Replace("m"," ")
.Replace("d"," ")
.Replace("Y"," ")
.Replace("M"," ")
.Replace("D"," ")
.Replace("#"," ")
.Replace("h"," ")
.Replace("m"," ")
.Replace("s"," ");
} public override string Text
{
get
{
if (base.Text == this.GetMaskChars() || base.Text == null)
{
return "";
}
else
{
return base.Text;
}
}
set
{
if(value == null || value == "")
{
base.Text = GetMaskChars();
}
else
{
base.Text = value;
}
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
char[] chars;
int j;
int i = this.SelectionStart;
if (keyData == Keys.Left || keyData == Keys.Right ||
keyData == Keys.Enter || keyData == Keys.Tab)
{
return base.ProcessCmdKey(ref msg, keyData);
} if (keyData == Keys.Delete)
{
this.Text = null;
return true;
} //退格删除
if (keyData == Keys.Back)
{
if (i == 0) return true; chars = base.Text.ToCharArray();
for(j = i-1; j>=0;j--)
{
if( chars[j] == ' ' || IsDigit(chars[j]) )
{
chars[j] = ' ';
break;
}
} string s= "";
foreach(char c in chars)
{
s += c;
}
base.Text = s;
this.SelectionStart = j;
}
//输入数字日期
if ( IsDigit(keyData) )
{
if (i >= this.Text.Length ) i = 0;
this.SelectionLength = 0; chars = base.Text.ToCharArray();
for(j = i; j<base.Text.Length; j++)
{
if( chars[j] == ' ' || IsDigit(chars[j]) )
{
chars[j] = GetDigit(keyData);
break;
}
}
string s= "";
foreach(char c in chars)
{
s += c;
}
base.Text = s;
this.SelectionStart = j + 1;
}
//拦截其它所有键
return true;
} char GetDigit(Keys keyData)
{
int code = (int)keyData;
if (keyData >= Keys.D0 && keyData <=Keys.D9)
{
code -= (int)Keys.D0;
} if (keyData >= Keys.NumPad0 && keyData <=Keys.NumPad9)
{
code -= (int)Keys.NumPad0;
} return char.Parse(code.ToString());;
} bool IsDigit(Keys keyData)
{
return (keyData >= Keys.D0 && keyData <=Keys.D9 ) ||
(keyData >= Keys.NumPad0 && keyData <=Keys.NumPad9);
} bool IsDigit(char c)
{
return (c <= '9') && (c>='0');
}
protected override void OnEnter(EventArgs e)
{
this.SelectionLength = 0;
this.SelectionStart = 0;
base.OnEnter (e);
} protected override void OnSizeChanged(EventArgs e)
{
BindButton();
base.OnSizeChanged (e);
} protected override void OnLocationChanged(EventArgs e)
{
BindButton();
base.OnLocationChanged (e);
}
private void button_Click(object sender, EventArgs e)
{
if (mc != null)
{
mc.Dispose();
mc = null;
}
mc = new MonthCalendar();
this.Parent.Controls.Add(mc); this.Parent.Click += new EventHandler(Parent_Click);
mc.LostFocus += new EventHandler(mc_LostFocus);
mc.DateSelected += new DateRangeEventHandler(mc_DateSelected);
mc.Focus();
mc.Visible = true;
mc.BringToFront(); mc.Left = this.Left; if (this.Parent.Height - this.Bottom < mc.Height )
{
//显示在控件上面
mc.Top = this.Top - mc.Height;
}
else
{
//显示在控件的下面
mc.Top = this.Bottom;
}
} private void mc_LostFocus(object sender, EventArgs e)
{
mc.Visible = false;
} private void mc_DateSelected(object sender, DateRangeEventArgs e)
{
this.Text = mc.SelectionEnd.ToString(this.Mask);
this.Focus();
mc.Visible = false;
} protected override void OnLostFocus(EventArgs e)
{
CheckDate();
base.OnLostFocus (e);
} private bool CheckDate()
{
bool bValid = true; if (base.Text == this.GetMaskChars())
{
return true;
} try
{
DateTime.Parse(base.Text);
}
catch
{
bValid = false;
} if (!bValid)
{
base.Text = this.GetMaskChars();
} return bValid;
} private void Parent_Click(object sender, EventArgs e)
{
mc.Visible = false;
} protected override void Dispose(bool disposing)
{
if (mc != null) mc.Dispose();
if (button != null) button.Dispose();
base.Dispose (disposing);
} }
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if((e.KeyChar==8) && (e.KeyChar<48 || e.KeyChar>57))
{
e.Handled=true;
}
else
{
if(this.textBox1.SelectionStart==4 || this.textBox1.SelectionStart==7 || this.textBox1.SelectionStart==10 || this.textBox1.SelectionStart==13 || this.textBox1.SelectionStart==16)
{
this.textBox1.SelectionStart=this.textBox1.SelectionStart+1;
this.textBox1.SelectionLength=1;
e.Handled=true;
}
else
{
}
}
} private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
this.textBox2.Text=e.KeyValue.ToString();
if(e.KeyValue==37 || e.KeyValue==38 || e.KeyValue==39 || e.KeyValue==40 //屏蔽方向键
|| e.KeyValue==46 ) //屏蔽del键
{
e.Handled=true;
}
} private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{ this.textBox1.SelectionLength=1;
if(this.textBox1.SelectionStart==4 || this.textBox1.SelectionStart==7 || this.textBox1.SelectionStart==10 || this.textBox1.SelectionStart==13 || this.textBox1.SelectionStart==16)
{
this.textBox1.SelectionStart=this.textBox1.SelectionStart+1;
this.textBox1.SelectionLength=1;
} if(this.textBox1.SelectionStart>18)
{
this.textBox1.SelectionStart=0;
this.textBox1.SelectionLength=1;
} }
那位有webform下的提供!
using System;
using System.Web.UI;
using System.Web.UI.WebControls;namespace OverRed.RichTextBox
{
/// <summary>
/// 自动处理换行的TextBox
/// </summary>
public class RichTextBox : System.Web.UI.WebControls.TextBox
{
/// <summary>
/// 重写的Text属性,获得经过处理的文本
/// </summary>
public override string Text
{
get
{
return format(base.Text);
}
set
{
base.Text = value;
}
}//字符转换
private string format(string msg)
{
//这里是生成了三个全新的字符串,如果需要替换的文本太长,就会对性能产生不好的影响
msg=ms.Replace( " ", " " ).Replace( Convert.ToString( ( char ) 10 ), " " ).Replace( Convert.ToString( ( char ) 13 ), "<br>" );
return msg;
}
}
}1.打开.aspx的设计视图
2.找出工具箱,在上面点右键-选择“添加/删除项”
3.在弹出的窗口中点浏览,找到你需要的那个自定义控件的.dll
4.添加,完成
5.控件添加到工具箱后,就可以像其他server controls一样使用,在需要的地方拖过去就可以