DataBinding Question

E

ESmith

I have a schema that defines a STRING variable: TimeLimit

On my data collection form, there are two numericUpDown controls - one for
hours, one for minutes.
Internally, the program does the conversion, hours * 60 + minutes to get the
TimeLimt in minutes.

Is it possible to DataBind in this situation? How (can) one create psuedo
field/OnChangedValue update so that when the user enters 1 in hours and 12
in minutes, that that value 72 is bound to the TimeLimit string, and
conversely, the value 72 updates the two numericUpDown controls to 1 and 12
respectively?

TIA!
 
M

Morten Wennevik [C# MVP]

I have a schema that defines a STRING variable: TimeLimit

On my data collection form, there are two numericUpDown controls - onefor
hours, one for minutes.
Internally, the program does the conversion, hours * 60 + minutes to get the
TimeLimt in minutes.

Is it possible to DataBind in this situation? How (can) one create psuedo
field/OnChangedValue update so that when the user enters 1 in hours and 12
in minutes, that that value 72 is bound to the TimeLimit string, and
conversely, the value 72 updates the two numericUpDown controls to 1 and 12
respectively?

TIA!

Hi ESmith,

You can solve this by binding each of the numeric controls to their own property and have those properties update a TimeLimit string/property. The code below should do what you seek, it will output the calculated time in label1

protected override void OnLoad(EventArgs e)
{
numericUpDown1.DataBindings.Add("Value", this, "Hours", false, DataSourceUpdateMode.OnPropertyChanged);
numericUpDown2.DataBindings.Add("Value", this, "Minutes", false, DataSourceUpdateMode.OnPropertyChanged);
label1.DataBindings.Add("Text", this, "Time");
}

private int hours;
private string time;
private int minutes;

public int Hours
{
get { return hours; }
set
{
hours = value;
Time = (hours * 60 + Minutes).ToString();
}
}

public int Minutes
{
get { return minutes; }
set
{
minutes = value;
Time = (Hours * 60 + minutes).ToString();
}
}

public string Time
{
get { return time; }
set { time = value; }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top