We help IT Professionals succeed at work.

Add columns to all ListViews in the form

227 Views
Last Modified: 2013-11-27
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?
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi tiehaze,

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

Author

Commented:
How do I set it up to search all of the child containers also (i.e. I have the listviews scattered throughout splitcontainers)
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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.StackOverflowException}

on line:

For Each ctrl As Control In Me.Controls

Commented:
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

Author

Commented:
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'?

Commented:
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

Author

Commented:
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

Commented:
   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
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.