Solved

ListView Tally Dollars

Posted on 2011-09-25
13
276 Views
Last Modified: 2012-06-21
I would like to automate this so it will calculate once the list is generated. I don't want the user to have to click on anything, I found this but can't seem to tweak it to be automatic.

Dim indexes As ListView.SelectedIndexCollection = _
        Me.ListView1.SelectedIndices
    Dim index As Integer
    Dim price As Double = 0.0
    For Each index In indexes
        price += Double.Parse(Me.ListView1.Items(index).SubItems(1).Text)
    Next

    ' Output the price to TextBox1.
    TextBox1.Text = CType(price, String)

Open in new window

0
Comment
Question by:Jack_Jones
  • 8
  • 5
13 Comments
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36598047
Anyone Alive?
0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36598645
You could put a timer on your form that will fire when anything in the listview index changes.
Perhaps in a click event or in your item click or itemcheck events.

put about 100 on the timer, disable it when it starts and run your code above in the timer.

0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36600528
would I just copy and paste that into a timer, right now it generates 0 in the textbox
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36600578
The more I look at the solution I gave the more I realize I had not  had coffee when I typed it.
A timer would be useless (unless) it ran on it's own and ran constantly which could cause a drain on you.

If you could put the above code in an event of your listview that fired when a value changed then you would be fine.  Otherwise - if there are no events that would help you like the ones I mentioned then a timer may work instead.

0
 
LVL 14

Accepted Solution

by:
Brook Braswell earned 500 total points
ID: 36600584
Would help if I put the code with this :) - need more coffee
' NO TIMER
Private sub ListView_ItemClick()
    Dim indexes As ListView.SelectedIndexCollection = Me.ListView1.SelectedIndices
    Dim index As Integer
    Dim price As Double = 0.0
    For Each index In indexes
        price += Double.Parse(Me.ListView1.Items(index).SubItems(1).Text)
    Next

    ' Output the price to TextBox1.
    TextBox1.Text = CType(price, String)

End Sub

' WITH TIMER
' set timer INTERVAL property to something you can tolerate where 1000 = 1 second
Private Sub tmrCalcME_Timer()
    
    Dim indexes As ListView.SelectedIndexCollection = Me.ListView1.SelectedIndices
    Dim index As Integer
    Dim price As Double = 0.0
    For Each index In indexes
        price += Double.Parse(Me.ListView1.Items(index).SubItems(1).Text)
    Next

    ' Output the price to TextBox1.
    TextBox1.Text = CType(price, String)
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36600832
For some reason it just don't generate anything into the textbox, I have the correct sub and listview entered. Just weird, is it possible to just open a file read it all and then calculate the values from it, that would be easier for me lol.
0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36600894
Perhaps the event is not working - did you try the timer method ?
make sure the timer is enabled and an interval entered
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36601006
well got my cup of joe kicked in, got it to load the timer but only gives a value of 0 as a result, the subitem should be 4 according to the colums that I set in ListView2 box.
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36601058
So did some tweaking and got it to generate a dollar amount, however I don't want the user to have to click anything to generate the info, is there a small code I can put to tell vb to auto select the first row of data?


The following code works to generate the info, but wanted it to be automated so user don't have to click.
 Dim indexes As ListView.SelectedIndexCollection = Me.ListView2.SelectedIndices
        Dim index As Integer
        Dim price As Double = 0.0
        For Each index In indexes
            price += Double.Parse(Me.ListView2.Items(index:=4).Text)
        Next

        ' Output the price to TextBox1.
        vehrtlprice.Text = CType(price, String)

Open in new window

0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36601653
Have you tried the timer ?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36601741
Yep the timer works, but only when you click an item in the ListView2 control, just want it to auto generate without the user having to click on the listview item.
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602107
Finally got it figured out, will give you props for the timer idea that I didn't even thing of.

ListView2.Items(0).Selected = True
        ListView2.Select()

Open in new window

0
 
LVL 1

Author Closing Comment

by:Jack_Jones
ID: 36602112
The key was having a timer, and then i finally found out how to auto select it.


ListView2.Items(0).Selected = True
        ListView2.Select()

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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