使用C1ReportDesigner控件 > 步骤七:连接控件 |
下一步是添加事件处理程序,将所有的控件连接在一起。
这里是_list 控件的SelectedIndexChanged事件的处理程序。添加以下代码,当用户从列表中选择一个新的报表,该代码在设计模式显示它:
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
' 选中了一个新报表:切换到设计模式并显示 Private Sub _list_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _list.SelectedIndexChanged ' 切换到设计模式 SetDesignMode(True) ' 将选中的报表关联到设计器以及预览控件 _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# |
拷贝代码
|
---|---|
private void _list_SelectedIndexChanged(object sender, System.EventArgs e) { // 切换到设计模式 SetDesignMode(true); // 将选中的报表关联到设计器以及预览控件 _c1rd.Report = null; _c1ppv.Document = null; if (_list.SelectedItem != null) _c1rd.Report = ((ReportHolder)_list.SelectedItem).Report; } |
设计器使用一个PropertyGrid控件(_ppg)将在设计器中选中的元素的属性暴露出来。这通过设置该PropertyGrid控件的SelectedObject属性完成;做为反馈,该控件触发一个SelectionChanged 事件。
当用户在设计器控件中选中了一个报表字段或者区域时,它将触发SelectionChanged事件。该事件的处理程序检查新的选择元素并将其设置给PropertyGrid控件。这是一个强大的机制。选中的元素可以是单个报表字段,一个字段分组,一个区域,或者整个报表。
将下面的代码添加到SelectionChanged 的方法实现:
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
' 选择发生变化,需要更新PropertyGrid并显示选中对象的属性 ' 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 ' 没有对象选中 Else _lblPropGrid.Text = "Properties" _ppg.SelectedObject = Nothing End If '完成 UpdateUI() End Sub |
To write code in C#
C# |
拷贝代码
|
---|---|
// 选择发生变化,需要更新PropertyGrid并显示选中对象的属性 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 // 没有对象选中 { _lblPropGrid.Text = "Properties"; _ppg.SelectedObject = null; } //完成 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 |
拷贝代码
|
---|---|
' 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# |
拷贝代码
|
---|---|
// 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(); } |