ListView auto selecting

ZeroEffect

Junior Contributor
Joined
Oct 24, 2004
Messages
204
Location
Detroit, MI
What I'm doing is taking a list of items based on day of the week and adding it to a new Listview. I am having Two problems.

Problem one:
while the progam loads I load one listview from a file then scan items in that listview and add the ones i want to a new listview as described above. But It doesn't work. My band aide cure was to add a time and delay the event by 1 millisecond. Now the second list loads.

Problem Two:
In the second listview I loop through the first column comparing it's data to the current time and if the time in the listview is greater than the current time exit the sub. But I have the same problem as above nothing gets selected, event if I time delay it.

If either sub is run after the form is completly loaded the work perfect.

Any thoughts this has me banging my head against the wall. :confused:

Thanks for your help

ZeroEffect

Here is the code
Visual Basic:
    Public Sub NextTimedEvent()
        On Error Resume Next
        Dim TimeListDay As ListView.SelectedListViewItemCollection = MainForm.PlayListView(8).SelectedItems
        Dim SelectedTime As ListViewItem
        Dim itm As ListViewItem
        Dim I
        Dim strDay As String
        Dim CurrentDayStr As String
        Dim temp As Integer
        MainForm.PlayListView(9).Items.Clear()

        CurrentDayStr = Date.Today.DayOfWeek.ToString
        temp = 1
        Do
            MainForm.PlayListView(8).Items(temp - 1).Selected = True
            For Each SelectedTime In TimeListDay 'Here is where the error happens when the sub is run before the from is loaded --- SelectedTime = nothing
                strDay = SelectedTime.SubItems(2).Text

                If strDay = CurrentDayStr Then

                    itm = MainForm.PlayListView(9).Items.Add(SelectedTime.Text)
                    itm.SubItems.Add(SelectedTime.SubItems(1).Text)
                    itm.SubItems.Add(SelectedTime.SubItems(2).Text)
                    itm.SubItems.Add(SelectedTime.SubItems(3).Text)

                End If
            Next
            temp = temp + 1
        Loop Until temp > MainForm.PlayListView(8).Items.Count
        MainForm.PlayListView(8).Items(0).Selected = True
        If LVR = 1 Then 'Using same timer controll
            NextInList()
        Else
            MainForm.Timer5.Enabled = True
        End If
    End Sub

    Public Sub NextInList()
        Dim TimeListDay As ListView.SelectedListViewItemCollection = MainForm.PlayListView(9).SelectedItems
        Dim SelectedTime As ListViewItem
        Dim strtime As Date
        Dim inttime As Integer
        Dim CurrentTimeStr As String
        Dim CurrentTimeInt As Integer
        Dim temp As Integer

        CurrentTimeStr = Replace(Format(Now(), "HH:mm"), ":", "")
        CurrentTimeInt = CurrentTimeStr

        temp = 1
        Do
            MainForm.PlayListView(9).Items(temp - 1).Selected = True
            For Each SelectedTime In TimeListDay 'Here is where the error happens when the sub is run before the from is loaded --- SelectedTime = nothing

                strtime = SelectedTime.Text

                inttime = Replace(Format(strtime, "HH:mm"), ":", "")

                If inttime > CurrentTimeInt Then
                    'Add Info to Label                    
                    Exit Sub
                Else
                    SelectedTime.Checked = True
                    SelectedTime.ForeColor = System.Drawing.Color.Gray
                    SelectedTime.Font = New Font(SelectedTime.Font, FontStyle.Strikeout)
                End If
            Next
            temp = temp + 1
        Loop Until temp > MainForm.PlayListView(9).Items.Count
    End Sub
 
Does each ListView have only one column (Field)? It looks like it has at least 1 item and 3 subitems. I don't think you can compare a complete listview line to a specific subitem.

Are you only wanting to compare a date or a time and date?
If I understanding correctly, you want to load one listview then load a second listview based on a date variable from the first listview...Is that correct?
 
Thanks for the response

DiverDan said:
Does each ListView have only one column (Field)? It looks like it has at least 1 item and 3 subitems. I don't think you can compare a complete listview line to a specific subitem.

Are you only wanting to compare a date or a time and date?
If I understanding correctly, you want to load one listview then load a second listview based on a date variable from the first listview...Is that correct?

Both ListViews are Identical. 4 columns, Start Time, End Time, Day, Description. Listview(8) is holding data that can be added to, removed or updated. Listview(9) is todays events from listview(8).


You are correct DiverDan.

The code above grabs the day of the week and then scans the second subitem, column 3 "Day" for matches if it matches it adds that row to Listview(9).


Well I found out my band aid cure does work with both Listviews. One of the Listviews is on a tabstrip and if that tab is not selected The code doesn't work. So I switched to the tab the code runs then switches back to the first tab.

it's a bandaid cure but it works.
 
Ok, but I think there's an easier way...
Why not populate the tabbed listview when the tab page is opened, not before.

For i = 0 To Listview1.Items.Count - 1
If 'blah Then
With ListView2.Items.Add('something)
.SubItems.Add('more)
'etc
End With
End If
Next
 
DiverDan That is what i'm doing but here is how I have to call it.
Visual Basic:
    Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
        If LVR = 0 Then
            MainForm.TabControl1.SelectedIndex = 3
            Timer5.Enabled = False
            NextTimedEvent()
            LVR = 1
        Else
            NextInList()
            Timer5.Enabled = False
            MainForm.TabControl1.SelectedIndex = 0
        End If

    End Sub

This is the only way I can get it to work.

ZeroEffect
 
Back
Top