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

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

[VB.NET] การสร้าง Context Menu แสดงผลในตารางกริด แบบ @RunTime

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

311

กระทู้

502

โพสต์

6072

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6072



Context Menu คือ หน้าเมนู Pop up ที่แสดงขึ้นเวลาที่เราทำการกดคลิกขวาที่เมาส์ ซึ่งจะแสดงรายละเอียดหรือทางลัดต่างๆตามที่เรากำหนด วันนี้แอดมินจะขอนำเสนอการสร้าง Context Menu เพื่อแสดงผลในตารางกริด (DataGridView) ด้วยวิธีการแบบ @Run Time หมายความว่า เราไม่ได้ทำการลาก ContextMenuStrip มาจากกลุ่มของเครื่องมือ (ToolBox) แต่สร้างขึ้นมาจากโค้ดแทน ซึ่งเป็นวิธีการที่ยืดหยุ่น และทรงประสิทธิภาพในการเขียนโปรแกรมด้วยภาษา Visual Basic ... ดังนั้นมิตรรักแฟนคลับจึงจำเป็นที่ต้องศึกษาโดยผ่านทางโค้ดแทนครับผม โดยตัวอย่างนี้จะเกิดการคลิกขวาที่เมาส์ จากนั้นให้ทำการเลือกแก้ไข (Edit) หรือลบแถวรายการ (Delete) ออกไป ...


การสร้าง Context Menu แบบ Run Time ...
  1.     Dim _ContextMenu As New ContextMenuStrip

  2.         '// Add new contextmenu
  3.         _contextmenu.Items.Add("Edit")
  4.         _contextmenu.Items.Add("Delete")
  5.         AddHandler _ContextMenu.ItemClicked, AddressOf Contextmenu_Click
  6.         '// IMPORTANT
  7.         For Each rw As DataGridViewRow In dgvData.Rows
  8.             For Each c As DataGridViewCell In rw.Cells
  9.                 c.ContextMenuStrip = _ContextMenu
  10.             Next
  11.         Next
คัดลอกไปที่คลิปบอร์ด

เหตุการณ์ตรวจสอบการคลิกขวาที่เมาส์หรือไม่ ...
  1.     Private Sub dgvData_CellMouseUp(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvData.CellMouseUp
  2.         '// RIGHT CLICKS
  3.         If e.Button = Windows.Forms.MouseButtons.Right Then
  4.             dgvData.Rows(e.RowIndex).Selected = True
  5.             '// Get the row.
  6.             rowIndex = e.RowIndex
  7.         End If
  8.     End Sub
คัดลอกไปที่คลิปบอร์ด


เหตุการณ์ที่เราสร้างขึ้นมา เพื่อให้เกิด Driven หรือสั่งไปทำอะไร ... ตัวอย่างนี้คือการเลือกรายการแถวมาแก้ไข หรือลบแถวออกไป
  1.     '// Force Event contextmenu_Click
  2.     Private Sub Contextmenu_Click(ByVal sender As System.Object, ByVal e As ToolStripItemClickedEventArgs)
  3.         '// Choose item from contextmenu.
  4.         Select Case e.ClickedItem.Text
  5.             Case "Edit"
  6.                 _ContextMenu.Visible = False
  7.                 MessageBox.Show("You select Primary Key = " & dgvData.Rows(rowIndex).Cells(0).Value & " to Edit.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  8.             Case "Delete"
  9.                 _ContextMenu.Visible = False
  10.                 MessageBox.Show("You select Primary Key = " & dgvData.Rows(rowIndex).Cells(0).Value & " to Delete.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  11.                 dgvData.Rows.RemoveAt(rowIndex)
  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. ' / Purpose: Context Menu in DataGridView @Run Time.
  9. ' / Microsoft Visual Basic .NET (2010)
  10. ' /
  11. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  12. ' / You can modify and/or distribute without to inform the developer.
  13. ' / --------------------------------------------------------------------
  14. #End Region
  15. Public Class frmContextMenu
  16.     Dim rowIndex As Integer = 0
  17.     Dim _ContextMenu As New ContextMenuStrip

  18.     Private Sub frmContextMenu_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  19.         Call InitializeGrid()
  20.         Call FillSampleData()
  21.         '// Add new contextmenu
  22.         _contextmenu.Items.Add("Edit")
  23.         _contextmenu.Items.Add("Delete")
  24.         AddHandler _ContextMenu.ItemClicked, AddressOf Contextmenu_Click
  25.         '// IMPORTANT
  26.         For Each rw As DataGridViewRow In dgvData.Rows
  27.             For Each c As DataGridViewCell In rw.Cells
  28.                 c.ContextMenuStrip = _ContextMenu
  29.             Next
  30.         Next
  31.     End Sub

  32.     Private Sub dgvData_CellMouseUp(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvData.CellMouseUp
  33.         '// RIGHT CLICKS
  34.         If e.Button = Windows.Forms.MouseButtons.Right Then
  35.             dgvData.Rows(e.RowIndex).Selected = True
  36.             '// Get the row.
  37.             rowIndex = e.RowIndex
  38.         End If
  39.     End Sub

  40.     '// Force Event contextmenu_Click
  41.     Private Sub Contextmenu_Click(ByVal sender As System.Object, ByVal e As ToolStripItemClickedEventArgs)
  42.         '// Choose item from contextmenu.
  43.         Select Case e.ClickedItem.Text
  44.             Case "Edit"
  45.                 _ContextMenu.Visible = False
  46.                 MessageBox.Show("You select Primary Key = " & dgvData.Rows(rowIndex).Cells(0).Value & " to Edit.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  47.             Case "Delete"
  48.                 _ContextMenu.Visible = False
  49.                 MessageBox.Show("You select Primary Key = " & dgvData.Rows(rowIndex).Cells(0).Value & " to Delete.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  50.                 dgvData.Rows.RemoveAt(rowIndex)
  51.         End Select
  52.     End Sub

  53.     ' / --------------------------------------------------------------------------------
  54.     '// Default settings for Grids in @Run Time
  55.     Private Sub InitializeGrid()
  56.         With dgvData
  57.             .RowHeadersVisible = False
  58.             .AllowUserToAddRows = False
  59.             .AllowUserToDeleteRows = False
  60.             .AllowUserToResizeRows = False
  61.             .MultiSelect = False
  62.             .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  63.             .ReadOnly = True
  64.             .Font = New Font("Tahoma", 9)
  65.             .RowHeadersVisible = True
  66.             .AlternatingRowsDefaultCellStyle.BackColor = Color.Orange
  67.             .DefaultCellStyle.SelectionBackColor = Color.LightSteelBlue
  68.             '/ Auto size column width of each main by sorting the field.
  69.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  70.             .AutoResizeColumns()
  71.             '/ Adjust Header Styles
  72.             With .ColumnHeadersDefaultCellStyle
  73.                 .BackColor = Color.Navy
  74.                 .ForeColor = Color.White
  75.                 .Font = New Font("Tahoma", 9, FontStyle.Bold)
  76.             End With
  77.         End With
  78.     End Sub

  79.     ' / --------------------------------------------------------------------------------
  80.     ' / SAMPLE DATA INTO DATAGRIDVIEW
  81.     Private Sub FillSampleData()
  82.         Dim dt As New DataTable
  83.         dt.Columns.Add("Primary Key")
  84.         dt.Columns.Add("ID")
  85.         dt.Columns.Add("Number Field")
  86.         dt.Columns.Add("Double Field")
  87.         dt.Columns.Add("Date Field")
  88.         Dim RandomClass As New Random()
  89.         For i As Long = 0 To 199
  90.             Dim dr As DataRow = dt.NewRow()
  91.             dr(0) = i + 1
  92.             dr(1) = "ID" & i + 1
  93.             dr(2) = RandomClass.Next(1, 99999)
  94.             dr(3) = FormatNumber(RandomClass.Next(100, 1000) + RandomClass.NextDouble(), 2)
  95.             '// Random Date
  96.             Dim d As Date = Date.Today
  97.             d = d.AddDays(RandomClass.Next(-30, 0))
  98.             dr(4) = FormatDateTime(d, DateFormat.ShortDate).ToString
  99.             dt.Rows.Add(dr)
  100.         Next
  101.         dgvData.DataSource = dt
  102.     End Sub

  103.     Private Sub frmContextMenu_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  104.         Me.Dispose()
  105.         Application.Exit()
  106.     End Sub

  107. End Class
คัดลอกไปที่คลิปบอร์ด

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

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

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

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

1

กระทู้

15

โพสต์

83

เครดิต

Member

Rank: 2

เครดิต
83
โพสต์ 2018-10-11 19:01:29 | ดูโพสต์ทั้งหมด

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

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

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

GMT+7, 2024-5-5 22:30 , Processed in 0.237049 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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