C
Claire
Hi,
I have a form with 3 textboxes.
My code is below (short working example from vs 2005).
The value "_cm" is the only stored data, km and miles are calculated values.
The text boxes work fine until an invalid value is entered. In that case,
the incorrect value is displayed in the text box rather than the correct
value stored in _cm.
I wanted to use this method in my "real" application for conversions between
km/miles but I can't get it to work.
What am I doing wrong please?
Claire
public partial class Form1 : Form
{
int _cm = 1000;
public int Cm
{
get {
return _cm;
}
set {
if ((value >= 0)&&(value <= 20000))
_cm = value;
}
}
public double km
{
get
{
return _cm / 100000.0f;
}
set
{
Cm = (int)(value * 100000.0f);
}
}
public double miles
{
get
{
return _cm * 0.000006213711922373339;
}
set
{
Cm = (int)(value / 0.000006213711922373339);
}
}
public Form1()
{
InitializeComponent();
textBox1.DataBindings.Add(new Binding("Text", this, "Cm", true,
DataSourceUpdateMode.OnValidation, null, "N3"));
textBox2.DataBindings.Add(new Binding("Text", this, "miles", true,
DataSourceUpdateMode.OnValidation, null, "N3"));
textBox3.DataBindings.Add(new Binding("Text", this, "km", true,
DataSourceUpdateMode.OnValidation, null, "N3"));
}
}
I have a form with 3 textboxes.
My code is below (short working example from vs 2005).
The value "_cm" is the only stored data, km and miles are calculated values.
The text boxes work fine until an invalid value is entered. In that case,
the incorrect value is displayed in the text box rather than the correct
value stored in _cm.
I wanted to use this method in my "real" application for conversions between
km/miles but I can't get it to work.
What am I doing wrong please?
Claire
public partial class Form1 : Form
{
int _cm = 1000;
public int Cm
{
get {
return _cm;
}
set {
if ((value >= 0)&&(value <= 20000))
_cm = value;
}
}
public double km
{
get
{
return _cm / 100000.0f;
}
set
{
Cm = (int)(value * 100000.0f);
}
}
public double miles
{
get
{
return _cm * 0.000006213711922373339;
}
set
{
Cm = (int)(value / 0.000006213711922373339);
}
}
public Form1()
{
InitializeComponent();
textBox1.DataBindings.Add(new Binding("Text", this, "Cm", true,
DataSourceUpdateMode.OnValidation, null, "N3"));
textBox2.DataBindings.Add(new Binding("Text", this, "miles", true,
DataSourceUpdateMode.OnValidation, null, "N3"));
textBox3.DataBindings.Add(new Binding("Text", this, "km", true,
DataSourceUpdateMode.OnValidation, null, "N3"));
}
}