Hi Im trying to achieve something which seems simple but is proving difficult. I have listview1 which will display the values of two textbox's when a button is clicked. I have listview2 which will make a calculation based on the two values in listview1 when a button is clicked. What i would like to achieve is how to get the total of the rows in listview2 and display in the last column of listview2. Also is there a easy way of doing this as my code seems a bit long winded because i have added 10 columns in listview which might not be needed. I have included my code.

Many thanks for any help as this is a real headache for me.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateListview1()
        CreateListview2()

        TextBox1.Text = 10
        TextBox2.Text = 15
    End Sub
    Public Sub CreateListview1()

        ListView1.View = View.Details
        ListView1.GridLines = True
        ListView1.Columns.Add("Textbox1", 70, HorizontalAlignment.Left)
        ListView1.Columns.Add("Textbox2", -2, HorizontalAlignment.Left)


    End Sub


    Public Sub CreateListview2()

        ListView2.View = View.Details
        ListView2.GridLines = True
        ListView2.Columns.Add("Number", 50, HorizontalAlignment.Left)
        ListView2.Columns.Add("(Row 1 Textbox1* Row 1 textbox2)/number", 150, HorizontalAlignment.Left)
        ListView2.Columns.Add("(Row 2 Textbox1* Row 2 textbox2)/number", 150, HorizontalAlignment.Left)
        ListView2.Columns.Add("(Row 3 Textbox1* Row 3 textbox2)/number", 150, HorizontalAlignment.Left)
        ListView2.Columns.Add("(Row 4 Textbox1* Row 4 textbox2)/number", 150, HorizontalAlignment.Left)
        ListView2.Columns.Add("(Row 5 Textbox1* Row 5 textbox2)/number", 150, HorizontalAlignment.Left)
        ListView2.Columns.Add("(Row 6 Textbox1* Row 6 textbox2)/number", 150, HorizontalAlignment.Left)
        ListView2.Columns.Add("(Row 7 Textbox1* Row 7 textbox2)/number", 150, HorizontalAlignment.Left)
        ListView2.Columns.Add("(Row 8 Textbox1* Row 8 textbox2)/number", 150, HorizontalAlignment.Left)
        ListView2.Columns.Add("(Row 9 Textbox1* Row 9 textbox2)/number", 150, HorizontalAlignment.Left)
        ListView2.Columns.Add("(Row 10 Textbox1* Row 10 textbox2)/number", 150, HorizontalAlignment.Left)

    End Sub



    Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
        ListView2.Items.Clear()
        For i = 1 To 10 Step 1

            Dim item2 As New ListViewItem(i)

            ListView2.Items.AddRange(New ListViewItem() {item2})

            For row = 0 To ListView1.Items.Count - 1

                item2.SubItems.Add(Formula(ListView1.Items(row).SubItems(0).Text, ListView1.Items(row).SubItems(1).Text, i))
            Next

            Next

    End Sub

    Private Function Formula(ByVal Value1 As Single, ByVal value2 As Single, ByVal value3 As Single) As Single


        Return (Value1 * value2) / value3


    End Function

    Private Sub AddTextboxValues_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddTextboxValues.Click


        Dim item1 As New ListViewItem(TextBox1.Text)
        item1.SubItems.Add(TextBox2.Text)

        ListView1.Items.AddRange(New ListViewItem() {item1})

    End Sub
End Class

You can do something like this:

    Dim itm As ListViewItem
    Dim total As Single
    For Each itm In ListView1.Items
        If itm IsNot Nothing Then
            For i = 0 To itm.SubItems.Count - 1
                total += CSng(itm.SubItems.Item(i).ToString)
            Next
            itm.SubItems.Add(total)
        End If
    Next

See if this works.

I am trying to understand what you want to accomplish here but I think there is a real disconnect between your explanation and the code so let's just scrap the code for now. Forget TextBoxes, ListViews, etc. and just tell me in English what you are trying to do. Pretend that computers do not exist and you are doing it entirely by hand with pencil and paper. What I gather is that you have two numbers which, for the sake of convenience, we'll call T1 and T2 (any resemblance to TextBox1 and TextBox2 is entirely intentional). You also have a table which contains ten rows (but which may contain more or fewer). I don't know how many columns are in this table. The best way, I think, to make your intentions clear is to show me an example of what your table looks like prior to doing any calculations, then show me what it looks like after the calculations are done. Be very clear on what you want because saying "I want the total of the rows" and "I want the total of each row" are entirely different things. If we can get through the explanation without referring to code or computers then I think we can nail this down. For example, I might say:

I have two numbers, T1 and T2, and a table of a variable number of rows. Column one contains a row number and column two contains a dollar amount. I want to calculate column three of the table as

COL1 * T1 + T2

and column four as the sum of columns two and three

If my input numbers are

T1 = 1.05
T2 = 400

and

Table = 1  12000
        2  15000
        3  18000
        4  21000
        5  24000

I want the results to look like

Table = 1  12000  13000  25000
        2  15000  16150  31150
        3  18000  19300  37300
        4  21000  22450  43450
        5  24000  25600  49600

Hi

Sorry for the confusion.

I have T1 & T2 which are linked to listview1 when a button is clicked.

Lets say that
T1 = 100
t2 = 50

When the button is clicked the listview1 will show

Entry 1 100 50

Now lets say the user wants to enter a another set of numbers say

T1 =600
T2 = 125

the listview1 will now show

Entry 1 100 50
Entry 2 600 125

This is the data entry.

Now from those numbers i would like to show in listview2 the following

Number total1 total2 Granf total (Total1+total2)
1 (100+50)/1 (600+125)/1 (100+50)/1 + (600+125)/1
2 (100+50)/2 (600+125)/2 (100+50)/2 + (600+125)/2
3 (100+50)/3 (600+125)/3 (100+50)/3 + (600+125)/3
4 etc
5 etc
6 etc
7 etc
8 etc
9 etc
10 etc

I would like to enter as many entries as i need and the listview2 will take that into account. so if the listview1 shows 10 rows of entries then there should be 10 totals and 1 grandtotal in listview2

I hope that this helps and i greatly appreciate your time. I have been trying all sorts of combinations with no luck.

Best Regards,

Mikey.

Try this

Public Class Form1

    Private Sub btnAddNumbers_Click(sender As System.Object, e As System.EventArgs) Handles btnAddNumbers.Click

        If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
            Dim item As New ListViewItem(TextBox1.Text)
            item.SubItems.Add(TextBox2.Text)
            ListView1.Items.Add(item)
        Else
            MsgBox("please enter two numbers")
        End If

    End Sub


    Private Sub btnCalc_Click(sender As System.Object, e As System.EventArgs) Handles btnCalc.Click

        'add one column for the row number,
        '    one column for each row in listview1
        '    one column for the row total

        For i As Integer = 1 To ListView1.Items.Count + 2
            Dim header As New ColumnHeader
            header.Text = "Column" & i
            ListView2.Columns.Add(header)
        Next

        'add some rows to listview2

        For i As Integer = 1 To 10
            ListView2.Items.Add(i)
        Next

        'Do the calculations. One column will be added to ListView2 for each row in ListView1.
        'That column will be (T1 + T2)/row# where T1 and T2 are from columns 1 and 2 in ListView1.
        'The rightmost column will be the sum of all previous columns excluding the first.

        For row2 As Integer = 1 To ListView2.Items.Count

            Dim total As Double = 0.0

            For Each row1 As ListViewItem In ListView1.Items
                Dim val As Double = (CDbl(row1.SubItems(0).Text) + CDbl(row1.SubItems(1).Text)) / CDbl(row2)
                ListView2.Items(row2 - 1).SubItems.Add(val)
                total += val
            Next

            ListView2.Items(row2 - 1).SubItems.Add(total)

        Next

    End Sub

End Class

The header can display columns at their actual width. If overall width of visible columns in a header exceeds size of visible grid area, the grid displays a horizontal scrollbar that supports two modes of work. In the first mode (ColumnScrollType.Optimized) it can display the highest volume of information in columns and cells. In the second mode (ColumnScrollType.Normal), the grid scrolls columns according to thumb position and displays empty space fter the last column.

Hi Reverand Jim

That's it. Thankyou so much for helping me on this. I need to learn a lot more. Again thankyou for your time.

Many thanks mikey.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.