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

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

[VB.NET] การสร้าง ListView และ ImageList Control ในแบบ Run Time หรือแบบไดนามิค

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

311

กระทู้

502

โพสต์

6060

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6060

การสร้าง ListView และ ImageList Control ในแบบ Run Time หรือแบบไดนามิค เป็นการใช้โค้ดล้วนๆในการสร้างคอนโทรลเหล่านี้ขึ้นมาในแบบอัตโนมัติ ...


มาดูโค้ดกันเถอะ ...
  1.     '// Create a new ListView control dynamically.
  2.     Private listView1 As New ListView()

  3.     Private Sub frmListViewImage_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  4.         Call CreateMyListView()
  5.     End Sub

  6.     ' / --------------------------------------------------------------------------------
  7.     '// Create ListView Control @Run Time.
  8.     ' / --------------------------------------------------------------------------------
  9.     Private Sub CreateMyListView()
  10.         listView1.Bounds = New Rectangle(New Point(10, 10), New Size(370, 500))

  11.         '// Set the view to show details.
  12.         With listView1
  13.             '// Set ListView view mode to show Large Icons.
  14.             .View = View.LargeIcon
  15.             .Cursor = Cursors.Hand
  16.             '// Adjust to the size of the form.
  17.             .Anchor = AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right + AnchorStyles.Top
  18.         End With

  19.         ' / --------------------------------------------------------------------------------
  20.         ' / SAMPLE BASIC ... Static ListView control
  21.         ' / Create items and sets of subitems for each item.
  22.         'Dim item1 As New ListViewItem("Table 1", 0)
  23.         'Dim item2 As New ListViewItem("Table 2", 1)
  24.         'Dim item3 As New ListViewItem("Table 3", 0)
  25.         'Dim item4 As New ListViewItem("Table 4", 0)
  26.         'Dim item5 As New ListViewItem("Table 5", 1)
  27.         'Dim item6 As New ListViewItem("Table 6", 1)
  28.         'Dim item7 As New ListViewItem("Table 7", 0)
  29.         'Add the items to the ListView.
  30.         'listView1.Items.AddRange(New ListViewItem() {item1, item2, item3, item4, item5, item6, item7})
  31.         Try
  32.             ' / --------------------------------------------------------------------------------
  33.             ' / @Run Time or Dynamically.
  34.             Dim Items As New List(Of ListViewItem)
  35.             Dim RancomClass As New Random()
  36.             For i As Integer = 0 To 16
  37.                 '// Text and Images index
  38.                 Dim item As New ListViewItem("Table " & i + 1, RancomClass.Next(0, 9) Mod 2)
  39.                 Items.Add(item)
  40.             Next
  41.             listView1.Items.AddRange(Items.ToArray)

  42.             '// Create ImageList objects @Run Time or Dynamically.
  43.             Dim imgList As New ImageList()
  44.             imgList.ImageSize = New Size(128, 128)
  45.             Dim strPathImages As String = MyPath(Application.StartupPath) & "images"
  46.             '// Initialize the ImageList objects with bitmaps.
  47.             With imgList.Images
  48.                 .Add(Bitmap.FromFile(strPathImages & "table-icon-green.png"))
  49.                 .Add(Bitmap.FromFile(strPathImages & "table-icon-red.png"))
  50.             End With
  51.             '// Assign the ImageList objects to the ListView.
  52.             listView1.LargeImageList = imgList

  53.             '// Add the ListView to the control collection.
  54.             Me.Controls.Add(listView1)
  55.             '// start event handling at any time during program execution.
  56.             AddHandler listView1.Click, AddressOf listView1_Click
  57.         Catch ex As Exception
  58.             MessageBox.Show(ex.Message)
  59.         End Try
  60.     End Sub 'CreateMyListView

  61.     '// Event Handler.
  62.     Private Sub listView1_Click(sender As System.Object, e As EventArgs)
  63.         MsgBox("ListView Index = " & listView1.SelectedIndices(0).ToString() & vbCrLf & "ListView Caption = " & listView1.SelectedItems.Item(0).Text)
  64.     End Sub

  65.     ' / --------------------------------------------------------------------------------
  66.     ' / Get my project path
  67.     ' / AppPath = C:\My Project\bin\debug
  68.     ' / Replace "\bin\debug" with ""
  69.     ' / Return : C:\My Project\
  70.     Function MyPath(AppPath As String) As String
  71.         '/ Return Value
  72.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  73.         '// Put the BackSlash "" or ASCII Code = 92 at the end.
  74.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  75.     End Function

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

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

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

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

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

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

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

GMT+7, 2024-4-30 01:56 , Processed in 0.211895 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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