The simple designer is almost done; it is only missing the code used to create new fields in the report.
If you look at the code we are using for the toolbar event handler, you will see that it set the CreateFieldInfo property on the designer and says to wait for the CreateField event from the designer.
Add the following code to create new fields in the report:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
' handle clicks on toolbar buttons Private Sub _tb_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles _tb.ButtonClick ' add fields ' (just set create info and wait for CreateField event from designer) If e.Button.Equals(_btnAddField) Then _c1rd.CreateFieldInfo = e.Button If e.Button.Equals(_btnAddLabel) Then _c1rd.CreateFieldInfo = e.Button End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
// handle clicks on toolbar buttons private void _tb_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { // add fields // (just set create info and wait for CreateField event from designer) if (e.Button == _btnAddField) _c1rd.CreateFieldInfo = e.Button; if (e.Button == _btnAddLabel) _c1rd.CreateFieldInfo = e.Button; } |
The CreateFieldInfo property can be set to any non-null object to indicate to the designer that you want to create a new field. The designer doesn't know what type of field you want to create or how you want to initialize it, so it tracks the mouse and allows the user to draw the field outline on a section. It then fires the CreateField event passing the information you need to create the field yourself.
Add the following code to handle the CreateField event:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Dim _ctr As Integer Private Sub _c1rd_CreateField(ByVal sender As Object, ByVal e As C1.Win.C1ReportDesigner.CreateFieldEventArgs) Handles _c1rd.CreateField ' save undo info _c1rd.UndoStack.SaveState() ' add label field _ctr = _ctr + 1 Dim fieldName As String = String.Format("NewField{0}", _ctr) Dim fieldText As String = fieldName Dim f As Field = e.Section.Fields.Add(fieldName, fieldText, e.FieldBounds) ' if this is a calculated field, ' change the Text and Calculated properties If e.CreateFieldInfo.Equals(_btnAddField) Then Dim fieldNames As String() = _c1rd.Report.DataSource.GetDBFieldList(True) If (fieldNames.Length > 0) Then f.Text = fieldNames(0) f.Calculated = True End If End If End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
int _ctr = 0; private void _c1rd_CreateField(object sender, C1.Win.C1ReportDesigner.CreateFieldEventArgs e) { // save undo info _c1rd.UndoStack.SaveState(); // add label field string fieldName = string.Format("NewField{0}", ++_ctr); string fieldText = fieldName; Field f = e.Section.Fields.Add(fieldName, fieldText, e.FieldBounds); // if this is a calculated field, // change the Text and Calculated properties if (e.CreateFieldInfo == _btnAddField) { string[] fieldNames = _c1rd.Report.DataSource.GetDBFieldList(true); if (fieldNames.Length > 0) { f.Text = fieldNames[0]; f.Calculated = true; } } } |
Note how the code starts by calling the SaveState method on the designer, so the user can undo the field creation. After that, the field is created, and the CreateFieldInfo parameter is used to customize the new field and make it behave as a label or as a calculated field.
That concludes the simple designer application: an introduction to the operation of the C1ReportDesigner control.