Share via


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

  1. Open the Web or Windows Form.
  2. From the View menu, click Designer.
  3. If you are developing a Web Site, do the following:
  4. Click the CrystalReportViewer control to select it.
  5. Press the LEFT ARROW on your keyboard so that a flashing cursor appears, and then press ENTER.
  6. If you are developing a Windows project, do the following:
    1. Click the CrystalReportViewer control to select it.
    2. From the Properties window, set Dock to "Bottom."
    3. Resize the CrystalReportViewer control, so that you leave enough room above it for a ListBox control.
    4. From the Properties window, set Anchor to "Top, Bottom, Left, Right."
  7. From the Toolbox, drag a ListBox control above the CrystalReportViewer control.
  8. Click on the ListBox control to select it.
  9. From the Properties window, do the following:
  10. Set the ID or Name to "defaultParameterValuesList."
  11. Set the SelectionMode to "Multiple" (in a Windows project, "MultiExtended").
  12. 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.

  1. Open the Web or Windows Form.

  2. From the View menu, click Code.

  3. 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)
{
}
```
  1. 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];
    
  2. 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;
    
  3. Declare and instantiate an ArrayList.

    Dim myArrayList As ArrayList = New ArrayList()
    
    ArrayList arrayList = new ArrayList();
    
  4. 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)
{
}
```
  1. If the available property is IsRange then, within the foreach loop, enter this code:

    If (Not myParameterValue.IsRange) Then
    
    End If
    
    if(!parameterValue.IsRange)
    {
    }
    
  2. 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)
    {
    }
    
  3. 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;
    
  4. 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());
    
  5. 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.

  1. In the ConfigureCrystalReports() method, create a couple of line breaks in the code immediately after the line of code that retrieves the ParameterFields instance.

  2. 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)
    {
    }
    
  3. 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);
```
  1. 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

  1. 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.
  1. 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.

  1. From the Build menu, select Build Solution.

  2. If you have any build errors, go ahead and fix them now.

  3. From the Debug menu, click Start.

The defaultParameterValuesListListBox control displays a complete list of default values (cities, in our tutorial).
  1. Return to Visual Studio and click Stop to exit from debug mode.