我想使listview控件可以單獨摺疊到組中。像Vista中的我的電腦一樣,只顯示組名,然後點擊組來擴展它。 我正在使用VB.NET 3.5可摺疊列表視圖
Q
可摺疊列表視圖
2
A
回答
0
絕對沒有辦法與bog標準VB.NET ListView
控件做到這一點。事實上,即使使用ObjectListView
也不可能,當我遇到ListView限制時,這是我通常的通話端口。
你既可以:
- 推出自己的控制
- 切換到
TreeView
- 放棄
我認爲,數你的情況最好的選擇,因爲這並不困難。第一是過度殺傷,需要很多時間。如果你有一個與你的邏輯和數據庫層緊密耦合的GUI層,那麼轉換成TreeView可能會困難得多。或者,您可以使用其他ListView視圖。 (在後一種情況下,來自同一個FOSS項目的TreeListView
可能是要走的路 - 但它是GPL。)
殺死這個功能可能更容易 - 我認爲這不是關鍵。
0
下面是擴展列表視圖一篇舊文: http://www.codeproject.com/KB/list/GroupListView.aspx?msg=1135144
下面是一個示例,你想要做我認爲有一個GridView,面板和摺疊面板擴展
你也可以切換到AJAX控制工具包Accordian控件。
2
檢查了這一點:
,也許:
Add Group Collapse Behavior on a Listview Control
兩者都是用C#編寫,但當然也可以在VB.NET項目仍然使用它們。
您只能在Vista和更高版本上使用這些功能。
0
我已經設法通過查看你們在這裏發佈的內容來實現這一點,這就是我的代碼的樣子。
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Reflection
Public Class ListViewCollapsible
Inherits ListView
Private Const LVM_FIRST As Integer = &H1000
' ListView messages
Private Const LVM_SETGROUPINFO As Integer = (LVM_FIRST + 147)
' ListView messages Setinfo on Group
Private Const WM_LBUTTONUP As Integer = &H202
' Windows message left button
Private Delegate Sub CallBackSetGroupState(lstvwgrp As ListViewGroup, state As ListViewGroupState)
Private Delegate Sub CallbackSetGroupString(lstvwgrp As ListViewGroup, value As String)
''' <summary>
''' Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
''' To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.
''' </summary>
''' <param name="hWnd">
''' [in] Handle to the window whose window procedure will receive the message.
''' If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
''' Microsoft Windows Vista and later. Message sending is subject to User Interface Privilege Isolation (UIPI). The thread of a process can send messages only to message queues of threads in processes of lesser or equal integrity level.
''' </param>
''' <param name="Msg">[in] Specifies the message to be sent.</param>
''' <param name="wParam">[in] Specifies additional message-specific information.</param>
''' <param name="lParam">[in] Type of LVGROUP, Specifies additional message-specific information.</param>
''' <returns>
''' Microsoft Windows Vista and later. When a message is blocked by UIPI the last error, retrieved with GetLastError, is set to 5 (access denied).
''' Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication.
''' The system only does marshalling for system messages (those in the range 0 to (WM_USER-1)). To send other messages (those >= WM_USER) to another process, you must do custom marshalling.
''' If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message. However, the sending thread will process incoming nonqueued messages while waiting for its message to be processed. To prevent this, use SendMessageTimeout with SMTO_BLOCK set. For more information on nonqueued messages, see Nonqueued Messages.
''' Windows 95/98/Me: SendMessageW is supported by the Microsoft Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
''' </returns>
<DllImport("User32.dll"), Description("Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message. To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.")> _
Private Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As IntPtr) As Integer
End Function
Private Shared Function GetGroupID(lstvwgrp As ListViewGroup) As System.Nullable(Of Integer)
Dim rtnval As System.Nullable(Of Integer) = Nothing
Dim GrpTp As Type = lstvwgrp.[GetType]()
If GrpTp IsNot Nothing Then
Dim pi As PropertyInfo = GrpTp.GetProperty("ID", BindingFlags.NonPublic Or BindingFlags.Instance)
If pi IsNot Nothing Then
Dim tmprtnval As Object = pi.GetValue(lstvwgrp, Nothing)
If tmprtnval IsNot Nothing Then
rtnval = CInt(tmprtnval)
End If
End If
End If
Return rtnval
End Function
Private Shared Sub setGrpState(lstvwgrp As ListViewGroup, state As ListViewGroupState)
If Environment.OSVersion.Version.Major < 6 Then
'Only Vista and forward allows collaps of ListViewGroups
Return
End If
If lstvwgrp Is Nothing OrElse lstvwgrp.ListView Is Nothing Then
Return
End If
If lstvwgrp.ListView.InvokeRequired Then
lstvwgrp.ListView.Invoke(New CallBackSetGroupState(AddressOf setGrpState), lstvwgrp, state)
Else
Dim GrpId As System.Nullable(Of Integer) = GetGroupID(lstvwgrp)
Dim gIndex As Integer = lstvwgrp.ListView.Groups.IndexOf(lstvwgrp)
Dim group As New LVGROUP
group.CbSize = Marshal.SizeOf(group)
group.State = state
group.Mask = ListViewGroupMask.State
Dim ip As IntPtr = IntPtr.Zero
Try
If GrpId IsNot Nothing Then
group.IGroupId = GrpId.Value
ip = Marshal.AllocHGlobal(group.CbSize)
Marshal.StructureToPtr(group, ip, False)
SendMessage(lstvwgrp.ListView.Handle, LVM_SETGROUPINFO, GrpId.Value, ip)
SendMessage(lstvwgrp.ListView.Handle, LVM_SETGROUPINFO, GrpId.Value, ip)
Else
group.IGroupId = gIndex
ip = Marshal.AllocHGlobal(group.CbSize)
Marshal.StructureToPtr(group, ip, False)
SendMessage(lstvwgrp.ListView.Handle, LVM_SETGROUPINFO, gIndex, ip)
SendMessage(lstvwgrp.ListView.Handle, LVM_SETGROUPINFO, gIndex, ip)
End If
lstvwgrp.ListView.Refresh()
Finally
If (ip <> IntPtr.Zero) Then Marshal.FreeHGlobal(ip)
End Try
End If
End Sub
Private Shared Sub setGrpFooter(lstvwgrp As ListViewGroup, footer As String)
If Environment.OSVersion.Version.Major < 6 Then
'Only Vista and forward allows footer on ListViewGroups
Return
End If
If lstvwgrp Is Nothing OrElse lstvwgrp.ListView Is Nothing Then
Return
End If
If lstvwgrp.ListView.InvokeRequired Then
lstvwgrp.ListView.Invoke(New CallbackSetGroupString(AddressOf setGrpFooter), lstvwgrp, footer)
Else
Dim GrpId As System.Nullable(Of Integer) = GetGroupID(lstvwgrp)
Dim gIndex As Integer = lstvwgrp.ListView.Groups.IndexOf(lstvwgrp)
Dim group As New LVGROUP
group.CbSize = Marshal.SizeOf(group)
group.PszFooter = footer
group.Mask = ListViewGroupMask.Footer
Dim ip As IntPtr = IntPtr.Zero
Try
If GrpId IsNot Nothing Then
group.IGroupId = GrpId.Value
ip = Marshal.AllocHGlobal(group.CbSize)
Marshal.StructureToPtr(group, ip, False)
SendMessage(lstvwgrp.ListView.Handle, LVM_SETGROUPINFO, GrpId.Value, ip)
Else
group.IGroupId = gIndex
ip = Marshal.AllocHGlobal(group.CbSize)
Marshal.StructureToPtr(group, ip, False)
SendMessage(lstvwgrp.ListView.Handle, LVM_SETGROUPINFO, gIndex, ip)
End If
Finally
If (ip <> IntPtr.Zero) Then Marshal.FreeHGlobal(ip)
End Try
End If
End Sub
Public Sub SetGroupState(state As ListViewGroupState)
For Each lvg As ListViewGroup In Me.Groups
setGrpState(lvg, state)
Next
End Sub
Public Sub SetGroupState(state As ListViewGroupState, lvg As ListViewGroup)
setGrpState(lvg, state)
End Sub
Public Sub SetGroupFooter(lvg As ListViewGroup, footerText As String)
setGrpFooter(lvg, footerText)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_LBUTTONUP Then
MyBase.DefWndProc(m)
End If
MyBase.WndProc(m)
End Sub
End Class
''' <summary>
''' LVGROUP StructureUsed to set and retrieve groups.
''' </summary>
''' <example>
''' LVGROUP myLVGROUP = new LVGROUP();
''' myLVGROUP.CbSize // is of managed type uint
''' myLVGROUP.Mask // is of managed type uint
''' myLVGROUP.PszHeader // is of managed type string
''' myLVGROUP.CchHeader // is of managed type int
''' myLVGROUP.PszFooter // is of managed type string
''' myLVGROUP.CchFooter // is of managed type int
''' myLVGROUP.IGroupId // is of managed type int
''' myLVGROUP.StateMask // is of managed type uint
''' myLVGROUP.State // is of managed type uint
''' myLVGROUP.UAlign // is of managed type uint
''' myLVGROUP.PszSubtitle // is of managed type IntPtr
''' myLVGROUP.CchSubtitle // is of managed type uint
''' myLVGROUP.PszTask // is of managed type string
''' myLVGROUP.CchTask // is of managed type uint
''' myLVGROUP.PszDescriptionTop // is of managed type string
''' myLVGROUP.CchDescriptionTop // is of managed type uint
''' myLVGROUP.PszDescriptionBottom // is of managed type string
''' myLVGROUP.CchDescriptionBottom // is of managed type uint
''' myLVGROUP.ITitleImage // is of managed type int
''' myLVGROUP.IExtendedImage // is of managed type int
''' myLVGROUP.IFirstItem // is of managed type int
''' myLVGROUP.CItems // is of managed type IntPtr
''' myLVGROUP.PszSubsetTitle // is of managed type IntPtr
''' myLVGROUP.CchSubsetTitle // is of managed type IntPtr
''' </example>
''' <remarks>
''' The LVGROUP structure was created by Paw Jershauge
''' Created: Jan. 2008.
''' The LVGROUP structure code is based on information from Microsoft's MSDN2 website.
''' The structure is generated via an automated converter and is as is.
''' The structure may or may not hold errors inside the code, so use at own risk.
''' Reference url: http://msdn.microsoft.com/en-us/library/bb774769(VS.85).aspx
''' </remarks>
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode), Description("LVGROUP StructureUsed to set and retrieve groups.")> _
Public Structure LVGROUP
''' <summary>
''' Size of this structure, in bytes.
''' </summary>
<Description("Size of this structure, in bytes.")> _
Public CbSize As Integer
''' <summary>
''' Mask that specifies which members of the structure are valid input. One or more of the following values:LVGF_NONENo other items are valid.
''' </summary>
<Description("Mask that specifies which members of the structure are valid input. One or more of the following values:LVGF_NONE No other items are valid.")> _
Public Mask As ListViewGroupMask
''' <summary>
''' Pointer to a null-terminated string that contains the header text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the header text.
''' </summary>
<Description("Pointer to a null-terminated string that contains the header text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the header text.")> _
<MarshalAs(UnmanagedType.LPWStr)> _
Public PszHeader As String
''' <summary>
''' Size in TCHARs of the buffer pointed to by the pszHeader member. If the structure is not receiving information about a group, this member is ignored.
''' </summary>
<Description("Size in TCHARs of the buffer pointed to by the pszHeader member. If the structure is not receiving information about a group, this member is ignored.")> _
Public CchHeader As Integer
''' <summary>
''' Pointer to a null-terminated string that contains the footer text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the footer text.
''' </summary>
<Description("Pointer to a null-terminated string that contains the footer text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the footer text.")> _
<MarshalAs(UnmanagedType.LPWStr)> _
Public PszFooter As String
''' <summary>
''' Size in TCHARs of the buffer pointed to by the pszFooter member. If the structure is not receiving information about a group, this member is ignored.
''' </summary>
<Description("Size in TCHARs of the buffer pointed to by the pszFooter member. If the structure is not receiving information about a group, this member is ignored.")> _
Public CchFooter As Integer
''' <summary>
''' ID of the group.
''' </summary>
<Description("ID of the group.")> _
Public IGroupId As Integer
''' <summary>
''' Mask used with LVM_GETGROUPINFO (Microsoft Windows XP and Windows Vista) and LVM_SETGROUPINFO (Windows Vista only) to specify which flags in the state value are being retrieved or set.
''' </summary>
<Description("Mask used with LVM_GETGROUPINFO (Microsoft Windows XP and Windows Vista) and LVM_SETGROUPINFO (Windows Vista only) to specify which flags in the state value are being retrieved or set.")> _
Public StateMask As Integer
''' <summary>
''' Flag that can have one of the following values:LVGS_NORMALGroups are expanded, the group name is displayed, and all items in the group are displayed.
''' </summary>
<Description("Flag that can have one of the following values:LVGS_NORMAL Groups are expanded, the group name is displayed, and all items in the group are displayed.")> _
Public State As ListViewGroupState
''' <summary>
''' Indicates the alignment of the header or footer text for the group. It can have one or more of the following values. Use one of the header flags. Footer flags are optional. Windows XP: Footer flags are reserved.LVGA_FOOTER_CENTERReserved.
''' </summary>
<Description("Indicates the alignment of the header or footer text for the group. It can have one or more of the following values. Use one of the header flags. Footer flags are optional. Windows XP: Footer flags are reserved.LVGA_FOOTER_CENTERReserved.")> _
Public UAlign As UInteger
''' <summary>
''' Windows Vista. Pointer to a null-terminated string that contains the subtitle text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the subtitle text. This element is drawn under the header text.
''' </summary>
<Description("Windows Vista. Pointer to a null-terminated string that contains the subtitle text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the subtitle text. This element is drawn under the header text.")> _
Public PszSubtitle As IntPtr
''' <summary>
''' Windows Vista. Size, in TCHARs, of the buffer pointed to by the pszSubtitle member. If the structure is not receiving information about a group, this member is ignored.
''' </summary>
<Description("Windows Vista. Size, in TCHARs, of the buffer pointed to by the pszSubtitle member. If the structure is not receiving information about a group, this member is ignored.")> _
Public CchSubtitle As UInteger
''' <summary>
''' Windows Vista. Pointer to a null-terminated string that contains the text for a task link when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the task text. This item is drawn right-aligned opposite the header text. When clicked by the user, the task link generates an LVN_LINKCLICK notification.
''' </summary>
<Description("Windows Vista. Pointer to a null-terminated string that contains the text for a task link when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the task text. This item is drawn right-aligned opposite the header text. When clicked by the user, the task link generates an LVN_LINKCLICK notification.")> _
<MarshalAs(UnmanagedType.LPWStr)> _
Public PszTask As String
''' <summary>
''' Windows Vista. Size in TCHARs of the buffer pointed to by the pszTask member. If the structure is not receiving information about a group, this member is ignored.
''' </summary>
<Description("Windows Vista. Size in TCHARs of the buffer pointed to by the pszTask member. If the structure is not receiving information about a group, this member is ignored.")> _
Public CchTask As UInteger
''' <summary>
''' Windows Vista. Pointer to a null-terminated string that contains the top description text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the top description text. This item is drawn opposite the title image when there is a title image, no extended image, and uAlign==LVGA_HEADER_CENTER.
''' </summary>
<Description("Windows Vista. Pointer to a null-terminated string that contains the top description text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the top description text. This item is drawn opposite the title image when there is a title image, no extended image, and uAlign==LVGA_HEADER_CENTER.")> _
<MarshalAs(UnmanagedType.LPWStr)> _
Public PszDescriptionTop As String
''' <summary>
''' Windows Vista. Size in TCHARs of the buffer pointed to by the pszDescriptionTop member. If the structure is not receiving information about a group, this member is ignored.
''' </summary>
<Description("Windows Vista. Size in TCHARs of the buffer pointed to by the pszDescriptionTop member. If the structure is not receiving information about a group, this member is ignored.")> _
Public CchDescriptionTop As UInteger
''' <summary>
''' Windows Vista. Pointer to a null-terminated string that contains the bottom description text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the bottom description text. This item is drawn under the top description text when there is a title image, no extended image, and uAlign==LVGA_HEADER_CENTER.
''' </summary>
<Description("Windows Vista. Pointer to a null-terminated string that contains the bottom description text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the bottom description text. This item is drawn under the top description text when there is a title image, no extended image, and uAlign==LVGA_HEADER_CENTER.")> _
<MarshalAs(UnmanagedType.LPWStr)> _
Public PszDescriptionBottom As String
''' <summary>
''' Windows Vista. Size in TCHARs of the buffer pointed to by the pszDescriptionBottom member. If the structure is not receiving information about a group, this member is ignored.
''' </summary>
<Description("Windows Vista. Size in TCHARs of the buffer pointed to by the pszDescriptionBottom member. If the structure is not receiving information about a group, this member is ignored.")> _
Public CchDescriptionBottom As UInteger
''' <summary>
''' Windows Vista. Index of the title image in the control imagelist.
''' </summary>
<Description("Windows Vista. Index of the title image in the control imagelist.")> _
Public ITitleImage As Integer
''' <summary>
''' Windows Vista. Index of the extended image in the control imagelist.
''' </summary>
<Description("Windows Vista. Index of the extended image in the control imagelist.")> _
Public IExtendedImage As Integer
''' <summary>
''' Windows Vista. Read-only.
''' </summary>
<Description("Windows Vista. Read-only.")> _
Public IFirstItem As Integer
''' <summary>
''' Windows Vista. Read-only in non-owner data mode.
''' </summary>
<Description("Windows Vista. Read-only in non-owner data mode.")> _
Public CItems As IntPtr
''' <summary>
''' Windows Vista. NULL if group is not a subset. Pointer to a null-terminated string that contains the subset title text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the subset title text.
''' </summary>
<Description("Windows Vista. NULL if group is not a subset. Pointer to a null-terminated string that contains the subset title text when item information is being set. If group information is being retrieved, this member specifies the address of the buffer that receives the subset title text.")> _
Public PszSubsetTitle As IntPtr
''' <summary>
''' Windows Vista. Size in TCHARs of the buffer pointed to by the pszSubsetTitle member. If the structure is not receiving information about a group, this member is ignored.
''' </summary>
<Description("Windows Vista. Size in TCHARs of the buffer pointed to by the pszSubsetTitle member. If the structure is not receiving information about a group, this member is ignored.")> _
Public CchSubsetTitle As IntPtr
End Structure
Public Enum ListViewGroupMask
None = &H0
Header = &H1
Footer = &H2
State = &H4
Align = &H8
GroupId = &H10
SubTitle = &H100
Task = &H200
DescriptionTop = &H400
DescriptionBottom = &H800
TitleImage = &H1000
ExtendedImage = &H2000
Items = &H4000
Subset = &H8000
SubsetItems = &H10000
End Enum
Public Enum ListViewGroupState
''' <summary>
''' Groups are expanded, the group name is displayed, and all items in the group are displayed.
''' </summary>
Normal = 0
''' <summary>
''' The group is collapsed.
''' </summary>
Collapsed = 1
''' <summary>
''' The group is hidden.
''' </summary>
Hidden = 2
''' <summary>
''' Version 6.00 and Windows Vista. The group does not display a header.
''' </summary>
NoHeader = 4
''' <summary>
''' Version 6.00 and Windows Vista. The group can be collapsed.
''' </summary>
Collapsible = 8
''' <summary>
''' Version 6.00 and Windows Vista. The group has keyboard focus.
''' </summary>
Focused = 16
''' <summary>
''' Version 6.00 and Windows Vista. The group is selected.
''' </summary>
Selected = 32
''' <summary>
''' Version 6.00 and Windows Vista. The group displays only a portion of its items.
''' </summary>
SubSeted = 64
''' <summary>
''' Version 6.00 and Windows Vista. The subset link of the group has keyboard focus.
''' </summary>
SubSetLinkFocused = 128
End Enum
我測試過這個,它對我來說工作得很好。
希望它能幫助你們中的一些人。
相關問題
- 1. 可摺疊視圖
- 2. 可摺疊列表
- 3. 可摺疊列表
- 4. xcode中的可摺疊表格視圖
- 5. 可摺疊名稱列表
- 6. 可擴展列表視圖不會在swift中摺疊
- 7. 可擴展列表視圖不會摺疊
- 8. jQuery Mobile可摺疊列表視圖展開方向向上
- 9. 把可摺疊的面板列表視圖
- 10. xamarin ios中的可摺疊視圖
- 11. Bootstrap 3 - 可摺疊列表不能正常摺疊
- 12. 可摺疊列表 - 使用單獨切換摺疊多個列表項目
- 13. 摺疊YUI樹視圖
- 14. iOS視圖'摺疊'動畫
- 15. 如何創建可摺疊列表?
- 16. Google網站可摺疊列表
- 17. 可摺疊列表,不響應點擊
- 18. 使用JavaScript的可摺疊列表
- 19. Windows Phone 7可摺疊列表
- 20. 展開/可摺疊對象列表
- 21. 移動列表可摺疊項目
- 22. 與jQuery的HTML可摺疊列表
- 23. 的Javascript可摺疊列表和XSL
- 24. 製作有序列表可摺疊
- 25. 無風格可摺疊列表
- 26. 可摺疊的gridview列
- 27. 可摺疊Sankey圖-D3
- 28. 如何改進多層列表中視圖的摺疊/展開?
- 29. Android - 展開/摺疊列表視圖不使用expandablelistview
- 30. 如何動畫列表視圖展開和摺疊
其實,ObjectListView *可以做可摺疊的組 - 但它們只能在Vista上運行。我認爲到目前爲止所提到的所有解決方案都只適用於Vista。 如果他有XP,沒有辦法做到這一點。在那種情況下,你對TreeListView的建議可能是他最好的選擇。 – Grammarian 2009-08-26 06:30:19
謝謝!不知道......我在食譜中看不到它。 – 2009-08-26 06:54:30