Populating the ComboBox Control in a Windows Project

Visual Studio 2005

In this section, you write code to populate a ComboBox control with a list of non-embedded reports. You will use the getReports() helper method to generate a list of reports from Crystal Reports web services and then bind this list to the ComboBox control.

For a Web Site, see Populating the DropDownList Control.

Populating the ComboBox Control in a Windows Project

In this procedure, you populate a sorted list of reports using Crystal Reports Web Services.

  1. Open the Windows Form.
  2. From the View menu, click Code.
  3. Above the class signature, add an "Imports"[Visual Basic] or "using"[C#] declaration to the top of the class for the System.IO, System.Collections and the System.Web.Services namespaces.
    [Visual Basic]
    Imports System.IO
    Imports System.Collections
    Imports System.Web.Services
    [C#]
    using System.IO;
    using System.Collections;
    using System.Web.Services
  4. Within the Form1_Load event handler, use the getReports() helper method to retrieve the list of reports from the Web Service. Assign the result to the reports array.
    [Visual Basic]
    Dim myReports As ArrayList = getReports()
    [C#]
    ArrayList reports = getReports();
  5. On the next line, instantiate an ArrayList object.
    [Visual Basic]
    Dim myArrayList As ArrayList = New ArrayList()
    [C#]
    ArrayList arrayList = new ArrayList();
  6. Next, create a For Each loop that loops through each element of reports.
    [Visual Basic]
    For Each myPath As String In myReports
    Next
    [C#]
    foreach (string path in reports)
    {
    }
  7. The next block of code uses string manipulation methods to remove the file path from the name of each report and replace escape characters with string literals. Insert these lines inside of the For Each loop.
    [Visual Basic]
    Dim myReportNamePrefix As Integer = myPath.LastIndexOf("/") + 1
    Dim myReportNameSufix As Integer = myPath.LastIndexOf("?")
    Dim myReportNameLength As Integer = myReportNameSufix - myReportNamePrefix
    Dim myReportName As String = myPath.Substring(myReportNamePrefix, myReportNameLength)
    myReportName = myReportName.Replace("%20", " ")
    [C#]
    int reportNamePrefix = path.LastIndexOf(@"/") + 1;
    int reportNameSufix = path.LastIndexOf(@"?");
    int reportNameLength = reportNameSufix - reportNamePrefix;
    string reportName = path.Substring(reportNamePrefix, reportNameLength);
    reportName = reportName.Replace("%20", " ");
  8. Still inside the For Each loop, add a line of code that adds the truncated report name to arrayList.
    [Visual Basic]
    myArrayList.Add(myReportName)
    [C#]
    arrayList.Add(reportName);
  9. Outside of the For Each loop, assign the ArrayList instance to the dataSource property of the DropDownList control. This binds the ArrayList data source to the ComboBox control.
    [Visual Basic]
    reportsList.DataSource = mysortedList
    [C#]
    reportsList.DataSource = sortedList;
  10. From the File menu, select Save All.
  11. From the Build menu, select Build Solution.
  12. If you have any build errors, go ahead and fix them now.

In the next section, you add a click event to bind the report based on selections from the reportsList ComboBox control.

Continue to Adding a Click Event to Bind the Report.

See Also

Other Tutorials | Tutorials and Sample Code | Tutorials Sample Code Directory

Show: