tiehaze
asked on
Add columns to all ListViews in the form
I have a windows application that, when the form loads, I need to add two columns to each of the ListViews in the form. I know you might be asking youself, why don't you add the columns in the design view... I can't because of some other code that I am using. Here is what I have:
AccountsLV.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
AccountsLV.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.AccountsLV.ColumnClick, AddressOf ListView_ColumnClick
ListView1.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
ListView1.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.ListView1.ColumnClick, AddressOf ListView_ColumnClick
ListView2.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
ListView2.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.ListView2.ColumnClick, AddressOf ListView_ColumnClick
ListView3.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
ListView3.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.ListView3.ColumnClick, AddressOf ListView_ColumnClick
How do I set it up to do this code for all listviews in one set of code, instead of having to repeat it over and over?
AccountsLV.Columns.Add(New
AccountsLV.Columns.Add(New
AddHandler Me.AccountsLV.ColumnClick,
ListView1.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
ListView1.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.ListView1.ColumnClick, AddressOf ListView_ColumnClick
ListView2.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
ListView2.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.ListView2.ColumnClick, AddressOf ListView_ColumnClick
ListView3.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
ListView3.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.ListView3.ColumnClick, AddressOf ListView_ColumnClick
How do I set it up to do this code for all listviews in one set of code, instead of having to repeat it over and over?
ASKER
How do I set it up to search all of the child containers also (i.e. I have the listviews scattered throughout splitcontainers)
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Am I missing something, you declared: ByVal cont As Control but didn't use it in the code below it. Am I overlooking it?
As of right now, I am getting the error:
{System.StackOverflowExcep tion}
on line:
For Each ctrl As Control In Me.Controls
As of right now, I am getting the error:
{System.StackOverflowExcep
on line:
For Each ctrl As Control In Me.Controls
Public Class Form1
Private lvc As ArrayList
'This would only be a better approach because it does not loop through everything.
Public Sub intit()
'add all created list views
lvc.Add(New ListView)
End Sub
Public Sub AddColumns(ByVal lv As ArrayList)
For Each ListView In lv
lv.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
lv.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.lv.ColumnClick, AddressOf ListView_ColumnClick
Next
End Sub
End Class
Private lvc As ArrayList
'This would only be a better approach because it does not loop through everything.
Public Sub intit()
'add all created list views
lvc.Add(New ListView)
End Sub
Public Sub AddColumns(ByVal lv As ArrayList)
For Each ListView In lv
lv.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
lv.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.lv.ColumnClick, AddressOf ListView_ColumnClick
Next
End Sub
End Class
ASKER
I do not quite understand what you are doing here:
Private lvc As ArrayList
Public Sub intit()
lvc.Add(New ListView)
End Sub
Do I need to add all of the listviews or does it automatically check. Second, how do I call 'AddColumns'?
Private lvc As ArrayList
Public Sub intit()
lvc.Add(New ListView)
End Sub
Do I need to add all of the listviews or does it automatically check. Second, how do I call 'AddColumns'?
put this in your form loading
lvc.Add(ListView1)
lvc.Add(ListView2)
lvc.Add(ListView3)
lvc.Add(ListView4)
then call AddColumns
or even easier in the form loading just call
AddColumns(ListView1)
AddColumns(ListView2)
AddColumns(ListView3)
AddColumns(ListView4)
Public Sub AddColumns(ByVal lv As ListView)
lv.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
lv.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.lv.ColumnClick, AddressOf ListView_ColumnClick
end sub
lvc.Add(ListView1)
lvc.Add(ListView2)
lvc.Add(ListView3)
lvc.Add(ListView4)
then call AddColumns
or even easier in the form loading just call
AddColumns(ListView1)
AddColumns(ListView2)
AddColumns(ListView3)
AddColumns(ListView4)
Public Sub AddColumns(ByVal lv As ListView)
lv.Columns.Add(New ColHeader("Account #", 85, HorizontalAlignment.Left, True))
lv.Columns.Add(New ColHeader("Account Name", 300, HorizontalAlignment.Left, True))
AddHandler Me.lv.ColumnClick, AddressOf ListView_ColumnClick
end sub
ASKER
That was my question to begin with... I am going to have over 20 listviews and I don't want to have to do:
AddColumns(ListView1)
AddColumns(ListView2)
AddColumns(ListView3)
AddColumns(ListView4)
etc.
I need to somehow have the code find all of the listviews in the parent and child controls, and then add the columns to the found listviews
AddColumns(ListView1)
AddColumns(ListView2)
AddColumns(ListView3)
AddColumns(ListView4)
etc.
I need to somehow have the code find all of the listviews in the parent and child controls, and then add the columns to the found listviews
Public Sub getlistviews(ByVal cc As ControlCollection)
Dim ccc As ControlCollection = Nothing
For Each ctrl As Control In cc
If ctrl.GetType Is GetType(ListView) Then
Dim lv As ListView = CType(ctrl, ListView)
lv.Columns.Add("Account #", 85, HorizontalAlignment.Left)
lv.Columns.Add("Account Name", 300, HorizontalAlignment.Left)
AddHandler lv.ColumnClick, AddressOf ListView_ColumnClick
ElseIf ctrl.Controls.Count > 0 Then
ccc.Add(ctrl)
getlistviews(ccc)
End If
Next
End Sub
Dim ccc As ControlCollection = Nothing
For Each ctrl As Control In cc
If ctrl.GetType Is GetType(ListView) Then
Dim lv As ListView = CType(ctrl, ListView)
lv.Columns.Add("Account #", 85, HorizontalAlignment.Left)
lv.Columns.Add("Account Name", 300, HorizontalAlignment.Left)
AddHandler lv.ColumnClick, AddressOf ListView_ColumnClick
ElseIf ctrl.Controls.Count > 0 Then
ccc.Add(ctrl)
getlistviews(ccc)
End If
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Loop through all the controls on the form, check if it's a ListView, then add the columns....
For Each ctrl As Control In Me.Controls
If ctrl.GetType Is GetType(ListView) Then
Dim lv As ListView = CType(ctrl, ListView)
lv.Columns.Add("Account #", 85, HorizontalAlignment.Left)
lv.Columns.Add("Account Name", 300, HorizontalAlignment.Left)
AddHandler lv.ColumnClick, AddressOf ListView_ColumnClick
End If
Next
Regards,
Wayne