zoukankan      html  css  js  c++  java
  • vb.net listview 可编辑 ,使用textbox附加实现

    ' 这部分在 form中编写

    注意全局声明 和 启用

    class form1xxxx

    ''' <summary>
    ''' 初始化,绑定 可编辑listview
    ''' -- 1、全局声明 Private ELvClass As EditableListView
    ''' -- 2、启用 initalinzeEditLv(ELv, LVfujian, New Integer() {0, 1, 2, 3})
    ''' </summary>
    ''' <param name="ELvClass">全局editbalelistview 类</param>
    ''' <param name="lv">要编辑的Listview控件</param>
    ''' <param name="EditColumsID">要编辑的列号的数组</param>
    Private Sub InitalinzeEditLv(ELvClass As EditableListView, lv As ListView, EditColumsID() As Integer)
    ELvClass = New EditableListView(LVfujian)
    ELvClass.Property_CanEditColumnsArr = EditColumsID
    ELvClass.Delegate_Submitting = New EditableListViewSubmitting(AddressOf listViewSaveEditHandler)
    End Sub


    Private Sub listViewSaveEditHandler(ByVal sender As Object, ByVal e As EditableListViewSubmittingEventArgs)
    If e.Cell Is Nothing Then Return

    Dim value As String = e.eValue
    Dim item As ListViewItem = e.Cell.LvItem
    Dim itemindex As Integer = e.Cell.LvColumnHeader.Index
    Try
    item.SubItems(itemindex).Text = value
    Catch ex As Exception
    End Try
    End Sub
    End Class

    ‘可编辑 listview 类
    Public Class ListViewCell '================================================================
    Public Property LvItem As ListViewItem
    Public Property LvItemIndex As Integer
    Public Property LvColumnHeader As ColumnHeader
    Public Property Bounds As Rectangle
    End Class

    Public Class ListViewCellLocator
    <DllImport("user32")>
    Public Shared Function GetScrollPos(ByVal hwnd As Integer, ByVal nBar As Integer) As Integer : End Function

    '<DebuggerStepThrough>
    Public Shared Function GetCell(ByVal LV As ListView, ByVal x As Integer, ByVal y As Integer) As ListViewCell
    Dim Cell As ListViewCell = New ListViewCell()
    Cell.LvItem = LV.GetItemAt(x, y)
    If Cell.LvItem Is Nothing Then Return Nothing
    'Cell.LvItemIndex = Cell.LvItem.Index
    Dim CurrentX As Integer = Cell.LvItem.GetBounds(ItemBoundsPortion.Entire).Left
    Dim CurrentH As Integer = Cell.LvItem.GetBounds(ItemBoundsPortion.Entire).Height
    Dim scrollLeft As Integer = GetScrollPos(LV.Handle.ToInt32(), 0)
    For i As Integer = 0 To LV.Columns.Count - 1
    If scrollLeft + x >= CurrentX AndAlso scrollLeft + x < CurrentX + LV.Columns(i).Width Then
    Cell.LvColumnHeader = LV.Columns(i)
    Dim itemBounds As Rectangle = Cell.LvItem.GetBounds(ItemBoundsPortion.Entire)
    Cell.Bounds = New Rectangle(CurrentX, itemBounds.Y, LV.Columns(i).Width, CurrentH)
    Exit For
    End If
    CurrentX += LV.Columns(i).Width
    Next

    If Cell.LvColumnHeader Is Nothing Then
    Return Nothing
    End If

    Return Cell
    End Function
    End Class

    Public Class EditableListViewSubmittingEventArgs
    Inherits EventArgs

    Public Property Cell As ListViewCell
    Public Property eValue As String
    End Class

    Public Delegate Sub EditableListViewSubmitting(ByVal sender As Object, ByVal e As EditableListViewSubmittingEventArgs)

    Class EditableListView
    Public Delegate_Submitting As EditableListViewSubmitting '委托变量
    Private MousePosition As Point = New Point()

    Private Property Property_LV As ListView
    Private Property Property_EditBox As TextBox
    Private Property Property_ComboBox As ComboBox
    Public Property Property_CanEditColumnsArr As Integer()

    Public Sub New(ByVal listView0 As ListView)
    Property_EditBox = New TextBox() : Property_EditBox.Visible = False : Property_EditBox.Multiline = True
    Property_EditBox.BorderStyle = BorderStyle.FixedSingle
    AddHandler Property_EditBox.KeyDown, New KeyEventHandler(AddressOf ebKeyDownHandle)
    AddHandler Property_EditBox.KeyUp, New KeyEventHandler(AddressOf ebKeyUpHandle)
    AddHandler Property_EditBox.KeyPress, New KeyPressEventHandler(AddressOf ebKeyPressHandle)
    AddHandler Property_EditBox.LostFocus, New EventHandler(AddressOf EditBox_LostForcus)


    Property_LV = listView0
    AddHandler Property_LV.MouseMove, New MouseEventHandler(
    Function(ByVal sender As Object, ByVal e As MouseEventArgs)
    MousePosition.X = e.X : MousePosition.Y = e.Y : Return Nothing
    End Function)
    Property_EditBox.Parent = Property_LV
    AddHandler Property_LV.DoubleClick, New EventHandler(AddressOf EditItem)
    End Sub

    Private Sub ebKeyDownHandle(ByVal sender As Object, ByVal e As KeyEventArgs)
    Try
    If e.KeyCode = Keys.Enter Then
    e.Handled = False
    End If
    Catch ex As Exception
    Debug.Print(ex.Source & ":" & ex.Message)
    End Try
    End Sub

    Private Sub ebKeyPressHandle(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    Try
    If e.KeyChar = Chr(Keys.Enter) Then
    e.Handled = False
    End If
    Catch ex As Exception
    Debug.Print(ex.Source & ":" & ex.Message)
    End Try
    End Sub

    Private Sub ebKeyUpHandle(ByVal sender As Object, ByVal e As KeyEventArgs)
    Try
    If e.KeyCode = Keys.Escape Then
    LeaveEdit()
    ElseIf e.KeyCode = Keys.Enter Then
    SaveEdit()
    End If
    Catch ex As Exception
    Debug.Print(ex.Source & ":" & ex.Message)
    End Try

    End Sub

    Private Sub EditBox_LostForcus(ByVal sender As Object, ByVal e As EventArgs)
    SaveEdit()
    End Sub

    '委托三个步骤
    ' 1、声明委托 用Delegate 声明一个委托 类型 参数要和 被委托的方法一样
    ' 例如 Delegate Function a(ByVal x As String) As String
    '2、实例化委托 dim t as New a(AddressOf Function Name)
    '3.通过 t(参数) 或者 t.Invoke(参数调用委托)

    Private Sub SaveEdit()
    If Delegate_Submitting IsNot Nothing Then
    Dim args As EditableListViewSubmittingEventArgs = New EditableListViewSubmittingEventArgs()

    If Property_EditBox.Tag IsNot Nothing Then
    args.Cell = CType(Property_EditBox.Tag, ListViewCell)
    Else
    args.Cell = Nothing
    End If

    args.eValue = Property_EditBox.Text.Replace(vbCrLf, "")
    LeaveEdit()
    Delegate_Submitting(Property_LV, args)
    End If
    End Sub

    Private Sub EditItem(ByVal sender As Object, ByVal e As EventArgs)
    Dim Cell As ListViewCell = ListViewCellLocator.GetCell(Property_LV, MousePosition.X, MousePosition.Y)
    If Cell Is Nothing Then Return
    If Property_CanEditColumnsArr.Contains(Cell.LvColumnHeader.Index) Then
    Property_EditBox.Bounds = Cell.Bounds
    Try
    Property_EditBox.Text = Cell.LvItem.SubItems(Cell.LvColumnHeader.Index).Text
    Catch ex As Exception
    End Try
    Property_EditBox.Visible = True
    Property_EditBox.Focus()
    Property_EditBox.Tag = Cell
    End If
    End Sub

    Public Function IsEditableColumn(ByVal columnIndex As Integer) As Boolean
    If Property_CanEditColumnsArr.Contains(columnIndex) Then
    Return True
    Else
    Return False
    End If
    End Function

    Public Sub LeaveEdit()
    Property_EditBox.Visible = False
    Property_EditBox.Tag = Nothing
    End Sub
    End Class

  • 相关阅读:
    使用 ServiceStack 构建跨平台 Web 服务
    .NET的微型Web框架 Nancy
    orcale复制表结构及其数据
    利用PL/SQL Developer工具导出数据到excel,导入excel数据到表
    PLSQL导入/导出数据方法
    基于Quqrtz.NET 做的任务调度管理工具
    Web监听器导图详解(转)
    GIT分支管理是一门艺术(转)
    我需要完全理解这部分代码才能确保它能够正常工作,如果由我来修复代码中的问题,我是不会这么写的,因此希望你也不要这么来写(转)
    不要学习代码,要学会思考(转)
  • 原文地址:https://www.cnblogs.com/MadeInChinese/p/15084698.html
Copyright © 2011-2022 走看看