The autocomplete box within Microsoft Visual Studio LightSwitch is an incredibly powerful tool that has many uses.
There are several settings for the type of matching that can be used, these include, ‘Starts with’, ‘Contains’ & ‘Equals’ Each of these can be case sensitive or insensitive.
The autocompletebox can be used for a variety of tasks within a Microsoft Visual Studio LightSwitch application, the first and probably most used place is to select data from a linked entity (where we may have in the past used a combobox), the advantage of this is we can filter the data by typing into the box, thereby not having to display all records at once, this post will not be dealing with these instances as the development environment deals with them automatically.
A second big use of the autocompletebox is for filter screens of data, often for reporting purposes. In these screen a grid can be filtered on the item selected in the autocompletebox, or even another autocompletebox so that further filtering can be achieved easily. These can be nested several layers deep to allow for faster filtering with each list being filtered on the previous selected values.
It is possible to disable the controls on the later levels of filtering, but not necessarily the easiest thing to do. To achieve this code needs to be bound to the ControlAvailable Event of the Autocompletebox, code similar to that shown below needs to be added to the Activated event of the screen.
this.FindControl("prpClient").ControlAvailable += new EventHandler(ClientListAvailable);
Within the ControlAvailable event code to bind to the LostFocus Event can then be added, along with other code, in the example below the control itself is bound to a class level object so it can be accessed later more easily.
private void ClientListAvailable(object sender, ControlAvailableEventArgs e)
{
((Control)e.Control).LostFocus += ClientList_LostFocus;
ClientBox = (AutoCompleteBox)e.Control;
} Actual code that needs to be run on the selected data in the autocompletebox can then be added in the lost focus event, in this case if a valid item is selected then the next filter list is enabled and if possible the third list too, if a non valid item is selected then the other lists are disabled again.
private void ClientList_LostFocus(object sender, RoutedEventArgs e)
{
if (ClientBox.SelectedItem != null)
{
ProjectBox.IsEnabled = true;
if (ProjectBox.SelectedItem != null)
{
BreakdownBox.IsEnabled = true;
}
else
{
BreakdownBox.IsEnabled = false;
}
}
else
{
ProjectBox.IsEnabled = false;
BreakdownBox.IsEnabled = false;
}
} As can be seen an autocompletebox in Microsoft Visual Studio LightSwitch can be a very useful control, that can reduce the amount of data entry that is required, and can be extremely useful for filtering purposes with multi level filters, with the autocompletebox being bound to a query property. Accessing the data from the autocompletebox isn’t necessarily the easiest thing, especially if you wish to access text that has been typed in that isn’t actually a valid entry. Control events do need to be bound if controls need to be enabled or disabled based on data selected, if only simple filtering is required then this doesn’t need to be done.
As with all areas of Visual Studio LightSwitch, simple things can be done very easily, however, it can be extended to attain more complex functionality.