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

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

[VB.NET] การสร้าง NumericUpDown หรือปุ่มเพิ่มลดค่ามาใส่ไว้ในตารางกริดแบบไดนามิค

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

311

กระทู้

502

โพสต์

6072

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6072



บทความนี้เป็นการขยายขีดความสามารถของตารางกริด (DataGridView) ซึ่งเป็น Control (หรือ Component) พื้นฐานที่ติดมาจากไมโครซอฟท์ ก็จะคล้ายๆกับ การนำเอา DateTimePicker และปุ่มคำสั่ง Button มาแสดงผลในตารางกริด นัั่นเอง แต่ครั้งนี้จะเป็น การนำเอาปุ่มที่เพิ่มหรือลดค่าตัวเลขแบบจำนวนเต็ม (NumericUpDown) มาใส่เข้าไปตารางกริดแทน (กำหนดค่า 1 - 100) แต่ทว่าจะมีอะไรพิเศษที่แตกต่างไปจากของเดิมเล็กน้อย เพราะจะเกิด 2 เหตุการณ์คือ การนำเมาส์ไปคลิ๊กที่เซลล์ของตารางกริดที่เราต้องการ จากนั้นตัว NumericUpDown ก็จะปรากฏขึ้นมา และการกดคีย์จากแป้นพิมพ์ลงไปในเซลล์ของตารางกริดแทน เพื่อป้องกันการกดค่าคีย์ที่เราไม่ต้องการ นอกจากตัวเลข 0 ถึง 9 เท่านั้น ...

เหตุการณ์ในการนำเมาส์ไปคลิ๊กที่เซลล์ของตารางกริด จะบังคับให้ตัว NumericUpDown ขึ้นมาแสดงผลทับลงไปในเซลล์แทน (โฟกัสไปในหลักที่ 3 หรือ Index = 2) ...
  1.     '// If users click cell (index = 2).
  2.     Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  3.         Select Case e.ColumnIndex
  4.             '// NumericUpDown
  5.             Case 2
  6.                 '//Adding NumericUpDown control into DataGridView   
  7.                 DataGridView1.Controls.Add(nUpDown)

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

  10.                 '//Setting area for NumericUpDown Control
  11.                 nUpDown.Size = New Size(oRectangle.Width, oRectangle.Height)

  12.                 '// Setting Location
  13.                 nUpDown.Location = New Point(oRectangle.X, oRectangle.Y)
  14.                 '// Read value from DataGridView into NumericUpDown
  15.                 nUpDown.Value = DataGridView1.CurrentCell.Value
  16.                 '// Now make it visible  
  17.                 nUpDown.Visible = True
  18.                 '// Force to change date value at nUpDown_ValueChanged Event.
  19.                 AddHandler nUpDown.ValueChanged, AddressOf nUpDown_ValueChanged

  20.                 '// Delete Button
  21.             Case 3
  22.                 'MsgBox(("Row : " + e.RowIndex.ToString & "  Col : ") + e.ColumnIndex.ToString)
  23.                 Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name
  24.                 If colName = "btnDelRow" Then
  25.                     '// Delete current row from DataGridView1
  26.                     DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
  27.                 End If
  28.         End Select
  29.     End Sub
คัดลอกไปที่คลิปบอร์ด

เหตุการณ์ที่เกิดการกดคีย์จากแป้นพิมพ์ ทำให้เราโฟกัสการป้อนค่าในเซลล์นั้นๆ แล้วทำการเช็คค่าคีย์ที่กดลงไป ...
  1.     ' / --------------------------------------------------------------------------------
  2.     Private Sub DataGridView1_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
  3.         Select Case DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Index
  4.             '// ColumeIndex 2 is an integer value.
  5.             Case 2
  6.                 '// Force to validate value at ValidKeyPress Event.
  7.                 RemoveHandler e.Control.KeyPress, AddressOf ValidKeyPress
  8.                 AddHandler e.Control.KeyPress, AddressOf ValidKeyPress
  9.         End Select
  10.     End Sub
คัดลอกไปที่คลิปบอร์ด

โปรแกรมย่อยตรวจสอบการกดแป้นพิมพ์ โดยรับค่าได้เฉพาะตัวเลข 0 - 9 และ Backspace เท่านั้น ...
  1.     ' / --------------------------------------------------------------------------------
  2.     Private Sub ValidKeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
  3.         Select Case DataGridView1.CurrentCell.ColumnIndex
  4.             Case 2  '// Integer
  5.                 Select Case e.KeyChar
  6.                     Case "0" To "9"   ' digits 0 - 9 allowed
  7.                     Case ChrW(Keys.Back)    ' backspace allowed for deleting (Delete key automatically overrides)
  8.                     Case Else ' everything else ....
  9.                         '// True = CPU cancel the KeyPress event
  10.                         e.Handled = True '// and it's just like you never pressed a key at all.
  11.                 End Select
  12.         End Select
  13.     End Sub
คัดลอกไปที่คลิปบอร์ด

มาดูโค้ดรวมทั้งหมดกันเถอะ ...
  1. #Region "ABOUT"
  2. ' / --------------------------------------------------------------------------------
  3. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  4. ' / eMail : thongkorn@hotmail.com
  5. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  6. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  7. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  8. ' / More: http://www.g2gnet.com/webboard
  9. ' / Purpose: Adding NumericUpDown and Button control into DataGridView @Runtime.
  10. ' / Microsoft Visual Basic .NET (2010)
  11. ' /
  12. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  13. ' / You can modify and/or distribute without to inform the developer.
  14. ' / --------------------------------------------------------------------------------
  15. #End Region

  16. Public Class frmDataGridUpDown
  17.     '// Declare variable object of NumericUpDown
  18.     Dim nUpDown As New NumericUpDown
  19.     Dim LastValue As Integer

  20.     Private Sub frmDataGridUpDown_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  21.         '// Initialize DataGridView Control
  22.         With DataGridView1
  23.             .AllowUserToAddRows = False
  24.             .AllowUserToDeleteRows = False
  25.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  26.             .AutoResizeColumns()
  27.             .RowTemplate.Height = 40
  28.         End With
  29.         '// Declare columns type.
  30.         Dim Column1 As New DataGridViewTextBoxColumn()
  31.         Dim Column2 As New DataGridViewTextBoxColumn()
  32.         Dim Column3 As New DataGridViewTextBoxColumn()
  33.         '// Add new Columns
  34.         DataGridView1.Columns.AddRange(New DataGridViewColumn() { _
  35.                 Column1, Column2, Column3 _
  36.                 })
  37.         With DataGridView1
  38.             .Columns(0).Name = "Product ID"
  39.             .Columns(1).Name = "Product Name"
  40.             .Columns(2).Name = "NumericUpDown"
  41.         End With
  42.         '// Add 4th column (Index = 3), It's Button.
  43.         Dim btn As New DataGridViewButtonColumn()
  44.         DataGridView1.Columns.Add(btn)
  45.         With btn
  46.             .HeaderText = ""
  47.             .Text = "Delete"
  48.             .Name = "btnDelRow"
  49.             .UseColumnTextForButtonValue = True
  50.             .Width = 120
  51.         End With
  52.         ' Adjust Header Styles
  53.         With DataGridView1.ColumnHeadersDefaultCellStyle
  54.             .BackColor = Color.Navy
  55.             .ForeColor = Color.Black
  56.             .Font = New Font("Century Gothic", 18, FontStyle.Bold)
  57.             .WrapMode = DataGridViewTriState.False
  58.         End With
  59.         With DataGridView1
  60.             .Font = New Font("Century Gothic", 20)
  61.         End With

  62.         '// SAMPLE DATA
  63.         Dim RandomClass As New Random()
  64.         '// DateTime.Today.AddDays(-RandomClass.Next(365)) --> Random past date 365 days.
  65.         Dim row As String() = New String() { _
  66.             "1", "Product 1", RandomClass.Next(99)}
  67.         DataGridView1.Rows.Add(row)
  68.         row = New String() { _
  69.             "2", "Product 2", RandomClass.Next(99)}
  70.         DataGridView1.Rows.Add(row)
  71.         row = New String() { _
  72.             "3", "Product 3", RandomClass.Next(99)}
  73.         DataGridView1.Rows.Add(row)
  74.         row = New String() { _
  75.             "4", "Product 4", RandomClass.Next(99)}
  76.         '// Add new row.
  77.         DataGridView1.Rows.Add(row)
  78.         '// Initialize NumericUpDown @Run Time
  79.         With nUpDown
  80.             .DecimalPlaces = 0
  81.             .Increment = 1
  82.             .Maximum = 100
  83.             .Minimum = 1
  84.             .ReadOnly = True
  85.         End With
  86.     End Sub

  87.     '// If users click cell (index = 2).
  88.     Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  89.         Select Case e.ColumnIndex
  90.             '// NumericUpDown
  91.             Case 2
  92.                 '//Adding NumericUpDown control into DataGridView   
  93.                 DataGridView1.Controls.Add(nUpDown)

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

  96.                 '//Setting area for NumericUpDown Control
  97.                 nUpDown.Size = New Size(oRectangle.Width, oRectangle.Height)

  98.                 '// Setting Location
  99.                 nUpDown.Location = New Point(oRectangle.X, oRectangle.Y)
  100.                 '// Read value from DataGridView into NumericUpDown
  101.                 nUpDown.Value = DataGridView1.CurrentCell.Value
  102.                 '// Now make it visible  
  103.                 nUpDown.Visible = True
  104.                 '// Force to change date value at nUpDown_ValueChanged Event.
  105.                 AddHandler nUpDown.ValueChanged, AddressOf nUpDown_ValueChanged

  106.                 '// Delete Button
  107.             Case 3
  108.                 'MsgBox(("Row : " + e.RowIndex.ToString & "  Col : ") + e.ColumnIndex.ToString)
  109.                 Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name
  110.                 If colName = "btnDelRow" Then
  111.                     '// Delete current row from DataGridView1
  112.                     DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
  113.                 End If
  114.         End Select
  115.     End Sub

  116.     '// Event Handler when the change value in NumericUpDown.
  117.     Private Sub nUpDown_ValueChanged(sender As Object, e As System.EventArgs)
  118.         '// Display numeric value.
  119.         DataGridView1.CurrentCell.Value = Val(nUpDown.Value)
  120.     End Sub

  121.     '// Event when the change value in Current Cell.
  122.     Private Sub DataGridView1_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
  123.         Select Case e.ColumnIndex
  124.             Case 2
  125.                 If Val(DataGridView1.CurrentCell.Value.ToString) > 100 Then
  126.                     DataGridView1.CurrentCell.Value = LastValue
  127.                 ElseIf Val(DataGridView1.CurrentCell.Value.ToString) <= 0 Then
  128.                     DataGridView1.CurrentCell.Value = LastValue
  129.                 Else
  130.                     DataGridView1.CurrentCell.Value = Val(DataGridView1.CurrentCell.Value)
  131.                 End If
  132.         End Select
  133.     End Sub

  134.     Private Sub DataGridView1_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
  135.         Select Case e.ColumnIndex
  136.             Case 2
  137.                 '// Keep last value before edit.
  138.                 LastValue = Val(DataGridView1.CurrentCell.Value)
  139.         End Select
  140.     End Sub

  141.     Private Sub DataGridView1_CellLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
  142.         '// Hidden Value
  143.         nUpDown.Visible = False
  144.     End Sub

  145.     Private Sub frmDataGridUpDown_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
  146.         nUpDown.Visible = False
  147.     End Sub

  148.     ' / --------------------------------------------------------------------------------
  149.     Private Sub DataGridView1_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
  150.         Select Case DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Index
  151.             '// ColumeIndex 2 is an integer value.
  152.             Case 2
  153.                 '// Force to validate value at ValidKeyPress Event.
  154.                 RemoveHandler e.Control.KeyPress, AddressOf ValidKeyPress
  155.                 AddHandler e.Control.KeyPress, AddressOf ValidKeyPress
  156.         End Select
  157.     End Sub

  158.     ' / --------------------------------------------------------------------------------
  159.     Private Sub ValidKeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
  160.         Select Case DataGridView1.CurrentCell.ColumnIndex
  161.             Case 2  '// Integer
  162.                 Select Case e.KeyChar
  163.                     Case "0" To "9"   ' digits 0 - 9 allowed
  164.                     Case ChrW(Keys.Back)    ' backspace allowed for deleting (Delete key automatically overrides)
  165.                     Case Else ' everything else ....
  166.                         '// True = CPU cancel the KeyPress event
  167.                         e.Handled = True '// and it's just like you never pressed a key at all.
  168.                 End Select
  169.         End Select
  170.     End Sub
  171. End Class
คัดลอกไปที่คลิปบอร์ด

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

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

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

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

0

กระทู้

3

โพสต์

67

เครดิต

ผู้ดูแลพิเศษ

Rank: 8Rank: 8

เครดิต
67
โพสต์ 2019-3-17 20:24:34 | ดูโพสต์ทั้งหมด

คล้ายๆ จะตอบโจทย์ผมได้เลยครับ

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

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

x

311

กระทู้

502

โพสต์

6072

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6072
 เจ้าของ| โพสต์ 2019-3-18 12:11:32 | ดูโพสต์ทั้งหมด

downrung ตอบกลับเมื่อ 2019-3-17 20:24
คล้ายๆ จะตอบโจทย์ผมได้เลยครับ

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

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

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

GMT+7, 2024-5-6 00:13 , Processed in 0.399976 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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