110,539
社区成员
发帖
与我相关
我的任务
分享
public partial class UDoubleInputBox : UserControl
{
private string _textValue;
private string _regexString;
private int _fieldDigit = 0;
private int _defaultValue = 0;
public int FieldDigit
{
get
{
return _fieldDigit;
}
set
{
_fieldDigit = value;
_regexString = @"^([0-9]\d*\.\d{0," + _fieldDigit.ToString() + @"}$)|^([0-9]\d*)$";
}
}
public int DefaultValue
{
get { return _defaultValue; }
set { _defaultValue = value; }
}
public string TextValue
{
get
{
//一旦控件失去焦点后,不管TextValue值有没有发生变化,都会执行这段代码
return _textValue;
}
set
{
if (_textValue == null ||
_textValue != value)
{
textBox1.Text = value;
}
}
}
public UDoubleInputBox()
{
InitializeComponent();
FieldDigit = 0;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty)
{
textBox1.Text = DefaultValue.ToString();
_textValue = DefaultValue.ToString();
return;
}
Match m = Regex.Match(textBox1.Text, _regexString);
if (!m.Success)
{
int selectionStart = textBox1.SelectionStart;
textBox1.Text = _textValue;
textBox1.SelectionStart = selectionStart;
}
else
{
_textValue = textBox1.Text;
}
}
}
public class ProductBaseInfo:INotifyPropertyChanged
{
private decimal productNetWeight;
[Display(Name = "产品净重")]
[Required]
[PlaceHolder(Name = "单个产品的重量")]
public Decimal ProductNetWeight{
get{
return productNetWeight;
}
set{
//一旦控件失去焦点后,不管TextValue值有没有发生变化,都会执行这段代码
productNetWeight = value;
NotifyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChanged([CallerMemberName] string propertyName=null)
{
if (null != PropertyChanged)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}