thongkorn โพสต์ 2018-8-14 13:23:54

แจกฟรีทั้งโค้ด และโปรแกรมการพิมพ์ใบแจ้งค่าใช้จ่าย สำหรับหอพัก ห้องเช่า อพาร์ทเมนท์ แบบกระดาษครึ่ง A4 (VB.NET เวอร์ชั่น II)

http://www.g2gnet.com/webboard/images/vbnet/RentRoomII.png

มาภาคต่อเป็นเวอร์ชั่น II กันครับ ... เวอร์ชั่นใหม่นี้จะปรับเพิ่มให้มีจำนวนหน่วย (Unit) เข้ามาใหม่ จากนั้นนำไปคูณเข้ากับราคาต่อหน่วย เป็นการแจกแจงรายละเอียดต่อผู้เช่าห้องได้ดียิ่งขึ้น ...
สำหรับท่านที่ต้องการใช้งานโปรแกรมเท่านั้น สามารถดาวน์โหลดได้แบบไม่ต้องติดตั้งโปรแกรม (คลิ๊กดาวน์โหลดที่นี่) เมื่อดาวน์โหลดไปแล้วให้ทำการแตกไฟล์ออกมา แล้วสั่งรันโปรแกรมที่มีชื่อว่า RoomPayment.exe (รูปไอคอนเป็นที่เก็บแฟ้มสีน้ำเงิน) หรือหากต้องการดูเวอร์ชั่นแรก (คลิ๊กดาวน์โหลดที่นี่)

ปัญหาที่อาจจะเกิดขึ้น กรณีที่ไม่ได้ติดตั้ง Access DataBase Engine รุ่น 32 บิต ...
http://www.g2gnet.com/webboard/images/vbnet/AccessDBEngine.png
คลิ๊กดาวน์โหลดไฟล์ตัวนี้ฟรี เพื่อติดตั้งแก้ไขให้โปรแกรมสามารถทำงานได้ ...

สำหรับนักพัฒนาโปรแกรมสายพันธุ์ VB6 สามารถดูรายละเอียดของโค้ดได้จากที่นี่ แจกฟรีโค้ดการพิมพ์ใบแจ้งค่าใช้จ่าย แบบกระดาษครึ่ง A4 ด้วย FarPoint Spread และ ActiveReports 2.0 ...

สำหรับนักพัฒนาโปรแกรมสายพันธุ์ VB.NET สามารถดูรายละเอียดของโค้ดในเวอร์ชั่นแรกได้จากที่นี่ แจกฟรีโค้ดการพิมพ์ใบแจ้งค่าใช้จ่าย สำหรับหอพัก ห้องเช่า อพาร์ทเมนท์ แบบกระดาษครึ่ง A4 (ActiveReports.NET) ...

ในเวอร์ชั่นใหม่นี้ ใช้วิธีการจับคู่หลักในการพิมพ์ ดังนั้นหากมีการเพิ่มเติมใดๆก็ควรทำตามดังภาพเอาไว้น่ะครับ โดยในไฟล์โค้ดโปรแกรมฉบับเต็ม แอดมินได้ทำตัวอย่างเอาไว้ให้ด้วย ช่วงนี้แอดมินมีงานเต็มมือ จึงไม่พร้อมที่จะมาอธิบายรายละเอียดให้ได้ ก็คงต้องศึกษาเอาเองจากโค้ดที่ได้ไปนั่นแหละข่ะรับ หรือ กลับไปอ่านจากเวอร์ชั่นแรกก่อน แต่ถ้าหากติดขัดปัญหาก็สามารถถามไถ่กันเข้ามาได้ครับ ...

http://www.g2gnet.com/webboard/images/vbnet/rentroomexcel.png

มาดูโค้ดกันเถอะ ... ในส่วนของฟอร์มหลัก
' / ----------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Purpose: Print bill payment for rented room.
' / Microsoft Visual Basic .NET (2010) & MS Excel 2003+
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / ----------------------------------------------------------------
Imports System.Data.OleDb

Public Class frmMainBill
    Dim ColCount As Integer
    Dim DT As DataTable
    Dim DR As OleDbDataReader

    Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
      '/ ประกาศใช้งาน Open File Dialog ในแบบ Run Time
      Dim dlgOpenFile As OpenFileDialog = New OpenFileDialog()

      ' / ตั้งค่าการใช้งาน Open File Dialog
      With dlgOpenFile
            .InitialDirectory = MyPath(Application.StartupPath)
            .Title = "เลือกไฟล์ MS Excel"
            .Filter = "MS Excel Files (*.xlsx;*.xls)|*.xlsx;*xls"
            .FilterIndex = 1
            .RestoreDirectory = True
      End With
      '/ หากเลือกปุ่ม OK หลังจากการ Browse ...
      If dlgOpenFile.ShowDialog() = DialogResult.OK Then
            txtFileName.Text = dlgOpenFile.FileName
            Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
                dlgOpenFile.FileName & "; Extended Properties=""Excel 12.0 Xml; HDR=YES"";"
            Dim Conn As New OleDbConnection(strConn)
            Conn.Open()
            '/ มอง WorkSheet ให้เป็นตารางข้อมูล (Table)
            Dim dtSheets As DataTable = Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim drSheet As DataRow

            cmbSheetName.Items.Clear()
            '/ นำรายชื่อ WorkSheet ทั้งหมด มาเก็บไว้ที่ ComboBox เพื่อรอให้ User เลือกนำไปใช้งานเอง
            For Each drSheet In dtSheets.Rows
                cmbSheetName.Items.Add(drSheet("TABLE_NAME").ToString())
            Next
            Conn.Close()
            '//
            dgvData.Columns.Clear()
      End If

    End Sub

    Private Sub cmbSheetName_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbSheetName.SelectedIndexChanged
      Dim Conn As OleDbConnection
      Dim Cmd As OleDbCommand
      Dim DAP As OleDbDataAdapter
      Dim DS As DataSet

      '// Clear all columns.
      dgvData.Columns.Clear()

      Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
            txtFileName.Text & "; Extended Properties=""Excel 12.0 Xml; HDR=YES"";"
      Try
            Conn = New OleDbConnection
            Conn.ConnectionString = strConn
            Cmd = New OleDbCommand
            Dim Sql As String = "Select * FROM [" & cmbSheetName.Text & "]"
            If Conn.State = ConnectionState.Closed Then Conn.Open()
            Cmd.Connection = Conn
            Cmd.CommandText = Sql
            DR = Cmd.ExecuteReader()
            DT = New DataTable
            Dim iCol As Integer
            '/ เริ่มอ่านค่าหลักจาก Excel เข้ามา ตั้งแต่ #ชื่อผู้เช่า (หลัก 0)
            For iCol = 0 To DR.FieldCount - 1
                DT.Columns.Add(DR.GetName(iCol))
                '// ตั้งแต่หลักที่ 3 คอลัมน์ C (ค่าห้อง) ขึ้นไป จะมีแต่ตัวเลข จึงต้องกำหนด Data Type เป็น Double
                If iCol >= 3 Then
                  DT.Columns(iCol).DataType = GetType(Double)
                End If
            Next
            '// อ่านค่าจำนวนแถวเข้ามา
            If DR.HasRows Then
                While DR.Read
                  Dim DRow As DataRow = DT.NewRow
                  For i = 0 To DR.FieldCount - 1
                        DRow(i) = DR.Item(i).ToString
                  Next
                  '// Next row
                  DT.Rows.Add(DRow)
                End While
            End If
            '// ผูก DataTable เข้าสู่ตารางกริด
            dgvData.DataSource = DT

            '// เพิ่มปุ่มคำสั่งเข้ามาไว้ท้ายสุดในตารางกริดสำหรับการสั่ง Preview
            Dim btn As New DataGridViewButtonColumn()
            dgvData.Columns.Add(btn)
            With btn
                .HeaderText = ""
                .Text = "Preview"
                .Name = "btnPreview"
                .UseColumnTextForButtonValue = True
            End With
            ColCount = DT.Columns.Count
            '// จัดระเบียบตารางกริด
            Call InitGrid()

      Catch ex As Exception
            MessageBox.Show(ex.Message)
      Finally
            Conn = Nothing
            Cmd = Nothing
            DAP = Nothing
            DS = Nothing
      End Try
    End Sub

    ' / --------------------------------------------------------------------------------
    '// Initialize DataGridView @Run Time
    Private Sub InitGrid()
      '// Setup each column
      With dgvData
            '// Column 2 to 9
            For i As Byte = 2 To ColCount - 1
                ' Header Alignment
                .Columns(i).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
                ' Cell Alignment
                .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                '// Value type
                .Columns(i).ValueType = GetType(Double)
                '// Format Number
                .Columns(i).DefaultCellStyle.Format = "N2"
                '// กรณีต้องการปิดการแสดงผลจำนวนหน่วย (ตั้งแต่หลัก F ค่าไฟฟ้าต่อหน่วย)
                'If i >= 5 Then
                '// ปิดการแสดงผลหลัก 5, 7 และหลัก 9 (การหารเอาเศษเลขจำนวนเต็มด้วย 2 หากได้คำตอบเป็น 1 คือเลขคี่)
                'If (i Mod 2) = 1 Then .Columns(i).Visible = False
                'End If
            Next
      End With
      With dgvData
            .RowHeadersVisible = False
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToResizeRows = False
            .MultiSelect = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .ReadOnly = True
            .Font = New Font("Tahoma", 9)
            ' Autosize Column
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
            .AutoResizeColumns()
            '// Even-Odd Color
            .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
            ' Adjust Header Styles
            With .ColumnHeadersDefaultCellStyle
                .BackColor = Color.Navy
                .ForeColor = Color.Black
                '.Font = New Font("Tahoma", 9, FontStyle.Bold)
                .WrapMode = DataGridViewTriState.False
            End With
      End With
    End Sub

    Private Sub txtFileName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtFileName.KeyPress
      '// Protect any key press in TextBox.
      e.Handled = True
    End Sub

    Private Sub dgvData_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellClick
      Select Case e.ColumnIndex
            Case ColCount
                'MsgBox(("Row : " + e.RowIndex.ToString & "Col : ") + e.ColumnIndex.ToString)
                Dim colName As String = dgvData.Columns(e.ColumnIndex).Name
                If colName = "btnPreview" Then Call PreviewData()
      End Select
    End Sub

    Private Sub dgvData_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dgvData.KeyDown
      If e.KeyCode = Keys.Enter Then
            Call dgvData_DoubleClick(sender, e)
            e.SuppressKeyPress = True
      End If
    End Sub

    Private Sub dgvData_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellContentClick
      '
    End Sub

    Private Sub dgvData_DoubleClick(sender As Object, e As System.EventArgs) Handles dgvData.DoubleClick
      If dgvData.Rows.Count <= 0 Then Exit Sub
      Call PreviewData()
    End Sub

    ' / ----------------------------------------------------------------
    ' / Preview
    Private Sub PreviewData()
      Try
            '/ Instance name ARDesigner มันจะชี้ไปที่ไฟล์ arRentRoomBill.vb
            Dim rpt As New arRentRoomBill
            '/ โหลดรายงาน document (arRentRoomBill) เข้าสู่ ActiveReports Viewer
            '// Zoom 90%
            Me.Viewer1.ReportViewer.Zoom = 0.9
            Me.Viewer1.Document = rpt.Document
            '/ Run Report
            rpt.Run()
      Catch ex As Exception
            MessageBox.Show(ex.Message)
      End Try
    End Sub

    Private Sub ToolStripStatusLabel2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripStatusLabel2.Click
      Process.Start("https://www.facebook.com/g2gnet")
    End Sub

    Private Sub itemBtnExit_Click(sender As System.Object, e As System.EventArgs) Handles itemBtnExit.Click
      Me.Close()
    End Sub

    Private Sub itemBtnSetup_Click(sender As System.Object, e As System.EventArgs) Handles itemBtnSetup.Click
      frmSetting.ShowDialog()
    End Sub

    Private Sub itemBtnAbout_Click(sender As System.Object, e As System.EventArgs) Handles itemBtnAbout.Click
      frmAboutMe.ShowDialog()
    End Sub

    Private Sub ToolStripStatusLabel3_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripStatusLabel3.Click
      Process.Start("http://www.g2gnet.com")
    End Sub

    Private Sub frmMainBill_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
      Application.Exit()
    End Sub

    Private Sub frmMainBill_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      Me.ToolStrip1.Cursor = Cursors.Hand
    End Sub

End Classhttp://www.g2gnet.com/webboard/images/vbnet/RentRoomSetup.png
โค้ดในส่วนของการตั้งค่า (Setting) ด้วยการใช้ Initial File (INI) ...
' / --------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Purpose: Setting up information.
' / Microsoft Visual Basic .NET (2010) & MS Excel 2003+
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
Public Class frmSetting
    Dim strFileINI As String

    Private Sub frmSetting_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
      Select Case e.KeyCode
            Case Keys.F8
                Call btnSave_Click(sender, e)
            Case Keys.Escape
                Me.Close()
      End Select
    End Sub

    ' / --------------------------------------------------------------------------------
    Private Sub frmSetting_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      strFileINI = MyPath(Application.StartupPath) & "Config.ini"
      '// เช็คว่ามีไฟล์ Config.ini อยู่หรือไม่???
      If My.Computer.FileSystem.FileExists(strFileINI) Then
            txtOwner.Text = ReadIni(strFileINI, "Config", "Owner", "")
            txtAddress.Text = ReadIni(strFileINI, "Config", "Address", "")
            txtHeaderBill.Text = ReadIni(strFileINI, "Config", "HeaderBill", "")
            txtElectricRate.Text = ReadIni(strFileINI, "Config", "ElectricRate", "")
            txtWaterRate.Text = ReadIni(strFileINI, "Config", "WaterRate", "")
            txtRemark1.Text = ReadIni(strFileINI, "Config", "Remark1", "")
            txtRemark2.Text = ReadIni(strFileINI, "Config", "Remark2", "")
            '// กรณีไม่เจอ ให้เริ่มต้นค่าใหม่
      Else
            txtOwner.Text = "ทองก้อน อพาร์ทเมนท์ แอนด์ โฮมเพลย์สเตชั่น"
            txtAddress.Text = "123/456 ถ.กลางเมือง ต.เมืองเก่า อ.เมือง จ.ขอนแก่น โทร.043-XXX-XXX"
            txtHeaderBill.Text = "- ใบแจ้งค่าใช้จ่าย -"
            txtElectricRate.Text = "10"
            txtWaterRate.Text = "5"
            txtRemark1.Text = " กรุณาชำระเงินภายในวันที่ 5 ของทุกเดือน"
            txtRemark2.Text = " ชำระค่าปรับล่าช้าวันละ 50 บาท"
      End If
    End Sub

    Private Sub txtElectricRate_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtElectricRate.KeyPress
      e.Handled = CheckDigitOnly(Asc(e.KeyChar))
    End Sub

    ' / --------------------------------------------------------------------------------
    '// บันทึกไฟล์ INI
    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
      WriteIni(strFileINI, "Config", "Owner", txtOwner.Text)
      WriteIni(strFileINI, "Config", "Address", txtAddress.Text)
      WriteIni(strFileINI, "Config", "HeaderBill", txtHeaderBill.Text)
      If txtElectricRate.Text.Length <> 0 Then
            WriteIni(strFileINI, "Config", "ElectricRate", txtElectricRate.Text)
      Else
            WriteIni(strFileINI, "Config", "ElectricRate", "0")
      End If
      If txtWaterRate.Text.Length <> 0 Then
            WriteIni(strFileINI, "Config", "WaterRate", txtWaterRate.Text)
      Else
            WriteIni(strFileINI, "Config", "WaterRate", "0")
      End If
      WriteIni(strFileINI, "Config", "Remark1", txtRemark1.Text)
      WriteIni(strFileINI, "Config", "Remark2", txtRemark2.Text)
      '//
      MessageBox.Show("บันทึกการตั้งค่าระบบเรียบร้อย.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Information)
      Me.Close()
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
      Me.Close()
    End Sub

    Private Sub frmSetting_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
    End Sub

    Private Sub txtOwner_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtOwner.KeyPress
      If e.KeyChar = Chr(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
      End If
    End Sub

    Private Sub txtAddress_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtAddress.KeyPress
      If e.KeyChar = Chr(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
      End If
    End Sub

    Private Sub txtRemark1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtRemark1.KeyPress
      If e.KeyChar = Chr(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
      End If
    End Sub

    Private Sub txtRemark2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtRemark2.KeyPress
      If e.KeyChar = Chr(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
      End If
    End Sub

    Private Sub txtHeaderBill_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtHeaderBill.KeyPress
      If e.KeyChar = Chr(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
      End If
    End Sub
End Classhttp://www.g2gnet.com/webboard/images/vbnet/RentRoomARNet.png
โค้ดในส่วนของ ActiveReports.NET ...
' / --------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Purpose: Print bill payment for rented room.
' / Microsoft Visual Basic .NET (2010) & MS Excel 2003+
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
Imports DataDynamics.ActiveReports
Imports DataDynamics.ActiveReports.Document
Imports DataDynamics.ActiveReports.Document.DDPaperSize
Imports System.Drawing.Printing.PaperSize
Imports System.Data.OleDb

Public Class arRentRoomBill
    '// ตัวแปรนี้ต้องประกาศเป็นแบบ Public (เฉพาะ arRoomReciept) เพื่อให้ส่วนของ Detail1_Format และ FetchData มองเห็นด้วย
    Private ItemNo As Integer
    Private GrandTotal As Double
    '// เลือกจากแถวในตารางกริด
    Private sRow As Integer
    '// ตัวแปรนับหลัก โดยเริ่มต้นที่ 2 และเพิ่มขึ้นทีละ 2 (Detail1_Format)
    Private sCol As Integer

    Private Sub arRoomReciept_ReportStart(sender As Object, e As System.EventArgs) Handles Me.ReportStart
      ' การตั้งค่าหน้ากระดาษ
      With PageSettings
            '/ หน่วยวัดเป็นนิ้ว
            .Margins.Left = CmToInch(1) '/ แปลงค่า 1.0 ซม. เป็นนิ้ว
            .Margins.Right = 0.1
            .Margins.Top = 0.5
            .Margins.Bottom = 0.2
            '/ ตั้งค่ากระดาษแนวตั้ง
            .Orientation = PageOrientation.Portrait
            '.Orientation = PageOrientation.Landscape
            ' กระดาษขนาด A4
            '.PaperKind = Drawing.Printing.PaperKind.A4
            '/ กรณีที่กำหนดขนาดกระดาษเอง
            .PaperKind = Drawing.Printing.PaperKind.Custom
            .PaperWidth = CmToInch(21) '/ 21 ซม.
            .PaperHeight = CmToInch(14.8)
      End With
      ' ปกติต้องเคลียร์ค่าต่างๆของ TextBox ก่อนการพิมพ์
      txtItem.Text = ""
      txtDesc.Text = ""
      txtUnitPrice.Text = ""
      txtQTY.Text = ""
      txtAmount.Text = ""
      txtGrandTotal.Text = ""
      '// แสดงกิจการและที่อยู่ โดยการอ่านค่าจาก INI (Initialized File)
      Dim strFileINI As String = MyPath(Application.StartupPath) & "Config.ini"
      If My.Computer.FileSystem.FileExists(strFileINI) Then
            txtOwner.Text = ReadIni(strFileINI, "Config", "Owner", "")
            txtAddress.Text = ReadIni(strFileINI, "Config", "Address", "")
            txtHeaderBill.Text = ReadIni(strFileINI, "Config", "HeaderBill", "")
            lblRemark1.Text = ReadIni(strFileINI, "Config", "Remark1", "")
            lblRemark2.Text = ReadIni(strFileINI, "Config", "Remark2", "")
            '// กรณีไม่เจอ ให้เริ่มต้นค่าใหม่
      Else
            txtOwner.Text = "ทองก้อน อพาร์ทเมนท์ แอนด์ โฮมเพลย์สเตชั่น"
            txtAddress.Text = "123/456 ถ.กลางเมือง ต.เมืองเก่า อ.เมือง จ.ขอนแก่น โทร.043-XXX-XXX"
            txtHeaderBill.Text = "- ใบแจ้งค่าใช้จ่าย -"
            lblRemark1.Text = " กรุณาชำระเงินภายในวันที่ 5 ของทุกเดือน"
            lblRemark2.Text = " ชำระค่าปรับล่าช้าวันละ 50 บาท"
      End If
      txtDate.Text = FormatDateTime(Now(), DateFormat.GeneralDate)
      '// อ่านค่าชื่อ WorkSheet มาพิมพ์ประจำเดือน (ตัวอย่าง "กรกฎาคม 2561")
      Dim strMonth As String = frmMainBill.cmbSheetName.Text
      '// ตัดคำที่ไม่ต้องการ
      strMonth = strMonth.Replace("'", "").Replace("[      DISCUZ_CODE_2      ]quot;, "")
      txtDateReciept.Text = strMonth

      '// รับค่า "แถว" ที่ถูกเลือกในตารางกริด
      sRow = frmMainBill.dgvData.CurrentRow.Index
      txtCname.Text = frmMainBill.dgvData.Rows(sRow).Cells(0).Value
      txtRoomNo.Text = frmMainBill.dgvData.Rows(sRow).Cells(1).Value
      '//
      ItemNo = 0
      '// ตั้งค่าหลักแรกที่จะพิมพ์คือหลักที่ 2 (หน่วยค่าห้อง)
      sCol = 2
    End Sub

    Private Sub arRoomReciept_FetchData(sender As Object, eArgs As DataDynamics.ActiveReports.ActiveReport.FetchEventArgs) Handles Me.FetchData
      ItemNo = ItemNo + 1
      '/ ตรวจสอบจำนวนการพิมพ์ทั้งหมด ซึ่งจะมีทั้งหมด 10 หลัก แต่จะใช้การนับหลักเพิ่มทีละ 2
      '/ จึงใช้ \ หรือการหารตัดเศษทิ้งด้วย 2 ก็จะได้หลักมา 5 หลักเท่านั้น
      If ItemNo >= frmMainBill.dgvData.Columns.Count \ 2 Then
            '/ หากหมดแล้วก็จบการพิมพ์
            eArgs.EOF = True
            Exit Sub
            '/ ยังไม่หมดข้อมูลในหลักที่จะพิมพ์
      Else
            eArgs.EOF = False
      End If
    End Sub

    Private Sub Detail1_Format(sender As Object, e As System.EventArgs) Handles Detail1.Format
      '// ItemNo หรือลำดับที่ โดยจะเริ่มจาก 1 (กระโดดไปทำงานที่ FetchData ก่อนลงมาใน Detail1_Format)
      txtItem.Text = ItemNo
      With frmMainBill
            '// ชื่อผู้เช่า (0), หมายเลขห้อง (1)
            '// อ่านค่าเริ่มจากหลัก 2
            '// 1 หน่วยค่าห้อง (2), ค่าเช่าห้อง (3), ค่าไฟฟ้า(4), ค่าไฟฟ้าต่อหน่วย (5) ... ไล่ไปทีละคู่
            txtDesc.Text = .dgvData.Columns(sCol).HeaderText
            txtQTY.Text = Format(Val(.dgvData.Rows(sRow).Cells(sCol).Value), "#,##0.00")
            '// อ่านค่าข้อมูลจากแถวที่เลือกมา (sRow) และข้อมูลอยู่ในหลัก 2 (และเลื่อนหลักถัดๆไปโดยให้ sCol เพิ่มขึ้นทีละ 2)
            txtUnitPrice.Text = Format(Val(.dgvData.Rows(sRow).Cells(sCol + 1).Value), "#,##0.00")
            txtAmount.Text = Format(CDbl(txtUnitPrice.Text) * CDbl(txtQTY.Text), "#,##0.00")
      End With
      '/ รวมจำนวนเงินผ่านทางตัวแปร
      GrandTotal = GrandTotal + CDbl(txtAmount.Text)
      txtGrandTotal.Text = "รวมเงิน: " & Format(GrandTotal, "#,##0.00") & " บาท."
      sCol += 2
    End Sub

End Class
โค้ดในส่วนของโมดูลหากิน ... modFunction.vb
Imports System.Data
Imports System.Data.OleDb
Module modFunction

    ' / --------------------------------------------------------------------------------
    ' / Initialized Management
    Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringW" ( _
      ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, _
      ByVal lpString As String, _
      ByVal lpFileName As String _
      ) As Int32

    Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" ( _
      ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, _
      ByVal lpDefault As String, _
      ByVal lpReturnedString As String, _
      ByVal nSize As Int32, _
      ByVal lpFileName As String _
      ) As Int32
    ' / --------------------------------------------------------------------------------

    ' / --------------------------------------------------------------------------------
    Public Sub WriteIni(ByVal iniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamVal As String)
      Dim Result As Integer = WritePrivateProfileString(Section, ParamName, ParamVal, iniFileName)
    End Sub

    Public Function ReadIni(ByVal IniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamDefault As String) As String
      Dim ParamVal As String = Space$(1024)
      Dim LenParamVal As Long = GetPrivateProfileString(Section, ParamName, ParamDefault, ParamVal, Len(ParamVal), IniFileName)
      ReadIni = Left$(ParamVal, LenParamVal)
    End Function

    ' / --------------------------------------------------------------------------------
    ' / Get my project path
    ' / AppPath = C:\My Project\bin\debug
    ' / Replace "\bin\debug" with "\"
    ' / Return : C:\My Project\
    Function MyPath(ByVal AppPath As String) As String
      '/ MessageBox.Show(AppPath);
      AppPath = AppPath.ToLower()
      '/ Return Value
      MyPath = AppPath.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) <> "\" Then MyPath = MyPath & "\"
    End Function

    ' / --------------------------------------------------------------------------------
    ' / ฟังค์ชั่นในการป้อนเฉพาะค่าตัวเลขได้เท่านั้น
    Function CheckDigitOnly(ByVal index As Integer) As Boolean
      Select Case index
            Case 48 To 57 ' เลข 0 - 9
                CheckDigitOnly = False
            Case 8, 13 ' Backspace = 8, Enter = 13
                CheckDigitOnly = False
            Case Else
                CheckDigitOnly = True
      End Select
    End Function
End Module
ดาวน์โหลดโค้ดต้นฉบับเต็ม VB.NET (2010) ได้ที่นี่

opekzaa โพสต์ 2018-12-27 16:19:31

:D:D:D:D:D:D:D:D:D

GOTT โพสต์ 2019-4-29 10:54:13

:D

ดีครับ

Peewoy122 โพสต์ 2019-5-1 23:02:05

รบกวนถามครับ ทำยังไงให้เรียกพิมพ์ แบบselect all ได้ทั้งหมดครับ บางทีเลือกพิมพ์ pageได้บางทีก็เลือไม่ได้ครับ

thongkorn โพสต์ 2019-5-2 18:45:58

Peewoy122 ตอบกลับเมื่อ 2019-5-1 23:02
รบกวนถามครับ ทำยังไงให้เรียกพิมพ์ แบบselect all ได้ทั้งห ...

ก็ปกติน่ะครับ ลองดาวน์โหลดโปรแกรมใหม่ได้ที่นี่

MrDen โพสต์ 2020-3-18 06:40:18

ขอบคุณครับ

หนึ่ง โพสต์ 2021-8-15 07:03:28

ขอบคุณครับ อาจารย์

kimnue โพสต์ 2022-10-24 03:12:44

แจ่มมากครับ อบตรงมีโ๕้ดให้ไปศึกษา
หน้า: [1]
ดูในรูปแบบกติ: แจกฟรีทั้งโค้ด และโปรแกรมการพิมพ์ใบแจ้งค่าใช้จ่าย สำหรับหอพัก ห้องเช่า อพาร์ทเมนท์ แบบกระดาษครึ่ง A4 (VB.NET เวอร์ชั่น II)