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

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

[VB.NET] การใช้งาน ListView ของ MaterialSkin2

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

311

กระทู้

502

โพสต์

6060

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6060

การใช้งาน ListView ของ MaterialSkin2 ซึ่งจะใช้งานได้สำหรับ .NET Framework 4.5 ขึ้นไป ...



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

มาดูโค้ดกันเถอะ ...
  1. '// Download packages.
  2. '// https://www.nuget.org/packages/MaterialSkin.2/

  3. Imports System.Data.OleDb
  4. Imports MaterialSkin
  5. Imports MaterialSkin.Controls

  6. Public Class frmMaterialSkinListview

  7.     Private Sub frmMaterialSkinListview_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  8.         If Not ConnectDataBase() Then
  9.             MessageBox.Show("Can't connect Northwind.accdb MS Access DataBase.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  10.             Application.Exit()
  11.         End If
  12.         With cmbColorTheme
  13.             .Items.Add("Orange")
  14.             .Items.Add("Green")
  15.             .Items.Add("Light Blue")
  16.             .Items.Add("Cyan")
  17.             .Items.Add("Gray")
  18.         End With
  19.         cmbColorTheme.SelectedIndex = 1
  20.         lblRecordCount.Text = ""
  21.         '// MaterialTextBox Properties. (Which you can customize at Design Time.)
  22.         With txtSearch
  23.             .Hint = "Type something to search and press Enter."
  24.             .UseTallSize = True
  25.             .UseAccent = True
  26.             .Anchor = AnchorStyles.Left + AnchorStyles.Right + AnchorStyles.Top + AnchorStyles.Bottom
  27.         End With
  28.         '// Show all data.
  29.         Call RetrieveData(False)
  30.     End Sub

  31.     ' / --------------------------------------------------------------------------------
  32.     ' / Collect all searches, come in the same place.
  33.     ' / blnSearch = True, Show that the search results.
  34.     ' / blnSearch is set default to False, for show all records.
  35.     Sub RetrieveData(Optional ByVal blnSearch As Boolean = False)
  36.         strSQL =
  37.             " SELECT Orders.OrderID, Customers.CompanyName, Customers.ContactName, Customers.Address, Customers.City, " &
  38.             " Customers.Region, Customers.PostalCode, Customers.Country, Orders.OrderDate, Orders.Freight, Orders.ShipName " &
  39.             " FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID "
  40.         If blnSearch Then
  41.             strSQL = strSQL &
  42.                 " WHERE " &
  43.                 " [OrderID] " & " Like '%" & txtSearch.Text & "%'" & " OR " &
  44.                 " [CompanyName] " & " Like '%" & txtSearch.Text & "%'" & " OR " &
  45.                 " [ContactName] " & " Like '%" & txtSearch.Text & "%'" & " OR " &
  46.                 " [Address] " & " Like '%" & txtSearch.Text & "%'" & " OR " &
  47.                 " [City] " & " Like '%" & txtSearch.Text & "%'" & " OR " &
  48.                 " [Region] " & " Like '%" & txtSearch.Text & "%'" & " OR " &
  49.                 " [PostalCode] " & " Like '%" & txtSearch.Text & "%'" & " OR " &
  50.                 " [Country] " & " Like '%" & txtSearch.Text & "%'" & " OR " &
  51.                 " [ShipName] " & " Like '%" & txtSearch.Text & "%'" &
  52.                 " ORDER BY OrderID "
  53.         Else
  54.             strSQL = strSQL & " ORDER BY OrderID "
  55.         End If
  56.         '//
  57.         Call SetupListView()
  58.         Try
  59.             If Conn.State = ConnectionState.Closed Then Conn.Open()
  60.             Cmd = New OleDb.OleDbCommand(strSQL, Conn)
  61.             DR = Cmd.ExecuteReader()
  62.             Dim iRow As Integer = 1 '// Counter
  63.             Do While DR.Read()
  64.                 Dim NewItem As New ListViewItem(iRow)
  65.                 With NewItem.SubItems
  66.                     .Add(DR.Item("OrderID").ToString)
  67.                     .Add(DR.Item("CompanyName").ToString)
  68.                     .Add(DR.Item("ContactName").ToString)
  69.                     .Add(Convert.ToDateTime(DR.Item("OrderDate").ToString))
  70.                     .Add(DR.Item("Freight").ToString)
  71.                     .Add(DR.Item("Country").ToString)
  72.                 End With
  73.                 lvwData.Items.Add(NewItem)
  74.                 iRow += 1
  75.             Loop
  76.             lblRecordCount.Text = "Total: " & Format(lvwData.Items.Count, "#,##0") & " Records."
  77.             DR.Close()
  78.             Cmd.Dispose()
  79.         Catch ex As Exception
  80.             MessageBox.Show(ex.Message)
  81.         End Try
  82.         txtSearch.Text = String.Empty
  83.         txtSearch.Focus()
  84.     End Sub

  85.     Private Sub txtSearch_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtSearch.KeyPress
  86.         If Trim(Me.txtSearch.Text) = "" Or Len(Trim(txtSearch.Text)) = 0 Then Exit Sub
  87.         If Asc(e.KeyChar) = 13 Then
  88.             '// No beep.
  89.             e.Handled = True
  90.             '/ Retrieve Data(True) means that it is searching for information which user want.
  91.             Call RetrieveData(True)
  92.         End If
  93.     End Sub

  94.     ' / ----------------------------------------------------------------------------------------
  95.     ' / Initailize MaterialListView Control
  96.     Sub SetupListView()
  97.         '// Properties of MaterialListView.
  98.         With lvwData
  99.             .Clear()
  100.             .View = View.Details
  101.             .GridLines = True
  102.             .FullRowSelect = True
  103.             .HideSelection = False
  104.             .MultiSelect = False
  105.             .GridLines = True
  106.             .HoverSelection = True
  107.             .OwnerDraw = True
  108.             .Font = New Font("Roboto", 22, FontStyle.Regular)   '// Row height adjustment Use the font size instead.
  109.             ' 1st Column Index = 0
  110.             .Columns.Add("#Item", 75)
  111.             .Columns.Add("OrderID", 100)
  112.             .Columns.Add("CompanyName", lvwData.Width \ 6 - 5)
  113.             .Columns.Add("ContactName", lvwData.Width \ 6 - 5)
  114.             .Columns.Add("OrderDate", lvwData.Width \ 6 - 5)
  115.             .Columns.Add("Freight", lvwData.Width \ 6 - 10)
  116.             .Columns.Add("Country", lvwData.Width \ 6 - 10)
  117.         End With
  118.     End Sub

  119.     '// Select Color Scheme
  120.     Private Sub cmbColorTheme_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbColorTheme.SelectedIndexChanged
  121.         Select Case cmbColorTheme.SelectedIndex
  122.             Case 0
  123.                 SkinManager.ColorScheme = New ColorScheme(Primary.Amber500, Primary.BlueGrey900, Primary.BlueGrey500, Accent.Red700, TextShade.WHITE)
  124.             Case 1
  125.                 SkinManager.ColorScheme = New ColorScheme(Primary.Green600, Primary.Green700, Primary.Green200, Accent.Orange700, TextShade.WHITE)
  126.             Case 2
  127.                 SkinManager.ColorScheme = New ColorScheme(Primary.LightBlue600, Primary.LightBlue700, Primary.Green200, Accent.Purple700, TextShade.WHITE)
  128.             Case 3
  129.                 SkinManager.ColorScheme = New ColorScheme(Primary.Cyan500, Primary.Cyan700, Primary.Cyan100, Accent.Blue700, TextShade.WHITE)
  130.             Case 4
  131.                 SkinManager.ColorScheme = New ColorScheme(Primary.Grey600, Primary.Grey700, Primary.Grey100, Accent.Teal700, TextShade.WHITE)
  132.         End Select
  133.         txtSearch.Focus()
  134.     End Sub

  135.     '// Doublick mouse event for show OrderID.
  136.     Private Sub lvwData_DoubleClick(sender As Object, e As EventArgs) Handles lvwData.DoubleClick
  137.         MsgBox("Order ID: " & lvwData.SelectedItems(0).SubItems(1).Text)
  138.     End Sub

  139.     Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
  140.         Call RetrieveData(False)    '// Show all data.
  141.     End Sub

  142.     Private Sub frmMaterialSkinListview_Resize(sender As Object, e As EventArgs) Handles Me.Resize
  143.         If lvwData.Items.Count = 0 Then Return
  144.         With lvwData
  145.             .Columns(0).Width = 75
  146.             .Columns(1).Width = 100
  147.             .Columns(2).Width = lvwData.Width \ 6 - 5
  148.             .Columns(3).Width = lvwData.Width \ 6 - 5
  149.             .Columns(4).Width = lvwData.Width \ 6 - 10
  150.             .Columns(5).Width = lvwData.Width \ 6 - 10
  151.             .Columns(6).Width = lvwData.Width \ 6 - 10
  152.         End With
  153.     End Sub

  154.     Private Sub frmMaterialSkinListview_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
  155.         Me.Dispose()
  156.         GC.SuppressFinalize(Me)
  157.         Application.Exit()
  158.     End Sub
  159. End Class
คัดลอกไปที่คลิปบอร์ด

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

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

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

GMT+7, 2024-4-30 03:20 , Processed in 0.155558 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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