Create a ListBox Control that Displays Default Parameters
The remainder of the tutorial is concerned with displaying a complete list of default values for the parameter field in a ListBox control, and based on selections that you make from that ListBox control, refiltering the contents of the report.
In this section you learn how to populate the ListBox control from the default values of the parameter field.
Note
Remember that you set the Default Values, a large list of cities, when you created this report at the beginning of the tutorial.
To do this, you must add and configure a ListBox control, and then create a helper method to populate the ListBox control.
To create and configure a ListBox control on the form
- Open the Web or Windows Form.
- From the View menu, click Designer.
- If you are developing a Web Site, do the following:
- Click the CrystalReportViewer control to select it.
- Press the LEFT ARROW on your keyboard so that a flashing cursor appears, and then press ENTER.
- If you are developing a Windows project, do the following:
- Click the CrystalReportViewer control to select it.
- From the Properties window, set Dock to "Bottom."
- Resize the CrystalReportViewer control, so that you leave enough room above it for a ListBox control.
- From the Properties window, set Anchor to "Top, Bottom, Left, Right."
- From the Toolbox, drag a ListBox control above the CrystalReportViewer control.
- Click on the ListBox control to select it.
- From the Properties window, do the following:
- Set the ID or Name to "defaultParameterValuesList."
- Set the SelectionMode to "Multiple" (in a Windows project, "MultiExtended").
- From the File menu, select Save All.
To create a helper method that retrieves the default values from the parameter field
You are now ready to create a helper method that retrieves the default values from the parameter field.
Open the Web or Windows Form.
From the View menu, click Code.
At the bottom of the class, create a new private method named GetDefaultValuesFromParameterField() that returns an ArrayList instance, with ParameterFields passed into the method signature.
``` vb
Private Function GetDefaultValuesFromParameterField(ByVal
myParameterFields As ParameterFields) As ArrayList
End Function
```
``` csharp
private ArrayList
GetDefaultValuesFromParameterField(ParameterFields parameterFields)
{
}
```
Retrieve the ParameterField instance from the ParameterFields indexed class, which is based on the index entry of the PARAMETER_FIELD_NAME constant.
Dim myParameterField As ParameterField = myParameterFields(PARAMETER_FIELD_NAME)
ParameterField parameterField = parameterFields[PARAMETER_FIELD_NAME];
Retrieve a ParameterValues indexed class (as the variable defaultParameterValues) from the DefaultValues property of the ParameterField instance.
Dim defaultParameterValues As ParameterValues = myParameterField.DefaultValues
ParameterValues defaultParameterValues = parameterField.DefaultValues;
Declare and instantiate an ArrayList.
Dim myArrayList As ArrayList = New ArrayList()
ArrayList arrayList = new ArrayList();
Create a foreach loop, to retrieve each ParameterValue instance from defaultParameterValues.
Within the foreach loop, you now create a nested conditional block that checks for discrete (as opposed to range) parameter values. Two versions of this conditional block exist, because the API has changed slightly across versions of Crystal Reports for Visual Studio. Check your API (using IntelliSense) to see which property is available under ParameterValue:
``` vb
For Each myParameterValue As ParameterValue In
defaultParameterValues
Next
```
``` csharp
foreach(ParameterValue parameterValue in defaultParameterValues)
{
}
```
If the available property is IsRange then, within the foreach loop, enter this code:
If (Not myParameterValue.IsRange) Then End If
if(!parameterValue.IsRange) { }
Or, if the available property is Kind (DiscreteOrRangeKind, an enum with three values: DiscreteValue, RangeValue, DiscreteAndRangeValue) then, within the foreach loop, enter this code instead:
If (myParameterValue.Kind = DiscreteOrRangeKind.DiscreteValue) Then End If
if(parameterValue.Kind == DiscreteOrRangeKind.DiscreteValue) { }
Within this nested conditional block, cast the ParameterValue instance to its extended class, DiscreteParameterValue.
Dim myParameterDiscreteValue As ParameterDiscreteValue = CType(myParameterValue, ParameterDiscreteValue)
ParameterDiscreteValue parameterDiscreteValue = (ParameterDiscreteValue)parameterValue;
Also within the nested conditional block, add the Value property of the ParameterDiscreteValue instance (converted to String) into the ArrayList instance.
myArrayList.Add(myParameterDiscreteValue.Value.ToString())
arrayList.Add(parameterDiscreteValue.Value.ToString());
Outside the conditional block, and outside the foreach loop, at the end of the method, return the ArrayList instance from the method.
Return myArrayList
return arrayList;
To bind the ArrayList returned from the method to the ListBox in a Web project
You have retrieved the default values from the parameter field and returned them from the method as an ArrayList. You now bind this ArrayList to the defaultParameterValuesListListBox control.
Your code varies slightly depending on whether you use a Web project or a Windows project; therefore, be sure to only complete either the Web or Windows procedure below.
In the ConfigureCrystalReports() method, create a couple of line breaks in the code immediately after the line of code that retrieves the ParameterFields instance.
Within the line breaks, create a Not IsPostBack conditional block.
Note
The Not IsPostBack conditional block is used to encapsulate code that should only be run the first time the page loads. Controls are typically bound to data values within Not IsPostBack conditional blocks so that their data values (and any subsequent control events) are not reset during page reloads.
If Not IsPostBack Then End If
if(!IsPostBack) { }
Within the Not IsPostBack conditional block, set the DataSource property of the defaultParameterValuesListListBox to the GetDefaultValuesFromParameterField() helper method, passing in the ParameterFields instance as a method parameter.
``` vb
defaultParameterValuesList.DataSource =
GetDefaultValuesFromParameterField(myParameterFields)
```
``` csharp
defaultParameterValuesList.DataSource =
GetDefaultValuesFromParameterField(parameterFields);
```
- Still within the Not IsPostBack conditional block, call the DataBind() method of the defaultParameterValuesListListBox.
``` vb
defaultParameterValuesList.DataBind()
```
``` csharp
defaultParameterValuesList.DataBind();
```
To bind the ArrayList returned from the method to the ListBox in a Windows project
- In the ConfigureCrystalReports() method, create a couple of line breaks in the code immediately after the line of code that retrieves the ParameterFields instance.
Within these line breaks, you can now enter additional code that sets the data source for the defaultParameterValuesList ListBox control when the page loads for the first time.
- Within the line breaks, set the DataSource property of the defaultParameterValuesList ListBox to the GetDefaultValuesFromParameterField() helper method, passing in the ParameterFields instance as a method parameter.
``` vb
defaultParameterValuesList.DataSource =
GetDefaultValuesFromParameterField(myParameterFields)
```
``` csharp
defaultParameterValuesList.DataSource =
GetDefaultValuesFromParameterField(parameterFields);
```
To test the population of the defaultParameterValuesList ListBox control
You are now ready to build and run the project, to verify whether the defaultParameterValuesListListBox is populated.
From the Build menu, select Build Solution.
If you have any build errors, go ahead and fix them now.
From the Debug menu, click Start.
The defaultParameterValuesListListBox control displays a complete list of default values (cities, in our tutorial).
- Return to Visual Studio and click Stop to exit from debug mode.