Thursday, November 1, 2007

lazy evaluation

Lazy evaluation is known as the ability to evaluate expression only when it is later needed.

Like lazy binding we don't bind the data in the first place when we set the datasource like in the case of a combo box we wait wait until the user actually tries to drop down the combo box.
So you have the effect that the user sees an empty combo box in the beginning. The deleting of already selected items works nearly similar. In that case (if the user press DEL or BACK button), I remove the databinding of the combo box (but still remember the datasource). The code itself is really simple:

The only interesting method is the one that does the data binding:

public void SetDataBinding(IList dataSource, object objectToModify,
string propertyName, string displayMember)
{
// init combo box and delete all databinding stuff
this.DisplayMember = String.Empty;
this.Items.Clear();
this.ValueMember = String.Empty;
this.Text = String.Empty;

// init private fields
this.mDataSource = dataSource;
this.mObjectToModify = objectToModify;
this.mPropertyName = propertyName;
this.mProperty =
this.mObjectToModify.GetType().GetProperty(this.mPropertyName);
this.mDisplayMember = displayMember;
this.mNullableMode = true;

// get selected item
object selectedItem =
this.mProperty.GetValue(this.mObjectToModify, null);

// if not null, bind to it
if (selectedItem != null)
{
this.DataSource = this.mDataSource;
this.SelectedItem = selectedItem;
}
// do nothing and set datasource to null
else
this.DataSource = null;
}
The drop down event:
protected override void OnDropDown(EventArgs e)
{

// if no datasource is set, set it
if (this.mNullableMode && this.mDataSource
!= null && this.DataSource == null)
this.DataSource = this.mDataSource;

base.OnDropDown(e);
}



And the key down event:

protected override void OnKeyDown(KeyEventArgs e)
{
// if DEL or BACK is pressed set property
//to null and data source to null
if (this.mNullableMode && (e.KeyCode ==
Keys.Delete e.KeyCode == Keys.Back))
{
// next line is very important:
// without you may get an OutOfRangeException
this.DroppedDown = false;
this.DataSource = null;
}

base.OnKeyDown(e);
}