Link to home
Start Free TrialLog in
Avatar of indy500fan
indy500fan

asked on

Why is the wrong item highlighted - when using the following code?

Friends,

Maybe I'm missing something with this, or maybe I've been looking at it too long, but...

When I run the following sub, the wrong row is highlighted (and it seems to be highlighting the row in the previous listview - acting like (i - 1) is the result).

For example if lblPosition.Text = 3 then invariably the first row in lvQualificationsStandings2 is highlighted when the first row in lvQualificationsStandings3 should be highlighted.

Any ideas what is wrong?

when you look at this code, imagine that lblPosition.Text = 3


    Public Sub QualificationStandings(ByVal dsn)
        Dim Server As String

        Dim data As New DataSet
        Dim r As DataRow

        'Dim ColumnCount As Integer

        lvQualificationsStandings1.Items.Clear()
        lvQualificationsStandings2.Items.Clear()
        lvQualificationsStandings3.Items.Clear()

        Dim con As New SqlClient.SqlConnection(dsn)

        'fill in the data for a lap ...
        data = New DataSet

        Dim dr As SqlClient.SqlDataReader

        con = New SqlClient.SqlConnection(dsn)
        con.Open()

        Dim c As New SqlClient.SqlCommand("QualificationStandingsSP", con) ' " & txtLapToUse.Text, con)
        dr = c.ExecuteReader(CommandBehavior.SingleResult)

        Dim found As Boolean
        Dim li As ListViewItem
        Dim i As Integer = 0
        found = False

       
        While dr.Read()

            i = (i + 1)
            'li = lvQualificationsStandings.Items.Add(CarNumber)

            If dr.Item("QualID") <> Nothing Then

                If i = 1 Or i = 4 Or i = 7 Or i = 10 Or i = 13 Or i = 16 Or i = 19 Or i = 22 Or i = 25 Or i = 28 Or i = 31 Then

                    If lblCurrentPosition.Text = Nothing Then

                        If i Mod 2 = 0 Then
                            'li.BackColor = Color.White
                        Else
                            'li.BackColor = Color.LightBlue
                        End If
                        li = lvQualificationsStandings1.Items.Add(dr("Car Number"))
                        li.SubItems.Add(dr("QualifyingSpeed"))

                    ElseIf i = lblCurrentPosition.Text Then

                        li.BackColor = Color.Yellow '<<< NEW LINE
                        li.ForeColor = Color.Black
                        li = lvQualificationsStandings1.Items.Add(dr("Car Number"))
                        li.SubItems.Add(dr("QualifyingSpeed"))
                    Else
                        li = lvQualificationsStandings1.Items.Add(dr("Car Number"))
                        li.SubItems.Add(dr("QualifyingSpeed"))
                    End If

            ElseIf i = 2 Or i = 5 Or i = 8 Or i = 11 Or i = 14 Or i = 17 Or i = 20 Or i = 23 Or i = 26 Or i = 29 Or i = 32 Then

                If lblCurrentPosition.Text = Nothing Then

                    If i Mod 2 = 0 Then
                        'li.BackColor = Color.White
                    Else
                        'li.BackColor = Color.LightBlue
                    End If
                    li = lvQualificationsStandings2.Items.Add(dr("Car Number"))
                    li.SubItems.Add(dr("QualifyingSpeed"))

                ElseIf i = lblCurrentPosition.Text Then

                    li.BackColor = Color.Yellow '<<< NEW LINE
                    li.ForeColor = Color.Black
                    li = lvQualificationsStandings2.Items.Add(dr("Car Number"))
                    li.SubItems.Add(dr("QualifyingSpeed"))
                Else
                    li = lvQualificationsStandings2.Items.Add(dr("Car Number"))
                        li.SubItems.Add(dr("QualifyingSpeed"))
                    End If

            ElseIf i = 3 Or i = 6 Or i = 9 Or i = 12 Or i = 15 Or i = 18 Or i = 21 Or i = 24 Or i = 27 Or i = 30 Or i = 33 Then

                    If lblCurrentPosition.Text = Nothing Then

                        If i Mod 2 = 0 Then
                            'li.BackColor = Color.White
                        Else
                            'li.BackColor = Color.LightBlue
                        End If
                        li = lvQualificationsStandings3.Items.Add(dr("Car Number"))
                        li.SubItems.Add(dr("QualifyingSpeed"))

                    ElseIf i = lblCurrentPosition.Text Then

                        li.BackColor = Color.Yellow '<<< NEW LINE
                        li.ForeColor = Color.Black
                        li = lvQualificationsStandings3.Items.Add(dr("Car Number"))
                        li.SubItems.Add(dr("QualifyingSpeed"))
                    Else
                        li = lvQualificationsStandings3.Items.Add(dr("Car Number"))
                        li.SubItems.Add(dr("QualifyingSpeed"))
                    End If
                End If
            End If

        End While
        con.Close()
        con = Nothing
    End Sub
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

First things first:

If i = 1 Or i = 4 Or i = 7 Or i = 10 Or i = 13 Or i = 16 Or i = 19 Or i = 22 Or i = 25 Or i = 28 Or i = 31 Then

   could be:

If (i - 1) Mod 3 = 0 Then

  and

     ElseIf i = 2 Or i = 5 Or i = 8 Or i = 11 Or i = 14 Or i = 17 Or i = 20 Or i = 23 Or i = 26 Or i = 29 Or i = 32 Then

   could be:

     ElseIf (i - 2) Mod 3 = 0 Then

  and

     ElseIf i = 3 Or i = 6 Or i = 9 Or i = 12 Or i = 15 Or i = 18 Or i = 21 Or i = 24 Or i = 27 Or i = 30 Or i = 33 Then

   could be:

     ElseIf i Mod 3 = 0 Then

Bob
Secondly, I don't see where you are selecting anything within the ListBox.

Bob
Avatar of indy500fan
indy500fan

ASKER

Bob,

I was going to ask about fixing that stuff (getting rid of all the Or's) in another question, but thanks, you saved me a question.

I'm a bit confused as to your question.  I'm not sure about the listbox thing.  

Right now, another part of my program assigns the value for the label (lblPosition.txt) for Position.  If that value for position has been assigned, I need that corresponding row to be highlighted in whatever listview that happens to fall in.

Here is a visual reference.

Data:

Position - Car - Speed (Sorted Descending on Speed)
1 - 10 - Speed1
2 - 15 - Speed2  
3 - 16 - Speed3
4 - 33 - Speed4
5 - 34 - Speed5
6 - 27 - Speed6
7 - 18 - Speed7
8 - 21 - Speed8
9 - 27 - Speed6
and so forth...

A form has three listviews

Row    |      ListView1  |  ListView2     |  Listview3
 
1         |  10 - Speed1  |  15 - Speed2  |  16 - Speed3 <---- This one should be highlighted, since lblPosition.Text = 3
2         |  33 - Speed4  |  34 - Speed5  |  27 - Speed6
3         |  18 - Speed7  |  21 - Speed8  |  19 - Speed9
.
..
....and so forth

Thanks!
Ok, ListView, then.  

I don't see where you are setting the SelectedItem for ListView3.  You are adding items to the ListView, but not:

lvQualificationsStandings3.SelectedItem = item (whatever item needs to be)

Bob



 
Bob,

I called it wrong, it is like this:

ElseIf i = lblCurrentPosition.Text Then

                        li.BackColor = Color.Yellow '<<< NEW LINE
                        li.ForeColor = Color.Black
                        li = lvQualificationsStandings3.Items.Add(dr("Car Number"))
                        li.SubItems.Add(dr("QualifyingSpeed"))
Then, you need to set selected item also:

lvQualificationsStandings3.SelectedItem = li

Bob
I'm really confusing... :)

It's not actually selected.  Actually the row has it's background and foreground colors changed from the rest.  Nothing is actually selected.  That was my fault on the terminology.

Sorry.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ding Ding!  We have a winner!!!  That was it.  I knew it was something silly like that!!!

Thanks a bunch!