Working on a vba module to dynamically create a form with two ListView components. Started out with ListBox components, but need to control color of individual items in the List, and therefore need to change to ListView components instead. For some reason the ListView can't be easily sized and positioned similarly to the ListBox component, and I have tried various options to no avail.
The Form looks something like this:
The ListViews are embedded in two frames, to at least position them appropriately, but I still can't get the size to work.
Below is some of the code to generate the form and add the first ListView
------
I of course tried to set .Left, .Top, .Height, and .Width - which are non existing for ListView
Interestingly enough, I found an example of a form with a ListView, and if I don't try to create it dynamically, but instead add a ListView to a form in the project, for some reason I am able to set width, height, etc. That off course doesn't help me either - as I want to be able to add this module to any project, without having to add the form.
I then tried to get the Window Handle of the ListView and use SetWindowPos to modify it:
This doesn't give me any errors, but it also doesn't change anything either.
Does anyone out there have a good example of how to dynaically add a ListView to a form, and size it and position it on the form?
Any help would be greatly appreciated.
Thanks
The Form looks something like this:
The ListViews are embedded in two frames, to at least position them appropriately, but I still can't get the size to work.
Below is some of the code to generate the form and add the first ListView
VBA Code:
Private Sub ModuleSelectForm()
Dim myForm As Object
' ListView Components
Dim LFrame As Frame
Dim SFrame As Frame
Dim LoadedModuleListBox As ListView
...
Dim ListViewHandle As Long
Dim ListViewRect As Rect
...
...
' Add The form
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
Set vbaModulesForm = myForm
'Create the User Form
With myForm
.Properties("Caption") = "Manage Expense Files"
.Properties("Width") = 410
.Properties("Height") = 350
End With
'Create Load Module ListBox inside Frame
Set LFrame = myForm.Designer.Controls.Add("Forms.Frame.1")
With LFrame
.Caption = ""
.BorderStyle = fmBorderStyleNone
.KeepScrollBarsVisible = fmScrollBarsNone
.ScrollBars = fmScrollBarsNone
.SpecialEffect = fmSpecialEffectFlat
.Top = 5
.Left = 10
.Width = 150
.Height = 150
Set LoadedModuleListBox = LFrame.Controls.Add("MSComctlLib.ListViewCtrl.2", "LoadedModuleListBox")
With LoadedModuleListBox
.Visible = True
.View = lvwList
.MultiSelect = True
.CheckBoxes = False
.HideColumnHeaders = True
.HideSelection = False
.LabelEdit = False
.BorderStyle = fmBorderStyleOpaque
'.Top = 5 ' Doesn't work
'.Left = 10 ' Commented out
'.Width = 150
'.Height = 150
End With
End With
...
End Sub
------
I of course tried to set .Left, .Top, .Height, and .Width - which are non existing for ListView
Interestingly enough, I found an example of a form with a ListView, and if I don't try to create it dynamically, but instead add a ListView to a form in the project, for some reason I am able to set width, height, etc. That off course doesn't help me either - as I want to be able to add this module to any project, without having to add the form.
I then tried to get the Window Handle of the ListView and use SetWindowPos to modify it:
VBA Code:
Private Sub UserForm_Initialize()
Dim LstItem As ListItem
Dim ListViewHandle As Long
Dim ListViewRect As Rect
' Size / Position of Listview
With Me.LoadedModuleListBox
ListViewHandle = .hWnd
Call GetWindowRect(ListViewHandle, ListViewRect)
SetWindowPos ListViewHandle, 0, ListViewRect.Left + 5, ListViewRect.Top + 10, 150, 150, &H40
..
End With
...
End Sub
This doesn't give me any errors, but it also doesn't change anything either.
Does anyone out there have a good example of how to dynaically add a ListView to a form, and size it and position it on the form?
Any help would be greatly appreciated.
Thanks