views:

24

answers:

1

HI I wonder if anyone will be kind enought to look at the code below and help remedy it. I am grateful to contributors from this site for even getting this far with my aim. The code attempts to do the following, to look at the first element in an array, if it is 1 it then looks at elements 13-16 and identifies the highest value, using a forecolor of BLue,it then puts that value in the first column of a listview and in an adjacent column puts the value of the array stored in element 7. If however the value at element 0 is nought then, using a forecolor or RED elements 19-22 are searched to find the lowest value. This value is then put in the first column and in an adjacent column the value in element 7 is put.

Trouble is it dont work that way. I wonder if anyone could help. Many thanks.

  Public Sub listboxplayer1winodds(ByVal parray, ByVal numbofgames)
    Dim win = System.Drawing.Color.DarkBlue
    Dim lose = System.Drawing.Color.DarkRed
    Dim odds = ""
    Dim cards = ""
    Form1.ListOddsP1.Items.Clear()
    Form1.ListOddsP1.Columns.Add("Odds", -2, HorizontalAlignment.Left)
    Form1.ListOddsP1.Columns.Add("Cards", -2, HorizontalAlignment.Left)
    Dim winlvi As New ListViewItem
    Dim loselvi As New ListViewItem
    For k = numbofgames To 1 Step -1
        If parray(k, 0) = 1 Then
            Form1.ListOddsP1.ForeColor = win

            odds = parray(k, 12)
            cards = parray(k, 7)
            For l = 13 To 16
                If parray(k, l) > odds Then
                    odds = parray(k, l)
                    cards = parray(k, 7)
                End If
            Next l
            Form1.ListOddsP1.ForeColor = win
            winlvi.Text = odds
            Form1.ListOddsP1.Items.Add(winlvi)
            winlvi.SubItems.Add(cards)
            Form1.ListOddsP1.ForeColor = win
        Else

            Form1.ListOddsP1.ForeColor = lose
            odds = parray(k, 18)
            cards = parray(k, 7)
            For l = 19 To 22
                If parray(k, l) > 0 And parray(k, l) < odds Then
                    odds = parray(k, l)
                    cards = parray(k, 7)
                End If
            Next l
            Form1.ListOddsP1.ForeColor = lose
            loselvi.Text = odds.ToString
            Form1.ListOddsP1.Items.Add(loselvi)
            loselvi.SubItems.Add(cards)
            Form1.ListOddsP1.ForeColor = lose
        End If

    Next k


End Sub    

Thanks for all and any help.

A: 

Regarding the extra columns, at the moment you add two new columns every time this method is called, but you never remove them.

The best way would be to move the lines

Form1.ListOddsP1.Columns.Add("Odds", -2, HorizontalAlignment.Left)
Form1.ListOddsP1.Columns.Add("Cards", -2, HorizontalAlignment.Left)

out to some other method that only gets called once during the lifetime of the form (I'd suggest the event handler for Form.Load).

Otherwise, the quick and easy change to your current code would be to change the clear line to not only clear items but to clear columns as well as:

Form1.ListOddsP1.Clear()

Regarding the colouring, you're currently calling something like:

Form1.ListOddsP1.ForeColor

Which says that you want the whole list to have the same colour, what you probably want to do would be to call something like

Form1.ListOddsP1.Items(x).ForeColor

Where x is the index of the row that you want to colour in. Or if you only want to colour in specific cells, then you probably want to do:

Form1.ListOddsP1.Items(x).SubItems(y).ForeColor

Where x is the row and y is the column.

Regarding the error, what line gives that error?

ho1
Thank you!Thank you! Thank you! I have only the number of columns that I want and the data is presented in the colours that I want! As for the error I solved that, the lines Dim winlvi As New ListViewItem and Dim loselvi As New ListViewItem were in the wrong place in the loop. Thanks to all who helped.
simon