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

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

[VB.NET] การสร้างหลัก Column Countdown เพื่อเปรียบเทียบและนับเวลาถอยหลังในตารางกริด

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

308

กระทู้

498

โพสต์

5971

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
5971




จากคำถามที่ถามมาในเว็บบอร์ด โจทย์ก็ธรรมดาแต่น่าสนใจดี แอดมินเลยมานำเสนอกระบวนการคิดในการแก้ปัญหา และเทคนิคการนำเอา DateTimePicker มาใส่ไว้ใน DataGridView โดยเราจะต้องเขียนโค้ด หรือที่เรียกว่า Run-Time (เห็นผลก็ต่อเมื่อรันโปรแกรม) ซึ่งมันจะมีประสิทธิภาพที่สูงกว่าการ Design-Time (ออกแบบอย่างไรจะมองเห็นผลได้ทันที) ... หลักการคือเพิ่มแถวรายการขึ้นมาก่อน จากนั้นทำการเปลี่ยนวันที่ เวลา แล้วนำเวลาในตารางกริดในแต่ละแถว ไปเปรียบเทียบกับเวลาปัจจุบัน หากเวลาในตารางกริดยังมากกว่าเวลาปัจจุบัน มันก็จะนับเวลาถอยหลังไปเรื่อย จนกว่าจะน้อยกว่าเวลาปัจจุบัน ก็จะหยุดการนับ


มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Public Class frmCountDownGrid
  2.     '// Declare object of the DateTimePicker.
  3.     Dim oDTP As New DateTimePicker

  4.     Private Sub frmCountDownGrid_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  5.         Me.Dispose()
  6.         GC.SuppressFinalize(Me)
  7.     End Sub

  8.     '// KeyDown Function Keys.
  9.     Private Sub frmCountDownGrid_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  10.         Select Case e.KeyCode
  11.             Case Keys.F3
  12.                 Call AddRow()
  13.             Case Keys.F5
  14.                 Call btnRemoveRow_Click(sender, e)
  15.             Case Keys.F10
  16.                 Me.Close()
  17.         End Select

  18.     End Sub

  19.     Private Sub frmCountDownGrid_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  20.         Me.KeyPreview = True    '// For KeyDown Event.
  21.         With dgvData
  22.             '// Autosize Column
  23.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  24.             .AllowUserToAddRows = False
  25.             .SelectionMode = DataGridViewSelectionMode.CellSelect
  26.             '// Data rows
  27.             .Font = New Font("Tahoma", 9)
  28.             ' Adjust Header Styles
  29.             With .ColumnHeadersDefaultCellStyle
  30.                 .BackColor = Color.Navy
  31.                 .ForeColor = Color.Black ' Color.White
  32.                 .Font = New Font("Tahoma", 9, FontStyle.Bold)
  33.             End With
  34.             .RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty
  35.         End With
  36.         '// Declare columns type.
  37.         Dim ProjectName As New DataGridViewTextBoxColumn()
  38.         Dim Deadline As New DataGridViewTextBoxColumn
  39.         Dim CountDown As New DataGridViewTextBoxColumn()
  40.         '// Add new Columns
  41.         dgvData.Columns.AddRange(New DataGridViewColumn() { _
  42.                                  ProjectName, Deadline, CountDown _
  43.                                 })

  44.         With dgvData
  45.             .Columns(0).HeaderText = "Project Name"
  46.             .Columns(1).HeaderText = "Deadline"
  47.             .Columns(2).HeaderText = "Countdown"
  48.             .Columns(2).DefaultCellStyle.Format = "dd/MM/yyyy HH:mm:ss"
  49.             .Columns(2).DataPropertyName = "Date"
  50.             '// Add 4th column (Index = 3), It's Button.
  51.             Dim btnAddRow As New DataGridViewButtonColumn()
  52.             dgvData.Columns.Add(btnAddRow)
  53.             With btnAddRow
  54.                 .HeaderText = ""
  55.                 .Text = "Add"
  56.                 .Name = "btnAddRow"
  57.                 .UseColumnTextForButtonValue = True
  58.                 .Width = 60
  59.             End With
  60.             '// Add 5th column (Index = 4), It's Button.
  61.             Dim btnRemoveRow As New DataGridViewButtonColumn()
  62.             dgvData.Columns.Add(btnRemoveRow)
  63.             With btnRemoveRow
  64.                 .HeaderText = ""
  65.                 .Text = "Delete"
  66.                 .Name = "btnRemoveRow"
  67.                 .UseColumnTextForButtonValue = True
  68.                 .Width = 60
  69.             End With
  70.         End With
  71.         '//
  72.         Timer1.Interval = 1000  '// Refresh every 1 second.
  73.         Timer1.Enabled = True
  74.         lblCompareTime.Text = Format(Now, "dd/MM/yyyy HH:mm:ss")
  75.     End Sub

  76.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
  77.         For row As Integer = 0 To dgvData.Rows.Count - 1
  78.             Dim StartDate As Date = dgvData.Rows(row).Cells(1).Value
  79.             Dim EndDate As Date = Date.Now
  80.             Dim TimeSpan As TimeSpan = StartDate.Subtract(EndDate)
  81.             Dim DifDays As Integer = TimeSpan.Days
  82.             Dim DifHr As Integer = TimeSpan.Hours
  83.             Dim DifMin As Integer = TimeSpan.Minutes
  84.             Dim DifSec As Integer = TimeSpan.Seconds
  85.             '// เปรียบเทียบเวลา Deadline กับเวลาปัจจุบัน (lblCompareTime) ที่จะเดินหน้าทุกวินาที
  86.             '// หากยังมีค่าไม่เท่ากัน ก็นับถอยหลังลงไปเรื่อยๆ จนกว่า Deadline จะมีค่าที่ต่ำกว่าเวลาปัจจุบัน
  87.             lblCompareTime.Text = Format(Now, "dd/MM/yyyy HH:mm:ss")
  88.             If dgvData.Rows(row).Cells(1).Value > CDate(lblCompareTime.Text) Then
  89.                 '// ถอยหลังเวลาลงไปเรื่อยๆ
  90.                 dgvData.Rows(row).Cells(2).Value = DifDays & " วัน, " & DifHr & " ชั่วโมง, " & DifMin & " นาที, " & DifSec & " วินาที."

  91.             ElseIf dgvData.Rows(row).Cells(1).Value <= CDate(lblCompareTime.Text) Then
  92.                 dgvData.Rows(row).Cells(2).Value = "0 วัน, 0 ชั่วโมง, 0 นาที, 0 วินาที."
  93.             End If
  94.         Next
  95.     End Sub

  96.     '// Add new row.
  97.     Private Sub AddRow()
  98.         Dim row As String() = New String() {"Project", Now(), "", Now()}
  99.         dgvData.Rows.Add(row)
  100.     End Sub

  101.     '/ Event is clicked in the cell.
  102.     Private Sub dgvData_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellClick
  103.         Select Case e.ColumnIndex
  104.             '// DateTimePicker
  105.             Case 1
  106.                 '//Adding DateTimePicker control into DataGridView   
  107.                 dgvData.Controls.Add(oDTP)

  108.                 '// Setting the custom format.
  109.                 oDTP.Format = DateTimePickerFormat.Custom
  110.                 oDTP.CustomFormat = "dd/MM/yyyy HH:mm:ss"
  111.                 oDTP.ShowUpDown = True

  112.                 '// It returns the retangular area that represents the Display area for a cell  
  113.                 Dim oRectangle = dgvData.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)

  114.                 '//Setting area for DateTimePicker Control  
  115.                 oDTP.Size = New Size(oRectangle.Width, oRectangle.Height)

  116.                 '// Setting Location
  117.                 oDTP.Location = New Point(oRectangle.X, oRectangle.Y)
  118.                 '// Read value from DataGridView into DateTimePicker
  119.                 oDTP.Value = dgvData.CurrentCell.Value
  120.                 '// Now make it visible  
  121.                 oDTP.Visible = True
  122.                 '// Force to change date value at oDTP_ValueChanged Event.
  123.                 AddHandler oDTP.ValueChanged, AddressOf oDTP_ValueChanged

  124.                 '// Add Button
  125.             Case 3
  126.                 Call AddRow()
  127.                 '// Delete Button
  128.             Case 4
  129.                 Call btnRemoveRow_Click(sender, e)
  130.         End Select
  131.     End Sub

  132.     '// Event Handler when the change value in DateTimePicker.
  133.     Private Sub oDTP_ValueChanged(sender As Object, e As System.EventArgs)
  134.         '// Display date value and copy the value into current cell.
  135.         dgvData.CurrentCell.Value = Format(oDTP.Value, "dd/MM/yyyy HH:mm:ss")
  136.     End Sub

  137.     Private Sub dgvData_CellLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellLeave
  138.         '// Hidden DateTimePicker when lost focus the cell.
  139.         oDTP.Visible = False
  140.     End Sub

  141.     Private Sub frmCountDownGrid_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
  142.         oDTP.Visible = False
  143.     End Sub

  144.     Private Sub btnAddRow_Click(sender As System.Object, e As System.EventArgs) Handles btnAddRow.Click
  145.         Call AddRow()
  146.     End Sub

  147.     Private Sub btnRemoveRow_Click(sender As System.Object, e As System.EventArgs) Handles btnRemoveRow.Click
  148.         If dgvData.RowCount = 0 Then Exit Sub
  149.         dgvData.Rows.Remove(dgvData.CurrentRow)
  150.     End Sub
  151. End Class
คัดลอกไปที่คลิปบอร์ด


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

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

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

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

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

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

GMT+7, 2024-3-29 07:27 , Processed in 0.190345 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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