Solved

Trying to SUM listbox items, but getting "Conversion from type 'ListItem' to type 'String' is not valid". Need someone to check code.

Posted on 2013-05-20
3
518 Views
Last Modified: 2013-05-21
Hello,
can someone please advise what I'm doing wrong. I have try many solutions, but just cannot get out from error like:
"Conversion from type 'ListItem' to type 'String' is not valid", or
Conversion from type 'ListItem' to type 'Double' is not valid, or
Conversion from type 'ListItem' to type 'Integer' is not valid, etc

Dim result As Double = Math.Round((TextBoxA.Text + TextBoxB.Text, 1)
        MsgBox(result)
        Dim Pcs As String = result * TextBoxC.Text
        MsgBox(Pcs)
        ListBox1.Items.Add(Pcs)
        Dim sum As Double
        For Each item As String In ListBox1.Items
            sum += Double.Parse(item)
        Next

Open in new window


Thanks in advance for hlep!
0
Comment
Question by:dejandejanovic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39182638
Dim result As Double = Math.Round((TextBoxA.Text + TextBoxB.Text, 1)
        MsgBox(result)
        Dim Pcs As String = result * TextBoxC.Text
        MsgBox(Pcs)
        ListBox1.Items.Add(Pcs)
        Dim sum As Double
        For Each item As Object In ListBox1.Items
            sum += Double.TryParse(item.ToString())
        Next

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39182646
better yet with TryParse to avoid exception:
Dim result As Double = Math.Round((TextBoxA.Text + TextBoxB.Text, 1)
        MsgBox(result)
        Dim Pcs As String = result * TextBoxC.Text
        MsgBox(Pcs)
        ListBox1.Items.Add(Pcs)
        Dim sum As Double
        Dim number As Double
        For Each item As Object In ListBox1.Items
            if (Double.TryParse(item.ToString(), number)) then
                sum += number
        else
                'parsing failed
        end if
        Next

Open in new window

0
 

Author Closing Comment

by:dejandejanovic
ID: 39183887
Works perfect! Thanks.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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