Listview horizontal scroll bar keeps disapearing

RoyLittle0

Active member
Joined
Nov 17, 2012
Messages
38
Location
Derby, Uk
Programming Experience
Beginner
Hi,

I have a form that updates 12 ListViews on a click, all works fine they update all the way to the end 1 - 12, then, with all ListViews full ii click on the upper ListViews i loose the horizontal scroll function on the next Listviews, this unly seems to happen when they have updated.
All listviews are set to 3 columns at 200, 90, 2000

Any idea why??

Image2.png

VB.NET:
Option Strict On
Imports System.Xml
Imports System.IO


Public Class Form1
    Dim xmlDoc As New XmlDocument()


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strmReader As New StreamReader("C:\Users\Roy Littlewood\Documents\Visual Studio 2010\Projects\WindowsApplication2\WindowsApplication2\bin\Debug\IBS2000.xml", System.Text.Encoding.UTF8)
        Dim xmlReader As XmlTextReader = New XmlTextReader(strmReader)
        Me.AutoScroll = True
        Me.VScroll = True
        Me.HScroll = True
        'read the file and load the first list view
        xmlReader.WhitespaceHandling = WhitespaceHandling.None
        xmlDoc.Load(xmlReader)


        Dim strXMLPos As String = "//Root/Root1/Section1"
        Dim xml_LV1List As XmlNodeList = xmlDoc.SelectNodes(strXMLPos)
        For Each curNode As XmlNode In xml_LV1List
            Dim LVI As New ListViewItem
            With LVI
                .Text = curNode.Attributes.ItemOf("Name").InnerText
                .SubItems.Add(curNode.Attributes.ItemOf("id").InnerText)
                .SubItems.Add(curNode.Attributes.ItemOf("Label").InnerText)
            End With
            ListView_1.Items.Add(LVI)
        Next
        'add each list view header
        AddListViewHeader(Listview_1)
        AddListViewHeader(ListView_2)
        AddListViewHeader(Listview_3)
        AddListViewHeader(Listview_4)
        AddListViewHeader(Listview_5)
        AddListViewHeader(Listview_6)
        AddListViewHeader(Listview_7)
        AddListViewHeader(Listview_8)
        AddListViewHeader(Listview_9)
        AddListViewHeader(Listview_10)
        AddListViewHeader(Listview_11)
        AddListViewHeader(Listview_12)
    End Sub


    'this add's the list view headers
    Private Sub AddListViewHeader(ByVal LV As ListView)
        Dim Headers() As ColumnHeader = {New ColumnHeader With {.Text = "Name", .Width = 200},
                                         New ColumnHeader With {.Text = "ID", .Width = 90, .TextAlign = HorizontalAlignment.Center},
                                         New ColumnHeader With {.Text = "Label", .Width = 2000}}
        LV.Columns.Clear()
        LV.Columns.AddRange(Headers)
    End Sub


    'see that we handle all the listview SelectedIndexChanged events here
    Private Sub ListView_1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Listview_1.SelectedIndexChanged, ListView_2.SelectedIndexChanged, Listview_3.SelectedIndexChanged, Listview_4.SelectedIndexChanged, Listview_5.SelectedIndexChanged, Listview_6.SelectedIndexChanged, Listview_7.SelectedIndexChanged, Listview_8.SelectedIndexChanged, Listview_9.SelectedIndexChanged, Listview_10.SelectedIndexChanged, Listview_11.SelectedIndexChanged, Listview_12.SelectedIndexChanged
        'cast the clicked list view to the listview type
        Dim myListView As ListView = DirectCast(sender, ListView)


        If myListView.SelectedItems.Count > 0 Then
            Const MaxListViews As Integer = 12
            Const ControlSelector As String = "ListView_"


            'identify and save the node location here
            Dim NodeLocation As Integer = CInt(myListView.Name.Substring(myListView.Name.LastIndexOf("_") + 1, (myListView.Name.Length - 1) - myListView.Name.LastIndexOf("_")))


            'clear the other list views
            For ctrlCount As Integer = NodeLocation + 1 To 12
                Dim ControlToClear As ListView = DirectCast(Me.Controls(ControlSelector & ctrlCount), ListView)
                ControlToClear.Items.Clear()
            Next


            If NodeLocation < MaxListViews Then
                'set an object to the next control to be updated
                Dim myNextControl As ListView = DirectCast(Me.Controls(ControlSelector & NodeLocation + 1), ListView)
                Dim strXMLPos As String = "//Root"
                'identify the selected ID of the element that has been clicked
                Dim SelectedID As Integer = CInt(myListView.SelectedItems(0).SubItems(1).Text)


                'create the string to read nodes from the file
                For Counter As Integer = 1 To NodeLocation + 1
                    strXMLPos += "/Root" & Counter & "/Section" & Counter
                Next


                'get the node list at the specified location
                Dim nodesToDisplay = (From MyNode In xmlDoc.SelectNodes(strXMLPos) Select MyNode, NodeID = CInt(DirectCast(MyNode, XmlNode).ParentNode.ParentNode.Attributes.ItemOf("id").InnerText) Where NodeID = SelectedID)
                For Each curNode In nodesToDisplay
                    Dim myNode As XmlNode = DirectCast(curNode.MyNode, XmlNode)
                    Dim LVI As New ListViewItem
                    With LVI
                        .Text = myNode.Attributes.ItemOf("Name").InnerText
                        .SubItems.Add(myNode.Attributes.ItemOf("id").InnerText)
                        .SubItems.Add(myNode.Attributes.ItemOf("Label").InnerText)
                    End With
                    myNextControl.Items.Add(LVI)
                Next
                AddListViewHeader(myNextControl)
            End If
        End If
    End Sub


Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click


End Sub


Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click


End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Form4.Show()
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Form3.Show()
End Sub


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Form2.Show()
End Sub


Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Me.Close()
End Sub
End Class
 
Hi Roy,

You have obviously set the HeaderStyle property to None on the ListView's so all you need to do is remove the AddListViewHeader(myNextControl) line in the code and it will retain the header style that was applied when you loaded the headers in the form load event.

Hope that helps.

Cheers,

Ian
 
Hi Ian,

I have tried that by commenting them out but it still does the same, its only happens when i have gone backwards and from then on it is not showing the scroll bars.

Roy
 
Hi Roy,

Hmm.. I am not sure then since it retains the style at my end when I go backwards? If you are happy to do so, then zip up your project along with the XML file and post to the forum so that we can have a look at what you have got. If you are not happy to post to the forum then email me privately, if you want to, at ianbryder@googlemail.com

Cheers,

Ian
 
Hi Ian,

I did try to send them to you direct but GoogleMail will not allow exe's to be sent via mail, didn't want to rename then, so here you are.

Roy
 
Last edited:
Hi Roy,

It works fine my end if you just comment out/delete the line AddListViewHeader(myNextControl)? You need to do this in the ListView_1_SelectedIndexChanged event.

Are you sure you tried this?

Cheers,

Ian
 
Hi Ian,

Sorry my mistake i was commenting out "AddListViewHeader(ListView_2)" all the way to 12, thinking that this was the problem.

Thanks again,

Roy
 
Hi Roy,

No worries, I thought I was going bonkers there for a moment.

Glad you have it sorted and you may want to ask the forum moderators to remove your posted application if you are not able to do it yourself.

Cheers,

Ian
 
It would normally be Brain to Mouth filter problem, as i aparently speak just before thinking, gets people a little upset when i say what is in my head before thinking what i should say :homer:
 
We have all been there and done that Roy so don't worry about it.

The only thing that you need to consider in life is that no one knows everything and therefore everyone has a different view about how to do the things that need to be done.

Cheers,

Ian
 
Back
Top