NumericUpDown seems to be only dealing with integers. How can I modify it (?), so I can use doubles as Value and Increment?
A:
There is a property called DecimalPlaces. Set it to something grater than 0 and it will allow you to work with decimals
Michael D.
2009-08-24 20:15:21
Doesn't work on CF.
Reed Copsey
2009-08-24 20:17:09
There seems to be no 'DecimalPlaces' in the CompactFramework
2009-08-24 20:17:38
oops, sorry didn't notice __CF__
Michael D.
2009-08-24 20:18:14
+1 to counteract the downvote. Compact-framework wasn't one of the original tags. Jeez people.
MusiGenesis
2009-08-24 20:22:01
@MusiGenesis: No, but it WAS in the original title of the question :)
Reed Copsey
2009-08-24 23:46:52
@Reed: I didn't see it there, either. Also, people asking CF questions usually don't put WinForms in the title or tags, so I assumed this was full .Net too.
MusiGenesis
2009-08-25 12:22:42
+2
A:
NumericUpDown works with decimal types, but is integer only on the compact framework. This is a limitation of the class on CF.
There is, however, a CodeProject UserControl that provides an implementation for CF.
Reed Copsey
2009-08-24 20:16:59
A:
I just use a textbox, then override the OnKeyPress event. This code has worked for me in the past, but is only good for groups that write 1234.56, not 1234,56.
public partial class NumberTextBox : TextBox
{
public NumberTextBox()
{
InitializeComponent();
}
public decimal Value
{
get
{
try
{
return decimal.Parse(Text);
}
catch (Exception)
{
return -1;
}
}
}
public int ValueInt
{
get { return int.Parse(Text); }
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (this).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
base.OnKeyPress(e);
}
public void AppendString(string value)
{
if (string.IsNullOrEmpty(value))
{
Text = string.Empty;
}
else
{
if (value == "." && Text.IndexOf('.') > -1)
return;
Text += value;
}
}
}
Chris Brandsma
2009-08-25 15:14:11