赤い線が表示される
プレビュー時の赤い線は、指定された用紙の幅を示します。指定された用紙の幅よりも、レポート全体の幅が大きい場合に、プレビュー時に赤い線が表示されます。
この赤い線を消すには、レポートの幅(ActiveReportオブジェクトのPrintWidthプロパティ)と左右のマージン(PageSettingsオブジェクトのMargins.Left,Margins.Rightプロパティ)および、とじしろ(PageSettingsオブジェクトのGutterプロパティ)を足した値が、用紙の幅(PrinterオブジェクトのPaperWidthプロパティまたはPaperHeightプロパティ)を超えないようにする必要があります。
つまり、「PageSettings.Margins.Left + PageSettings.Margins.Right + PageSettings.Gutter + PrintWidth <= Document.Printer.PaperWidth(Document.Printer.PaperHeight)」の関係を守る必要があります。
下記の何れかの方法で指定してください。
以下のサンプルコードでは、レポートの幅(PrintWidthプロパティ)を、用紙の向きやマージンおよびとじしろに合わせて調整しています。
Visual Basic
| Visual Basicコード |
コードのコピー
|
|---|---|
Private Sub SectionReport1_ReportStart(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ReportStart
If Me.PageSettings.Orientation = PageOrientation.Landscape Then
' 用紙が横方向の場合
Me.PrintWidth = Me.Document.Printer.PaperHeight _
- (Me.PageSettings.Margins.Left + Me.PageSettings.Margins.Right + Me.PageSettings.Gutter)
Else
' 用紙が縦方向の場合
Me.PrintWidth = Me.Document.Printer.PaperWidth _
- (Me.PageSettings.Margins.Left + Me.PageSettings.Margins.Right + Me.PageSettings.Gutter)
End If
End Sub
|
|
C#
| C#コード |
コードのコピー
|
|---|---|
private void SectionReport1_ReportStart(object sender, EventArgs e)
{
if (this.PageSettings.Orientation == PageOrientation.Landscape)
{
// 用紙が横方向の場合
this.PrintWidth = this.Document.Printer.PaperHeight
- (this.PageSettings.Margins.Left + this.PageSettings.Margins.Right + this.PageSettings.Gutter);
}
else
{
// 用紙が縦方向の場合
this.PrintWidth = this.Document.Printer.PaperWidth
- (this.PageSettings.Margins.Left + this.PageSettings.Margins.Right + this.PageSettings.Gutter);
}
}
|
|
レポートを生成しながらプレビューする
ViewerのDocumentプロパティにレポートのDocumentインスタンスを設定した上で、Run(True)を実行すると、生成されたページから順次プレビューすることが可能です。
Visual Basic
| Visual Basicコード |
コードのコピー
|
|---|---|
Dim rpt As New SectionReport1
Viewer1.Document = rpt.Document
rpt.Run(True)
|
|
C#
| C#コード |
コードのコピー
|
|---|---|
SectionReport1 rpt = new SectionReport1();
this.viewer1.Document = rpt.Document;
rpt.Run(true);
|
|
ただし、生成されたページから順次プレビューする処理を、FormのLoadイベントで実装する場合は注意が必要です。LoadイベントがRunメソッドの完了まで継続し、この間の画面表示が更新されません。このため、Run(True)を実行しても、ビューワに順次レポートが表示されることはありません。
FormのLoadイベントを利用する場合、以下のような方法が考えられます。
Visual Basic
| Visual Basicコード |
コードのコピー
|
|---|---|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim thread As New System.Threading.Thread( _ New System.Threading.ThreadStart(AddressOf Thread1)) thread.IsBackground = True thread.SetApartmentState(Threading.ApartmentState.STA) thread.Start() End Sub Delegate Sub CreateReportDelegate() Private Sub CreateReport() Dim rpt As New SectionReport1 Viewer1.Document = rpt.Document rpt.Run(True) End Sub Private Sub Thread1() Me.Viewer1.Invoke(New CreateReportDelegate(AddressOf CreateReport)) End Sub |
|
C#
| C#コード |
コードのコピー
|
|---|---|
private void Form1_Load(object sender, System.EventArgs e)
{
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(Thread1));
thread.IsBackground = true;
thread.SetApartmentState(System.Threading.ApartmentState.STA);
thread.Start();
}
delegate void CreateReportDelegate();
private void CreateReport()
{
SectionReport1 rpt = new SectionReport1();
this.viewer1.Document = rpt.Document;
rpt.Run(true);
}
void Thread1()
{
this.viewer1.Invoke(new CreateReportDelegate(CreateReport));
}
|
|
Visual Basic
| Visual Basicコード |
コードのコピー
|
|---|---|
Private timer As New Timer()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
' タイマ・メソッドの作成します。
AddHandler timer.Tick, New EventHandler(AddressOf CreateReport)
timer.Interval = 100
' Loadイベントが終了してから0.1秒後にレポートを生成します。
timer.Enabled = True
End Sub
Private Sub CreateReport(ByVal sender As Object, ByVal e As EventArgs)
' タイマ・メソッドが今後発生しないようにします。
timer.Enabled = False
' レポート生成処理。
Dim rpt As New SectionReport1()
Me.viewer1.Document = rpt.Document
rpt.Run(True)
End Sub
|
|
C#
| C#コード |
コードのコピー
|
|---|---|
private Timer timer = new Timer();
private void Form1_Load(object sender, System.EventArgs e)
{
// タイマ・メソッドの作成します。
timer.Tick += new EventHandler(CreateReport);
timer.Interval = 100;
// Loadイベントが終了してから0.1秒後にレポートを生成します。
|
|
.NET Coreデスクトップアプリにおいて、Windowsフォーム用ビューワまたはWPFビューワでスクリプトを含むセクションレポートをプレビューする時、System.NotSupportedException例外が発生する
.NET Coreデスクトップアプリでは、Windowsフォーム用ビューワまたはWPFビューワを使用してスクリプトを含むセクションレポートをプレビューする時、「System.NotSupportedException:1252のエンコードに使用できるデータはありません。」例外が発生します。カスタムエンコーディングを定義する方法については、Encoding.RegisterProviderメソッドを参照してください。
これは、Windowsフォーム用ビューワ、WPFビューワ、およびデザイナを組み込んだ.NET Coreアプリケーションを使用する前に、エンコーディングを登録しないことに関連して発生します。
この事象を回避するには、以下の設定を行ってください。
|
コードのコピー
|
|
|---|---|
static void Main() { System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new DesignerForm()); } |
|
セクションレポート(コード)をプレビューする時、「同じID 'System.ValueTupple'のアセンブリが既にインポートされています。」エラーが発生します。
Visual Studio 2013でセクションレポート(コード)をプレビューする場合、「同じID 'System.ValueTupple'のアセンブリが既にインポートされています。重複している参照の一方を削除してください。」例外が発生します。
これは、Visual Studio 2013で、プロジェクトのターゲットが.NET Framework 4.6.2より新しいバージョンの場合、System.ValueTupleが2回追加されるために発生しています。
この事象を回避するには、ターゲットのバージョンを4.6.2に変更して、重複する参照の一方を削除してください。なお、その後、レポートをプレビューすると、「<ファイル>をコピーできませんでした」というエラーが表示される場合があります。これを解決するには、Visual Studioを再起動する必要があります。