上次一时兴起,做了一个仿金蝶的凭证金额输入的控件,今天实际运用了下,修改了几个操作上的问题,加强了点功能
主要修改如下
1、在金额修改的时候,实现整数位在输入“.”的时候可以自动跳转到小数位(红线后)并复写小数位
2、修改了小数位能输入其他非数字的元素
代码如下
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Voucher
{
[DefaultProperty("jText")]
[ToolboxData(@"<{0}:VoucherInput jText = ''
runat='server'></{0}:VoucherInput>")
]
//设置默认的属性
public class VoucherInput : WebControl
{
private TextBox _TextBox; //整数位
private TextBox _PTextBox; //小数位
属性#region 属性
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("金额")
]
public string jText
{
get
{
EnsureChildControls();
return _TextBox.Text + "." + _PTextBox.Text;
}
set
{
EnsureChildControls();
if (value == "" || value == null)
{
}
else
{
string[] Allstring = value.Split('.');
_TextBox.Text = Allstring[0];
_PTextBox.Text = Allstring[1];
}
}
}
#endregion Properties delegated to child controls
方法#region 方法
protected override void CreateChildControls()
{
Controls.Clear();
_TextBox = new TextBox();
_TextBox.ID = "TextBox1";
_TextBox.MaxLength = 13;
_TextBox.Width = 120;
_TextBox.Height = 20;
_TextBox.Attributes.Add("onkeypress", "keyPress()");
_TextBox.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url(text.gif)");
_TextBox.Style.Add(HtmlTextWriterStyle.Direction, "rtl");
_TextBox.Style.Add(HtmlTextWriterStyle.BorderWidth, "0");
_PTextBox = new TextBox();
_PTextBox.Width = 15;
_PTextBox.Height = 20;
_PTextBox.MaxLength = 2;
_PTextBox.ID = "PTextBox";
_PTextBox.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url(textPoint.gif)");
_PTextBox.Style.Add(HtmlTextWriterStyle.BorderWidth, "0");
_PTextBox.Attributes.Add("onFocus", "PFocus()");
_PTextBox.Attributes.Add("onkeypress", "PkeyPress()");
this.Controls.Add(_TextBox);
this.Controls.Add(_PTextBox);
}
protected override void Render(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
_TextBox.RenderControl(writer);
_PTextBox.RenderControl(writer);
writer.Write("<script language=javascript>function keyPress(){if(!(event.keyCode>=48&&event.keyCode<=57 ||event.keyCode==46 || event.keyCode==45)){event.keyCode = 0;}if(event.keyCode==46){event.keyCode = 0;document.getElementById('PTextBox').focus();}}</script>"); //对字符的控制
writer.Write("<script language=javascript>function PFocus(){ var box=document.getElementById('PTextBox'); var rng = box.createTextRange(); rng.collapse(true); rng.moveEnd('character',2); rng.moveStart('character',0); rng.select(); }</script>");
writer.Write("<script language=javascript>function PkeyPress(){if(event.keyCode==46){event.keyCode = 0;} if(!(event.keyCode>=48&&event.keyCode<=57 ||event.keyCode==46 || event.keyCode==45)){event.keyCode = 0;} }</script>");
}
#endregion Overriden methods
}
}
总结:老问题还是没解决,删金额的时候还是小数位和整数位分开删除的,主要原因是因为我现在不能通过光标来判断是否需要转到整数位