ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 4583|ตอบกลับ: 1

[VB.NET] การคำนวณหาเวลาการเข้าออกของพนักงาน (Time Attendance) ในระดับชั่วโมงและนาที

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6050

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6050




แอดมินเคยเขียนโค้ดแจกให้ไปแล้วทั้ง VB6 และ VBA โดยสามารถตามกลับไปอ่านได้จาก [VB6] การคำนวณหาเวลาการเข้าออกของพนักงาน (Time Attendance) ในระดับนาที และ [VBA] การคำนวณหาเวลาการเข้ามาทำงานของพนักงาน (Time Attendance) ... สำหรับตอนนี้จะเป็น VB.NET กันบ้างครับ



ข้อมูลที่ได้รับมาจากเครื่องสแกนลายนิ้วมือ (Finger Scan) ซึ่งชุดข้อมูลจะถูกแยกออกจากกันด้วย Space หรือ CHR(32)


มาดูโค้ดกันเถอะ ...
  1. ' / ------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / Purpose: Time attendance & calculate difference time.
  8. ' / Microsoft Visual Basic .NET (2010)
  9. ' /
  10. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  11. ' / You can modify and/or distribute without to inform the developer.
  12. ' / ------------------------------------------------------------------

  13. Public Class frmCalTime

  14.     ' / ------------------------------------------------------------------
  15.     ' / Get my project path
  16.     ' / AppPath = C:\My Project\bin\debug
  17.     ' / Replace "\bin\debug" with ""
  18.     ' / Return : C:\My Project\
  19.     Function MyPath(ByVal AppPath As String) As String
  20.         '/ MessageBox.Show(AppPath);
  21.         AppPath = AppPath.ToLower()
  22.         '/ Return Value
  23.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  24.         '// If not found folder then put the \ (BackSlash) at the end.
  25.         If Microsoft.VisualBasic.Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
  26.     End Function

  27.     ' / ------------------------------------------------------------------
  28.     ' / Browse text file and calculate difference time.
  29.     Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
  30.         '/ Declaration Open File Dialog @Run Time
  31.         Dim dlgOpenFile As OpenFileDialog = New OpenFileDialog()

  32.         ' / Open File Dialog
  33.         With dlgOpenFile
  34.             .InitialDirectory = MyPath(Application.StartupPath)
  35.             .Title = "Select text file."

  36.             .Filter = "Text Files (*.txt)|*.txt"
  37.             .FilterIndex = 1
  38.             .RestoreDirectory = True
  39.         End With
  40.         ' Choose OK button after Browse ...
  41.         If dlgOpenFile.ShowDialog() = DialogResult.OK Then

  42.             txtFileName.Text = dlgOpenFile.FileName
  43.             ' / Initialize DataGridView
  44.             With dgvData
  45.                 .ColumnHeadersVisible = False
  46.                 .AlternatingRowsDefaultCellStyle.BackColor = Color.LimeGreen
  47.                 .DefaultCellStyle.SelectionBackColor = Color.LightSteelBlue
  48.                 .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  49.                 .RowTemplate.Height = 22
  50.                 .ReadOnly = True
  51.                 .MultiSelect = False
  52.                 .Columns.Clear()
  53.                 '// Autosize Column
  54.                 .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  55.                 .AutoResizeColumns()
  56.             End With

  57.             Dim FileName As String = dlgOpenFile.FileName
  58.             '/ Using a Chr(32) or Space delimited text file.
  59.             Dim Lines = (From line In IO.File.ReadAllLines(FileName)
  60.                          Select line.Split(Chr(32))).ToArray
  61.             '/ Read for each column
  62.             For x As Integer = 0 To Lines(0).GetUpperBound(0)
  63.                 dgvData.Columns.Add(Lines(0)(x), Lines(0)(x))
  64.             Next
  65.             '/ Add new column for calculate time.
  66.             dgvData.Columns.Add("CalDiff", "Difference Time")

  67.             '/ Read line by line for each rows
  68.             For x As Integer = 0 To Lines.GetUpperBound(0)
  69.                 dgvData.Rows.Add(Lines(x))
  70.             Next
  71.             '/ Difference Time
  72.             For sRow = 0 To dgvData.RowCount - 1
  73.                 '// Time in
  74.                 If (sRow Mod 2) = 0 Then
  75.                     '// Compare with Time IN = 08:00 AM
  76.                     dgvData.Rows(sRow).Cells(dgvData.Columns.Count - 1).Value = CalTime("08:00", dgvData.Rows(sRow).Cells(dgvData.Columns.Count - 2).Value)
  77.                 Else
  78.                     '// Compare with Time OUT = 17:00 PM
  79.                     dgvData.Rows(sRow).Cells(dgvData.Columns.Count - 1).Value = CalTime("17:00", dgvData.Rows(sRow).Cells(dgvData.Columns.Count - 2).Value)
  80.                 End If
  81.             Next
  82.         End If
  83.     End Sub

  84.     ' / ------------------------------------------------------------------
  85.     ' / StartTime is IN/OUT reference time, EndTime is actual employee time.
  86.     Public Function CalTime(StartTime As DateTime, ByVal EndTime As DateTime) As String
  87.         CalTime = ""
  88.         Dim dFrom As DateTime
  89.         Dim dTo As DateTime
  90.         Dim sDateFrom As String
  91.         Dim sDateTo As String
  92.         '/ Format the date before processing.
  93.         sDateFrom = FormatDateTime(StartTime, DateFormat.ShortTime)
  94.         sDateTo = FormatDateTime(EndTime, DateFormat.ShortTime)
  95.         '/
  96.         If DateTime.TryParse(sDateFrom, dFrom) AndAlso DateTime.TryParse(sDateTo, dTo) Then
  97.             Dim TS As TimeSpan = dTo - dFrom
  98.             Dim hour As Integer = TS.Hours
  99.             Dim mins As Integer = TS.Minutes
  100.             Dim secs As Integer = TS.Seconds
  101.             Dim TimeDiff As String = "0"
  102.             If hour <> 0 Then
  103.                 TimeDiff = (hour.ToString & " Hr ") + mins.ToString & " Min"
  104.             ElseIf mins <> 0 Then
  105.                 TimeDiff = mins.ToString & " Min"
  106.             End If
  107.             '/ You can defined to format the problem.
  108.             '/ Sample: Output 16 mins in format 00:16:00
  109.             'TimeDiff = ((hour.ToString("00") & ":") + mins.ToString("00") & ":") + secs.ToString("00")
  110.             CalTime = TimeDiff
  111.         End If
  112.     End Function

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

  116.     Private Sub frmCalTime_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  117.         Me.Dispose()
  118.         Application.Exit()
  119.     End Sub

  120.     Private Sub txtFileName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtFileName.KeyPress
  121.         '// Lock any key press.
  122.         e.Handled = True
  123.     End Sub
  124. End Class
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

เครดิต
10
โพสต์ 2022-10-25 20:01:00 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-25 10:35 , Processed in 0.412963 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้