[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

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?
0
tiehaze
Asked:
tiehaze
  • 4
  • 3
  • 3
2 Solutions
 
Wayne Taylor (webtubbs)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
0
 
tiehazeAuthor Commented:
How do I set it up to search all of the child containers also (i.e. I have the listviews scattered throughout splitcontainers)
0
 
Wayne Taylor (webtubbs)Commented:
Use a Recursive loop....

    Public Sub AddColumns(ByVal cont As Control)
        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
            ElseIf ctrl.Controls.Count > 0 Then
                AddColumns(ctrl)
            End If
        Next
    End Sub

...and call it initially like this....

    AddColumns(Me)

Wayne
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tiehazeAuthor 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
0
 
ColemssCommented:
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
0
 
tiehazeAuthor 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'?
0
 
ColemssCommented:
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
0
 
tiehazeAuthor 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
0
 
ColemssCommented:
   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
0
 
Wayne Taylor (webtubbs)Commented:
Sorry, I forgot to replace Me with cont....

    Public Sub AddColumns(ByVal cont As Control)
        For Each ctrl As Control In cont.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
            ElseIf ctrl.Controls.Count > 0 Then
                AddColumns(ctrl)
            End If
        Next
    End Sub

Wayne
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now