?
Solved

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

Posted on 2005-05-09
9
Medium Priority
?
202 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:indy500fan
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13961655
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13961667
Secondly, I don't see where you are selecting anything within the ListBox.

Bob
0
 

Author Comment

by:indy500fan
ID: 13961785
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!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 13961847
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



0
 

Author Comment

by:indy500fan
ID: 13961902
 
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"))
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13961908
Then, you need to set selected item also:

lvQualificationsStandings3.SelectedItem = li

Bob
0
 

Author Comment

by:indy500fan
ID: 13961966
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 13962113
Ok, so highlighting ListView items, and not selecting ListBox rows.  Man, it's Monday :)

I am starting to wake up, I guess :)

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

Let's examine this code, and see if I am really understanding.  Set the item colors, then create a new item.

My guess is that it should be:
 li = lvQualificationsStandings3.Items.Add(dr("Car Number"))
 li.SubItems.Add(dr("QualifyingSpeed"))
 li.BackColor = Color.Yellow '<<< NEW LINE
 li.ForeColor = Color.Black

Otherwise, you would be changing the color for the previously added item held by li.

Bob
0
 

Author Comment

by:indy500fan
ID: 13962157
Ding Ding!  We have a winner!!!  That was it.  I knew it was something silly like that!!!

Thanks a bunch!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question