<rp id="wnpn7"><ruby id="wnpn7"></ruby></rp>
<progress id="wnpn7"><track id="wnpn7"><rt id="wnpn7"></rt></track></progress>
<ruby id="wnpn7"></ruby>
<ruby id="wnpn7"><blockquote id="wnpn7"><div id="wnpn7"></div></blockquote></ruby>

    1. <em id="wnpn7"><ruby id="wnpn7"><input id="wnpn7"></input></ruby></em>
      1. <button id="wnpn7"><acronym id="wnpn7"></acronym></button><button id="wnpn7"><acronym id="wnpn7"></acronym></button>

        <rp id="wnpn7"><acronym id="wnpn7"></acronym></rp>

        <li id="wnpn7"><object id="wnpn7"><u id="wnpn7"></u></object></li>
        VB.net 2010 視頻教程 VB.net 2010 視頻教程 python基礎視頻教程
        SQL Server 2008 視頻教程 c#入門經典教程 Visual Basic從門到精通視頻教程
        當前位置:
        首頁 > 編程開發 > VBnet >
        • vb.net教程之VB.NET 中 使用 ListView 控件的簡單例子

        ListView 控件 在 程序開發過程中的使用是非常廣泛的。因為其不支持數據庫的綁定,所以在數據庫程序開發領域無法與datagridview抗衡 ,但是ListView的確是一個非常好用的控件。下面就把 一個簡單的 ListView的例子發出來。
         
         
         
        Public Class Form6
            ''' <summary>
            ''' 英雄類
            ''' </summary>
            ''' <remarks></remarks>
            Public Class hero
                Private s_name As String
                Private s_age As Long
                Private s_like As String
                Public Property myName() As String
                    Get
                        Return s_name
                    End Get
                    Set(ByVal value As String)
                        s_name = value
                    End Set
                End Property
                Public Property myAge() As Long
                    Get
                        Return s_age
                    End Get
                    Set(ByVal value As Long)
                        s_age = value
                    End Set
                End Property
                Public Property myLike() As String
                    Get
                        Return s_like
                    End Get
                    Set(ByVal value As String)
                        s_like = value
                    End Set
                End Property
            End Class
            ''' <summary>
            ''' 公共屬性
            ''' </summary>
            ''' <remarks></remarks>
            Dim arrLike() As String = {"殺人", "放火", "吸毒", "搶劫", "盜竊", "上吊", "自虐", "賭博", "偷窺", "飆車"}
            Dim heroList As New SortedList
            Dim maxNumber As Long
            Const firstName As String = "塞北的雪"
            Dim s_flag As Boolean
         
            ''' <summary>
            ''' 生成隨即愛好
            ''' </summary>
            ''' <param name="i"></param>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Private Function createLike(ByVal i As Long) As String
                Dim j As Long
                Dim k As Long
                Dim str As New System.Text.StringBuilder
                For j = arrLike.GetLowerBound(0) To arrLike.GetUpperBound(0)
                    Dim rd As New System.Random(Environment.TickCount * (i + 1) * j / 100)
                    k = System.Math.Round(rd.NextDouble * 100, 0)
                    If k Mod 2 = 0 Then
                        str.Append(" ")
                        str.Append(arrLike(j))
                    End If
                Next
                Return str.ToString
            End Function
            ''' <summary>
            '''  裝載數據
            ''' </summary>
            ''' <param name="sender"></param>
            ''' <param name="e"></param>
            ''' <remarks></remarks>
            Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
                s_flag = True
                Me.lv1.CheckBoxes = True
         
                Dim i As Integer
                Const maxCount As Long = 10
                Dim iItem As ListViewItem
                Dim aHero As hero
                For i = maxNumber To maxNumber + maxCount - 1
                    Dim rd As New System.Random(Environment.TickCount * i)
                    aHero = New hero
                    iItem = New ListViewItem
                    aHero.myName = firstName & i.ToString
                    aHero.myAge = System.Math.Round(rd.NextDouble * 100, 0)
                    aHero.myLike = createLike(i)
                    iItem.Text = aHero.myName
                    iItem.Tag = aHero
                    iItem.SubItems.Add(aHero.myAge)
                    iItem.SubItems.Add(aHero.myLike)
                    Me.lv1.Items.Add(iItem)
                    heroList.Add(aHero.myName, aHero)
                Next
                maxNumber = i
                If Me.lv1.Items.Count > 0 Then
                    Me.lv1.Items.Item(0).Checked = True
                End If
                s_flag = False
            End Sub
            ''' <summary>
            ''' 選定所有項
            ''' </summary>
            ''' <param name="sender"></param>
            ''' <param name="e"></param>
            ''' <remarks></remarks>
            Private Sub btnCheckAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckAll.Click
                s_flag = True
                If Me.lv1.Items.Count > 0 Then
                    Dim i As Integer
                    For i = 0 To Me.lv1.Items.Count - 1
                        Me.lv1.Items.Item(i).Checked = True
                    Next
                End If
                s_flag = False
            End Sub
            ''' <summary>
            ''' 查找所有選定項
            ''' </summary>
            ''' <param name="sender"></param>
            ''' <param name="e"></param>
            ''' <remarks></remarks>
            Private Sub btnFindCheckted_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindCheckted.Click
                Dim sb As New System.Text.StringBuilder
                If Me.lv1.Items.Count > 0 Then
                    Dim i As Integer
                    For i = 0 To Me.lv1.CheckedItems.Count - 1
                        sb.AppendLine()
                        sb.Append(Me.lv1.CheckedItems.Item(i).Text)
                    Next
                End If
                MsgBox(sb.ToString)
            End Sub
            ''' <summary>
            ''' 刪除所有
            ''' </summary>
            ''' <param name="sender"></param>
            ''' <param name="e"></param>
            ''' <remarks></remarks>
            Private Sub btnDeleteAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteAll.Click
                Me.lv1.Items.Clear()
                Me.heroList.Clear()
                maxNumber = 0
            End Sub
            ''' <summary>
            '''  添加一項
            ''' </summary>
            ''' <param name="sender"></param>
            ''' <param name="e"></param>
            ''' <remarks></remarks>
            Private Sub btnAddOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddOne.Click
                s_flag = True
                Dim iItem As New ListViewItem
                Dim aHero As New hero
                Dim rd As New System.Random(Environment.TickCount * maxNumber)
                aHero.myName = firstName & maxNumber.ToString
                aHero.myAge = System.Math.Round(rd.NextDouble * 100, 0)
                aHero.myLike = createLike(maxNumber)
                iItem.Text = aHero.myName
                iItem.Tag = aHero
                iItem.SubItems.Add(aHero.myAge)
                iItem.SubItems.Add(aHero.myLike)
                Me.lv1.Items.Add(iItem)
                heroList.Add(aHero.myName, aHero)
                s_flag = False
                maxNumber += 1
            End Sub
         
            Private Sub Form6_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                Me.lv1.View = View.Details
                Me.lv1.HeaderStyle = ColumnHeaderStyle.Clickable
                Me.lv1.MultiSelect = True
                Me.lv1.FullRowSelect = True
                Me.lv1.GridLines = True
         
                Me.lv1.Columns.Add("姓名", 120, HorizontalAlignment.Center)
                Me.lv1.Columns.Add("年齡", 50, HorizontalAlignment.Right)
                Me.lv1.Columns.Add("愛好", 300, HorizontalAlignment.Left)
         
                Dim iItem As New ListViewItem
                Dim aHero As New hero
                Dim rd As New System.Random(Environment.TickCount)
                aHero.myName = "塞北的雪"
                aHero.myAge = System.Math.Round(rd.NextDouble * 100, 0)
                aHero.myLike = createLike(1)
                iItem.Text = aHero.myName
                iItem.Tag = aHero
                iItem.SubItems.Add(aHero.myAge)
                iItem.SubItems.Add(aHero.myLike)
                Me.lv1.Items.Add(iItem)
                heroList.Add(aHero.myName, aHero)
                Me.lv1.Items.Item(0).Selected = True
                maxNumber = 0
                s_flag = False
            End Sub
            ''' <summary>
            ''' 刪除所有選定項
            ''' </summary>
            ''' <param name="sender"></param>
            ''' <param name="e"></param>
            ''' <remarks></remarks>
            Private Sub btnDeleteSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteSelected.Click
                If Me.lv1.Items.Count > 0 Then
                    If Me.lv1.SelectedItems.Count > 0 Then
                        Dim i As Integer
                        Dim delList As New SortedList
                        For i = 0 To Me.lv1.SelectedItems.Count - 1
                            heroList.Remove(Me.lv1.SelectedItems.Item(i).Text)
                            ' Me.lv1.Items.Remove(Me.lv1.SelectedItems.Item(i))
                            delList.Add(Me.lv1.SelectedItems.Item(i).Text, Me.lv1.SelectedItems.Item(i))
                        Next
                        For i = 0 To delList.Count - 1
                            Me.lv1.Items.Remove(delList.GetByIndex(i))
                        Next
                    End If
                End If
            End Sub
         
            Private Sub lv1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lv1.ItemCheck
                If Me.lv1.Items.Count > 0 Then
                    If s_flag = False Then
                        If e.NewValue = CheckState.Checked And e.CurrentValue <> CheckState.Checked Then
                            MsgBox("你選擇了:" & Me.lv1.Items.Item(e.Index).Text)
                        ElseIf e.NewValue = CheckState.Unchecked And e.CurrentValue <> CheckState.Unchecked Then
                            MsgBox("你取消了:" & Me.lv1.Items.Item(e.Index).Text)
                        End If
                    End If
                End If
            End Sub
         
         
         
            Private Sub lv1_ItemChecked1(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles lv1.ItemChecked
                If Me.lv1.Items.Count > 0 Then
                    If s_flag = False Then
                        If e.Item.Checked Then
                            MsgBox("你選擇了:" & e.Item.Text)
                        Else
                            MsgBox("你取消了:" & e.Item.Text)
                        End If
                    End If
                End If
            End Sub
         
            Private Sub btnUnCheckAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnCheckAll.Click
                s_flag = True
                If Me.lv1.Items.Count > 0 Then
                    Dim i As Integer
                    For i = 0 To Me.lv1.Items.Count - 1
                        Me.lv1.Items.Item(i).Checked = False
                    Next
                End If
                s_flag = False
            End Sub
        End Class
         
         
        程序運行的截圖如下:
         
         
        ————————————————
        版權聲明:本文為CSDN博主「precipitant」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
        原文鏈接:https://blog.csdn.net/precipitant/article/details/1441018
        相關教程
                
        免费看成年人视频大全_免费看成年人视频在线观看