|  | 
 
|  
 การแสดงผลภาพลงใน ActiveReports ด้วยวิธีการ UnBound Data หรือการไม่ผูกฟิลด์ข้อมูลใดๆเข้ากับ Control จะทำให้เกิดความยืดหยุ่นในการปรับแต่งใช้งาน เช่น ในฐานข้อมูลจะเก็บเฉพาะชื่อไฟล์ภาพเท่านั้น แล้วใช้พาธ (Path) ในการนำภาพมาแสดงผลลงใน PictureBox โค้ดชุดนี้แอดมินได้ตัดส่วน Detail ออกไป แต่จะใช้เหตุการณ์ FetchData หรือการเรียกข้อมูลมาแสดงผลใหม่แทนเท่านั้น ...
 
 
  Design Time (AR Designer)
 
 
  Add References ...
 
 มาดูโค้ดฉบับเต็มกันเถอะ ... (ฟอร์มหลัก)
 
 คัดลอกไปที่คลิปบอร์ดImports DataDynamics.ActiveReports.Export.Pdf
Public Class frmAR6PrintImage
    Private Sub btnPreview_Click(sender As System.Object, e As System.EventArgs) Handles btnPreview.Click
        '// ประกาศตัวแปร rpt จากหน้ารายงาน ActiveReports
        Dim rpt As New NewActiveReport1
        '// สั่งรัน
        rpt.Run()
        Me.Viewer1.ReportViewer.Zoom = 1    '(100%)
        '// แสดงผลรายงานเอกสาร
        Me.Viewer1.Document = rpt.Document
    End Sub
    Private Sub btnExportPDF_Click(sender As System.Object, e As System.EventArgs) Handles btnExportPDF.Click
        '// Add Reference: ActiveReports.PdfExport.DLL เข้ามาก่อน
        '// ประกาศตัวแปร rpt จากหน้ารายงาน ActiveReports
        Dim rpt As New NewActiveReport1
        rpt.Run()
        Me.Viewer1.Document = rpt.Document
        '//
        Dim pdf As New DataDynamics.ActiveReports.Export.Pdf.PdfExport()
        Dim dlgSaveFile As New SaveFileDialog()
        With dlgSaveFile
            .Filter = "PDF|*.pdf"
            .Title = "Export report to PDF File"
            .DefaultExt = "pdf"
            .InitialDirectory = MyPath(Application.StartupPath)
            .RestoreDirectory = True
        End With
        If dlgSaveFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            pdf.Export(Me.Viewer1.Document, dlgSaveFile.FileName)
            '// แสดงผลไฟล์ PDF ด้วย Default PDF Reader.
            Process.Start(dlgSaveFile.FileName)
        End If
    End Sub
    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
    Private Sub frmAR6PrintImage_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
        GC.SuppressFinalize(Me)
        Application.Exit()
    End Sub
End Class
โค้ดในส่วนของ ActiveReports (NewActiveReport1.vb)
 
 คัดลอกไปที่คลิปบอร์ดImports System.Data.OleDb
Imports DataDynamics.ActiveReports
Imports DataDynamics.ActiveReports.Document 
Public Class NewActiveReport1
    '// พาธกำหนดตำแหน่งไฟล์ภาพ
    Private strPathImages As String = MyPath(Application.StartupPath) & "Images"
    ' / --------------------------------------------------------------------------------
    '// Start Here.
    ' / --------------------------------------------------------------------------------
    Private Sub NewActiveReport1_ReportStart(sender As Object, e As System.EventArgs) Handles Me.ReportStart
        '/ การตั้งค่าหน้ากระดาษ
        With PageSettings
            '/ หน่วยวัดเป็นนิ้ว แต่ใช้การแปลงหน่วยด้วย CmToInch เข้าช่วย หากเราถนัดหน่วยวัด ซม.
            .Margins.Left = CmToInch(0.75) '// แปลงค่า 1.0 ซม. เป็นนิ้ว
            .Margins.Right = CmToInch(0.5)
            .Margins.Top = CmToInch(1.0)
            .Margins.Bottom = CmToInch(0.2)
            '/ ตั้งค่ากระดาษแนวตั้ง
            .Orientation = PageOrientation.Portrait
            '/ กระดาษขนาด A4
            .PaperKind = Drawing.Printing.PaperKind.A4
        End With
        '// เคลียร์ค่าก่อนการพิมพ์
        txtFullName.Text = ""
        txtNickName.Text = ""
        txtPositionName.Text = ""
        txtDepartmentName.Text = ""
        Picture1.SizeMode = SizeModes.Stretch
    End Sub
    ' / --------------------------------------------------------------------------------
    '// Add the report's fields collection. (First time only.)
    ' / --------------------------------------------------------------------------------
    Private Sub NewActiveReport1_DataInitialize(sender As Object, e As System.EventArgs) Handles Me.DataInitialize
        '// เริ่มต้นการเชื่อมต่อฐานข้อมูล
        Try
            '// modDataBase.vb
            Call ConnectDataBase()
            '// ทำการ Query ฟิลด์ข้อมูลที่ต้องการมาแสดงผล
            strSQL = _
                " SELECT EmployeePK, EmployeeID, Fullname, Nickname, PictureName, tblPosition.PositionName, tblDepartment.DepartmentName " & _
                " FROM tblDepartment INNER JOIN (tblPosition INNER JOIN tblEmployee ON tblPosition.PositionPK = tblEmployee.PositionFK) ON tblDepartment.DepartmentPK = tblEmployee.DepartmentFK " & _
                " ORDER BY EmployeePK, FullName ASC "
            '// Open connection and create DataReader.     
            If Conn.State = ConnectionState.Closed Then Conn.Open()
            Cmd = New OleDbCommand(strSQL, Conn)
            DR = Cmd.ExecuteReader()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    ' / --------------------------------------------------------------------------------
    '// Retrieve information to populate the report fields.
    ' / --------------------------------------------------------------------------------
    Private Sub NewActiveReport1_FetchData(sender As Object, eArgs As DataDynamics.ActiveReports.ActiveReport.FetchEventArgs) Handles Me.FetchData
        Try
            DR.Read()
            '// นำข้อมูลมาผูกเข้ากับฟิลด์ที่เรากำหนดบน ActiveReports
            txtFullName.Text = DR("FullName")
            txtNickName.Text = DR("NickName")
            txtPositionName.Text = DR("PositionName")
            txtDepartmentName.Text = DR("DepartmentName")
            Picture1.Image = Image.FromFile(strPathImages & DR("PictureName"))
            '// ยังไม่หมดข้อมูล End Of File = เท็จ ... 
            '// กระโดดไปพิมพ์ที่ Detail1_Format อีกครั้ง แต่ Detail1_Format ไม่มี มันจะเด้งกลับมาหาโปรแกรมย่อยของตัวเองอีกที
            eArgs.EOF = False
        Catch ex As Exception
            '// แสดงว่าหมดข้อมูลแล้ว นั่นคือสิ้นสุดการพิมพ์
            eArgs.EOF = True
        End Try
    End Sub
    Private Sub NewActiveReport1_ReportEnd(sender As Object, e As System.EventArgs) Handles Me.ReportEnd
        DR.Close()
        Conn.Close()
    End Sub
End Class
โค้ดในส่วนของโมดูลการเชื่อมต่อฐานข้อมูล (modDataBase.vb)
 
 คัดลอกไปที่คลิปบอร์ดImports System.Data.OleDb
Imports Microsoft.VisualBasic
Module modDataBase
    '// Declare variable one time but use many times.
    Public Conn As OleDbConnection
    Public Cmd As OleDbCommand
    'Public DS As DataSet
    Public DR As OleDbDataReader
    'Public DA As OleDbDataAdapter
    Public strSQL As String '// Major SQL
    'Public strStmt As String    '// Minor SQL
    '// Data Path 
    Public strPathData As String = MyPath(Application.StartupPath) & "Data"
    '// Images Path
    'Public strPathImages As String = MyPath(Application.StartupPath) & "Images"
    Public Sub ConnectDataBase()
        Dim strConn As String = _
            "Provider = Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source = " & strPathData & "Employee.accdb;"
        Try
            Conn = New OleDb.OleDbConnection(strConn)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    ' / --------------------------------------------------------------------------------
    ' / Get my project path
    ' / AppPath = C:\My Project\bin\debug
    ' / Replace "\bin\debug" with ""
    ' / Return : C:\My Project\
    Function MyPath(AppPath As String) As String
        '/ Return Value
        MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
        '// If not found folder then put the \ (BackSlash) at the end.
        If Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
    End Function
End Module
ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...
 
 
 | 
 
xขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึงคุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน  |