Reports for WinForms | ComponentOne
Using the C1ReportDesigner Control / Step 7 of 9: Hook Up the Controls
In This Topic
    Step 7 of 9: Hook Up the Controls
    In This Topic

    The next step is to add the event handlers that hook up all the controls together.

    Here is the handler for the SelectedIndexChanged event of the _list control. Add the following code so that when the user selects a new report from the list, the code displays it in design mode:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' a new report was selected: switch to design mode and show it        
    Private Sub _list_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _list.SelectedIndexChanged        
       ' switch to design mode        
       SetDesignMode(True)    
            
       ' attach selected report to designer and preview controls        
       _c1rd.Report = Nothing        
       _c1ppv.Document = Nothing        
       If _list.SelectedIndex > -1 Then        
          _c1rd.Report = _list.SelectedItem.Report        
       End If        
    End Sub 
    

    To write code in C#

    C#
    Copy Code
    private void _list_SelectedIndexChanged(object sender, System.EventArgs e)        
    {
        
        // switch to design mode        
        SetDesignMode(true);
     
        // attach selected report to designer and preview controls        
        _c1rd.Report = null;        
        _c1ppv.Document = null;        
        if (_list.SelectedItem != null)        
            _c1rd.Report = ((ReportHolder)_list.SelectedItem).Report;        
    }
    

    The designer uses a property grid control (_ppg) to expose the properties of the report elements selected in the designer. This is done by setting the SelectedObject property of the property grid control; in response the control fires a SelectionChanged event.

    When the user selects a report section or a field in the designer control, it fires the SelectionChanged event. The event handler inspects the new selection and assigns it to the property grid control. This is a powerful mechanism. The selection can be a single report field, a group of fields, a section, or the whole report.

    Add the following code to implement the SelectionChanged event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' the selection changed, need to update property grid and show the        
    ' properties of the selected object         
    Private Sub _c1rd_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _c1rd.SelectionChanged        
       Dim sel As Object() = _c1rd.SelectedFields        
       If (sel.Length > 0) Then        
          _lblPropGrid.Text = "Field Properties"        
          _ppg.SelectedObjects = sel        
       ElseIf Not IsNothing(_c1rd.SelectedSection) Then        
             _lblPropGrid.Text = "Section Properties"        
             _ppg.SelectedObject = _c1rd.SelectedSection        
          ElseIf Not IsNothing(_c1rd.Report) Then        
                _lblPropGrid.Text = "Report Properties"        
                _ppg.SelectedObject = _c1rd.Report        
             ' nothing selected        
             Else        
                _lblPropGrid.Text = "Properties"        
                _ppg.SelectedObject = Nothing        
             End If        
       ' done        
       UpdateUI()       
    End Sub
    

    To write code in C#

    C#
    Copy Code
    // the selection changed, need to update property grid and show the        
    // properties of the selected object         
    private void _c1rd_SelectionChanged(object sender, System.EventArgs e)        
    {        
        object[] sel = _c1rd.SelectedFields;        
        if (sel.Length > 0)        
        {        
            _lblPropGrid.Text = "Field Properties";        
            _ppg.SelectedObjects = sel;        
        }        
        else if (_c1rd.SelectedSection != null)         
        {        
            _lblPropGrid.Text = "Section Properties";        
            _ppg.SelectedObject = _c1rd.SelectedSection;        
        }        
        else if (_c1rd.Report != null)        
        {        
            _lblPropGrid.Text = "Report Properties";        
            _ppg.SelectedObject = _c1rd.Report;        
        }        
        else // nothing selected        
        {        
            _lblPropGrid.Text = "Properties";        
            _ppg.SelectedObject = null;        
        }
      
        // done        
        UpdateUI();        
    }
    

    The property grid (_ppg) displays the properties of the object selected in the designer (_c1rd). When the user changes the properties of an object using the grid, the designer needs to be notified so it can update the display. Conversely, when the user edits an object using the designer, the grid needs to be notified and update its display.

    Add the following code to implement the handlers for the PropertyValueChanged event of the _ppg control and the ValuesChanged event of the _c1rd control:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' when a value changes in the property window, refresh the designer to show the changes        
    Private Sub _ppg_PropertyValueChanged(ByVal s As Object, ByVal e As System.Windows.Forms.PropertyValueChangedEventArgs) Handles _ppg.PropertyValueChanged        
       _c1rd.Refresh()        
       _dirty = True        
       UpdateUI()        
    End Sub 
        
    ' when properties of the selected objects change in the designer,        
    ' update the property window to show the changes        
    Private Sub _c1rd_ValuesChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _c1rd.ValuesChanged        
       _c1rd.Refresh()        
       _dirty = True        
       UpdateUI()        
    End Sub 
    

    To write code in C#

    C#
    Copy Code
    // when a value changes in the property window, refresh the designer        
    // to show the changes        
    private void _ppg_PropertyValueChanged(object s, Systems.Windows.Forms.PropertyValueChangedEventArgs e)        
    {        
        _c1rd.Refresh();        
        _dirty = true;        
        UpdateUI();        
    }        
    // when properties of the selected objects change in the designer,        
    // update the property window to show the changes        
    private void _c1rd_ValuesChanged(object sender, System.EventArgs e)        
    {        
        _ppg.Refresh();        
        _dirty = true;        
        UpdateUI();        
    }