转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
如何编辑ListView的subitem(VB)         ★★★★

如何编辑ListView的subitem(VB)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1320 更新时间:2009/4/23 15:43:13

摘自 jjkk168(老加班的人) ( 五级(中级)) 信誉:112

文件一,Form1.frm

加入一个Listview,两个Imagelist,一个文本框

代码如下:
Option Explicit
''''
'''' Copyright ?1997-1999 Brad Martinez, http://www.mvps.org
''''
'''' Demonstrates how to in place do SubItem editing in the VB ListView.

Private m_hwndLV As Long   '''' ListView1.hWnd
Private m_hwndTB As Long   '''' TextBox1.hWnd
Private m_iItem As Long         '''' ListItem.Index whose SubItem is being edited
Private m_iSubItem As Long   '''' zero based index of ListView1.ListItems(m_iItem).SubItem being edited
''''

Private Sub Form_Load()
  Dim i As Long
  Dim item As ListItem
 
''''  Text1.Appearance = ccFlat   '''' ComctlLib enum value
  Text1.Visible = False
  m_hwndTB = Text1.hWnd
 
  '''' Initialize the ImageLists
  With ImageList1
    .ImageHeight = 32
    .ImageWidth = 32
    .ListImages.Add Picture:=Icon
  End With
 
  With ImageList2
    .ImageHeight = 16
    .ImageWidth = 16
    .ListImages.Add Picture:=Icon
  End With
 
  '''' Initialize the ListView
  With ListView1
''''    .LabelEdit = lvwManual
    .HideSelection = False
    .Icons = ImageList1
    .SmallIcons = ImageList2
    m_hwndLV = .hWnd
   
    For i = 1 To 4
      .ColumnHeaders.Add Text:="column" & i
    Next
   
    For i = 0 To &H3F
      Set item = .ListItems.Add(, , "item" & i, 1, 1)
      item.SubItems(1) = i * 10
      item.SubItems(2) = i * 100
      item.SubItems(3) = i * 1000
    Next
  End With
 
 
End Sub

Private Sub Form_Resize()
''''  ListView1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub ListView1_DblClick()
  Dim lvhti As LVHITTESTINFO
  Dim rc As RECT
  Dim li As ListItem
   
  '''' If a left button double-click... (change to suit)
  If (GetKeyState(vbKeyLButton) And &H8000) Then
 
    '''' If a ListView SubItem is double clicked...
    Call GetCursorPos(lvhti.pt)
    Call ScreenToClient(m_hwndLV, lvhti.pt)
    If (ListView_SubItemHitTest(m_hwndLV, lvhti) <> LVI_NOITEM) Then
      If lvhti.iSubItem Then
       
        '''' Get the SubItem''''s label (and icon) rect.
        If ListView_GetSubItemRect(m_hwndLV, lvhti.iItem, lvhti.iSubItem, LVIR_LABEL, rc) Then
         
          '''' Either set the ListView as the TextBox parent window in order to
          '''' have the TextBox Move method use ListView client coords, or just
          '''' map the ListView client coords to the TextBox''''s paent Form
  ''''        Call SetParent(m_hwndTB, m_hwndLV)
          Call MapWindowPoints(m_hwndLV, hWnd, rc, 2)
          Text1.Move (rc.Left + 4) * Screen.TwipsPerPixelX, _
                              rc.Top * Screen.TwipsPerPixelY, _
                              (rc.Right - rc.Left) * Screen.TwipsPerPixelX, _
                              (rc.Bottom - rc.Top) * Screen.TwipsPerPixelY
         
          '''' Save the one-based index of the ListItem and the zero-based index
          '''' of the SubItem(if the ListView is sorted via the  API, then ListItem.Index
          '''' will be different than lvhti.iItem +1...)
          m_iItem = lvhti.iItem + 1
          m_iSubItem = lvhti.iSubItem
         
          '''' Put the SubItem''''s text in the TextBox, save the SubItem''''s text,
          '''' and clear the SubItem''''s text.
          Text1 = ListView1.ListItems(m_iItem).SubItems(m_iSubItem)
          Text1.Tag = Text1
          ListView1.ListItems(m_iItem).SubItems(m_iSubItem) = ""
         
          '''' Make the TextBox the topmost Form control, make the it visible, select
          '''' its text, give it the focus, and subclass it.
          Text1.ZOrder 0
          Text1.Visible = True
          Text1.SelStart = 0
          Text1.SelLength = Len(Text1)
          Text1.SetFocus
          Call SubClass(m_hwndTB, AddressOf WndProc)
         
        End If   '''' ListView_GetSubItemRect
      End If   '''' lvhti.iSubItem
    End If   '''' ListView_SubItemHitTest
  End If   '''' GetKeyState(vbKeyLButton)
 
End Sub

'''' Selects the ListItem whose SubItem is being edited...

Private Sub Text1_GotFocus()
  ListView1.ListItems(m_iItem).Selected = True
End Sub

'''' If the TextBox is shown, size its width so that it''''s always a little
'''' longer than the length of its Text.

Private Sub Text1_Change()
  If m_iItem Then Text1.Width = TextWidth(Text1) + 180
End Sub

'''' Update the SubItem text on the Enter key, cancel on the Escape Key.

Private Sub Text1_KeyPress(KeyAscii As Integer)
 
  If (KeyAscii = vbKeyReturn) Then
    Call HideTextBox(True)
    KeyAscii = 0
  ElseIf (KeyAscii = vbKeyEscape) Then
    Call HideTextBox(False)
    KeyAscii = 0
  End If

End Sub

Friend Sub HideTextBox(fApplyChanges As Boolean)
 
  If fApplyChanges Then
    ListView1.ListItems(m_iItem).SubItems(m_iSubItem) = Text1
  Else
    ListView1.ListItems(m_iItem).SubItems(m_iSubItem) = Text1.Tag
  End If
 
  Call UnSubClass(m_hwndTB)
  Text1.Visible = False
  Text1 = ""
''''  Call SetParent(m_hwndTB, hWnd)
''''  ListView1.SetFocus
  m_iItem = 0
 
End Sub

文件二:Module1.bas

Option Explicit
''''
'''' Copyright ?1997-1999 Brad Martinez, http://www.mvps.org
''''
Public Type POINTAPI   '''' pt
  X As Long
  Y As Long
End Type

Public Type RECT   '''' rct
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As KeyCodeConstants) As Integer

Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Declare Function MapWindowPoints Lib "user32" (ByVal hwndFrom As Long, ByVal hwndTo As Long, lppt As Any, ByVal cPoints As Long) As Long

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                            (ByVal hWnd As Long, _
                            ByVal wMsg As Long, _
                            ByVal

[1] [2]  下一页


[VB.NET程序]VB编辑ListView的SubItem  
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台