Glossary Item Box
ActiveReports objects and controls are completely accessible at run time. The properties of any of the report sections or controls can be modified to produce a dynamic view of the report. The format event allows the properties of report sections and controls to be modified including height, visibility and other visual properties. The format event is the only event in which the printable area of a section can be modified. Once this event is completed, any changes to the section's height will not be reflected in the report output.
Note: Controls are added dynamically in the ReportStart event. Controls added dynamically after the ReportStart event cause problems.This walkthrough illustrates how to create a report layout at run time based on user input using Visual Studio 2003.
This walkthrough is split up into the following activities:
To complete the walkthrough, you must have access to the NorthWind database (NWind.mdb).
When you have completed this walkthrough, you will have output that looks similar to the following.
To add controls to the form
Control | Dock Property | Name | Text | Misc. Details |
---|---|---|---|---|
Panel | Left | pnlFields |
Width: 200 |
|
Label | Top | lblSelectFields | Select Fields for Your Report | Container: pnlFields |
Checked ListBox | Fill | clbFields |
Container: pnlFields |
|
Button | Bottom | btnGenRpt | Generate Report | Container: pnlFields |
Viewer (Drag onto right side of form.) | Fill | Viewer1 | Container: Windows Form |
To generate a dataset for the Windows Form
To add an ActiveReport to your project
To connect the report to a data source
To create an array list and add controls at run time
In the code view of rptFieldsRT, insert the following code beneath "ActiveReports Designer generated code."
' Visual Basic Private m_arrayFields As ArrayList Public WriteOnly Property FieldsList() As ArrayList Set(ByVal Value As ArrayList) m_arrayFields = Value End Set End Property Private m_defaultHeight As Single = 0.2F Private m_defaultWidth As Single = 4.0F Private m_currentY As Single = 0.0F Private Sub constructReport() Try Me.Detail.CanGrow = True Me.Detail.CanShrink = True Me.Detail.KeepTogether = True Dim i As Integer For i = 0 To m_arrayFields.Count - 1 If m_arrayFields(i).ToString <> "CategoryID" Then Dim lbl As New Label() lbl.Text = m_arrayFields(i) + ":" lbl.Location() = New System.Drawing.PointF _ (0.0F, m_currentY) lbl.Width = 0.9F lbl.Height = m_defaultHeight Me.Detail.Controls.Add(lbl) Dim txt As New TextBox() txt.DataField = m_arrayFields(i) txt.Location = New System.Drawing.PointF _ (1.0F, m_currentY) txt.Width = m_defaultWidth txt.Height = m_defaultHeight Me.Detail.Controls.Add(txt) If m_arrayFields(i) = "UnitPrice" Then txt.OutputFormat = "$#.00" End If m_currentY = m_currentY + m_defaultHeight End If Next Catch ex As Exception System.Windows.Forms.MessageBox.Show _ ("Error in Report-constructReport: " + ex.Message, "Project Error", _ System.Windows.Forms.MessageBoxButtons.OK, _ System.Windows.Forms.MessageBoxIcon.Error) End Try End Sub
//C# private System.Collections.ArrayList m_arrayFields; public System.Collections.ArrayList FieldsList { set{m_arrayFields = value;} } float m_defaultHeight = .2f; float m_defaultWidth = 4f; float m_currentY = 0f; private void constructReport() { try { this.Detail.CanGrow = true; this.Detail.CanShrink = true; this.Detail.KeepTogether = true; for(int i=0;i<m_arrayFields.Count;i++) { if(m_arrayFields[i].ToString() != "CategoryID") { Label lbl = new Label(); lbl.Text = m_arrayFields[i].ToString() + ":"; lbl.Location = new System.Drawing.PointF (0f,m_currentY); lbl.Width =.9f; lbl.Height = m_defaultHeight; this.Detail.Controls.Add(lbl); TextBox txt = new TextBox(); txt.DataField = m_arrayFields[i].ToString(); txt.Location = new System.Drawing.PointF (1f,m_currentY); txt.Width = m_defaultWidth; txt.Height = m_defaultHeight; this.Detail.Controls.Add(txt); m_currentY = m_currentY + m_defaultHeight; } } } catch(Exception ex) { System.Windows.Forms.MessageBox.Show("Error in Report-constructReport: " + ex.Message,"Project Error",System.Windows.Forms .MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error); } }
To write the code in Visual Basic
To write the code in C#
The following example shows what the code for the method looks like.
' Visual Basic
Private Sub rptFieldsRT_ReportStart(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles MyBase.ReportStart
constructReport()
End Sub
//C#
private void rptFieldsRT_ReportStart(object sender, System.EventArgs eArgs)
{
constructReport();
}
To write the code in Visual Basic
To write the code in C#
The following example shows what the code for the method looks like.
' Visual Basic
Dim m_count As Integer
Private Sub Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Detail.Format
If m_count Mod 2 = 0 Then
Me.Detail.BackColor = System.Drawing.Color.SlateGray
Else
Me.Detail.BackColor = System.Drawing.Color.DarkSeaGreen
End If
m_count = m_count + 1
End Sub
//C#
int m_count;
private void Detail_Format(object sender, System.EventArgs eArgs)
{
if(m_count % 2 ==0)
{
this.Detail.BackColor = System.Drawing.Color.SlateGray;
}
else
{
this.Detail.BackColor = System.Drawing.Color.DarkSeaGreen;
}
m_count++;
}
To create a method to fill the check list box
' Visual Basic Dim i As Integer Dim c As Integer Private Sub fillCheckBox() For i = 0 To Me.DataSet11.Tables.Count - 1 For c = 0 To Me.DataSet11.Tables(i).Columns.Count - 1 Me.clbFields.Items.Add(Me.DataSet11.Tables(i).Columns(c).ColumnName) Next Next End Sub
//C# private void fillCheckBox() { for(int i=0;i<this.dataSet11.Tables.Count;i++) { for(int c=0;c<this.dataSet11.Tables[i].Columns.Count;c++) { this.clbFields.Items.Add(this.dataSet11.Tables[i].Columns[c] .ColumnName); } } }
To write the code in Visual Basic
To write the code in C#
The following example shows what the code for the event looks like.
' Visual Basic
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Try
fillCheckBox()
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(Me, "Error in Form_Load: " + _ ex.Message, "Project Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
//C#
private void Form1_Load(object sender, System.EventArgs e)
{
try
{
fillCheckBox();
}
catch(Exception ex)
{
MessageBox.Show(this,"Error in Form_Load: " + ex.Message, "Project Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
To write the code in Visual Basic
To write the code in C#
The following example shows what the code for the method looks like.
' Visual Basic
Private Sub clbFields_SelectedIndexChanged(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles clbFields.SelectedIndexChanged
If Me.clbFields.CheckedItems.Count = 0 Then
Me.btnGenRpt.Enabled = False
Else
Me.btnGenRpt.Enabled = True
End If
End Sub
//C#
private void clbFields_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(this.clbFields.CheckedItems.Count>0)
{
this.btnGenRpt.Enabled = true;
}
else
{
this.btnGenRpt.Enabled = false;
}
}
To create a method to launch the report
' Visual Basic Dim m_arrayField As New ArrayList Private Sub launchReport() Dim rpt As New rptFieldsRT() Try rpt.FieldsList = m_arrayField rpt.DataMember = Me.DataSet11.Products.TableName Viewer1.Document = rpt.Document rpt.Run() Catch ex As Exception System.Windows.Forms.MessageBox.Show(Me, "Error in launchReport: " _ + ex.Message, "Project Error", MessageBoxButtons.OK, _ MessageBoxIcon.Error) End Try End Sub
//C# System.Collections.ArrayList m_arrayField = new System.Collections.ArrayList(); private void launchReport() { try { rptFieldsRT rpt = new rptFieldsRT(); rpt.FieldsList = m_arrayField; rpt.DataMember = this.dataSet11.Products.TableName; this.viewer1.Document = rpt.Document; rpt.Run(); } catch(Exception ex) { MessageBox.Show(this,"Error in launchReport: " + ex.Message, "Project Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } }
To write the code in Visual Basic
To write the code in C#
The following example shows what the code for the method looks like.
' Visual Basic
Private Sub btnGenRpt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnGenRpt.Click
Me.m_arrayField.Clear()
For i = 0 To Me.clbFields.CheckedItems.Count - 1
m_arrayField.Add(Me.clbFields.CheckedItems(i).ToString)
Next
launchReport()
End Sub
//C#
private void btnGenRpt_Click(object sender, System.EventArgs e)
{
this.m_arrayField.Clear();
for(int i=0;i<this.clbFields.CheckedItems.Count;i++)
{
m_arrayField.Add(this.clbFields.CheckedItems[i].ToString());
}
launchReport();
}
To select fields and view the report
See Also |
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.