DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: add numbers from listbox to a textbox

  1. #1
    golfmaltais Guest

    add numbers from listbox to a textbox


    I am trying to add numbers from a listbox
    Its working with vb6 but not with vb.net.

    That's what I tried but it's not working

    Dim sum As Integer
    Dim i As Integer

    sum = 0
    i = 0
    For i = 0 To ListBox2.Items.Count - 1
    sum = sum + ListBox2.Items(ListBox2.SelectedIndex)
    Next i
    TextBox5.Text = sum

    I would like the sum of what's inside the listbox to appear inside the textbox


    Ex: 12 + 12 + 12 from the listbox and sum would be 36 in the textbox

  2. #2
    Dave Keighan Guest

    Re: add numbers from listbox to a textbox

    Hi golfmaltais

    I don't know about Net, I'm having trouble figuring it out how it worked in
    VB6.

    Dim sum As Integer
    Dim i As Integer

    sum = 0
    i = 0
    For i = 0 To ListBox2.Items.Count - 1
    **** sum = sum + ListBox2.Items(ListBox2.SelectedIndex)
    Next i
    TextBox5.Text = sum

    The way I read the code, you're always adding the value of SetlectedIndex -
    which remains the same through the loop unless you intentionally change the
    selection in the loop.

    Shouldn't the marked line read:
    sum = sum + ListBox2.Items(i)

    --
    Dave


    --
    Dave Keighan

  3. #3
    Phil Weber Guest

    Re: add numbers from listbox to a textbox

    > That's what I tried but it's not working

    Golf: How does it "not work?" Do you get an error message? If so, what does it
    say?
    --
    Phil Weber



  4. #4
    Jason Sobell \(iGadget\) Guest

    Re: add numbers from listbox to a textbox

    Erk... VB.NET encourages type-safe code, and what you have here is a cludge
    of automatic casting of strings to numbers.
    In VB6, ListBox2.Items(ListBox2.SelectedIndex) returns a string, not a
    number, and this sort of slackness doesn't work in .NET. Look at the parse
    functions and ToString().
    I also don't quite understand what this code is supposed to do.
    ListBox2.SelectedIndex will always return the same value, so you are adding
    a number to itself once for every item in the listbox?

    Cheers,
    Jason

    "golfmaltais" <golfmaltais@hotmail.com> wrote in message
    news:3dbc4d07$1@tnews.web.devx.com...
    >
    > I am trying to add numbers from a listbox
    > Its working with vb6 but not with vb.net.
    >
    > That's what I tried but it's not working
    >
    > Dim sum As Integer
    > Dim i As Integer
    >
    > sum = 0
    > i = 0
    > For i = 0 To ListBox2.Items.Count - 1
    > sum = sum + ListBox2.Items(ListBox2.SelectedIndex)
    > Next i
    > TextBox5.Text = sum
    >
    > I would like the sum of what's inside the listbox to appear inside the

    textbox
    >
    >
    > Ex: 12 + 12 + 12 from the listbox and sum would be 36 in the textbox




  5. #5
    Dave Keighan Guest

    Re: add numbers from listbox to a textbox

    Hi Jason

    > Erk... VB.NET encourages type-safe code, and what you have here is a
    > cludge of automatic casting of strings to numbers.


    Does Net only 'encourage' strongly typed code or is it enforced?

    I thought they did away with default values or is this just encouraged as
    well? 'ListBox2.Items(i) = ListBox2.Items(i).Text'

    > I also don't quite understand what this code is supposed to do.
    > ListBox2.SelectedIndex will always return the same value, so you are
    > adding a number to itself once for every item in the listbox?


    Given the example list of 12, 12 & 12 golfmaltais should be getting the
    results he wants _if_ SelectedIndex is any ListBox2.Item so I'm guessing
    that he's either getting an error and SelectedIndex is -1 _or_ is VB.Net
    returning '' as a result of ListBox2.Items(-1) ?

    Thanx
    --
    Dave

  6. #6
    Jason Sobell \(iGadget\) Guest

    Re: add numbers from listbox to a textbox


    "Dave Keighan" <dkeighan@hotmail.com> wrote in message
    news:Xns92B553E4F8ABEkeighandhotmailcom@209.1.14.29...
    > Hi Jason
    >
    > > Erk... VB.NET encourages type-safe code, and what you have here is a
    > > cludge of automatic casting of strings to numbers.

    >
    > Does Net only 'encourage' strongly typed code or is it enforced?


    Encourages unless you have Option Strict on.

    > I thought they did away with default values or is this just encouraged as
    > well? 'ListBox2.Items(i) = ListBox2.Items(i).Text'


    The Items() property refers to an object, not necessarily a string, so if
    the object type supports .ToString and the destination accepts a string as
    its parameter then this will work.

    > > I also don't quite understand what this code is supposed to do.
    > > ListBox2.SelectedIndex will always return the same value, so you are
    > > adding a number to itself once for every item in the listbox?

    >
    > Given the example list of 12, 12 & 12 golfmaltais should be getting the
    > results he wants _if_ SelectedIndex is any ListBox2.Item so I'm guessing
    > that he's either getting an error and SelectedIndex is -1 _or_ is VB.Net
    > returning '' as a result of ListBox2.Items(-1) ?


    Yes, if he's getting a syntax error, but since the logic is all messed up I
    suppose he might be 'getting the wrong answer'? I can't work out if he
    really wants to multiply a single entry by itself 'n' times, or if he
    intends summing all the values, or summing multiple selected items. I can
    see what it does, but not why he wants it
    "Its working with vb6 but not with vb.net." suggests that he's aware of the
    SelectedIndex issues, although then again... perhaps not

    Cheers,
    Jason



  7. #7
    Dave Keighan Guest

    Re: add numbers from listbox to a textbox

    Hi Jason

    Thanks for the response, makes sense. Got the VS.Net-Pro 60 day trial CDROM
    in the mail the other day ... think I'll actually give it a spin - when I
    get a chance.

    --
    Dave

  8. #8
    Jason Sobell \(iGadget\) Guest

    Re: add numbers from listbox to a textbox

    "Dave Keighan" <dkeighan@hotmail.com> wrote in message
    news:Xns92BB5945DC50Ekeighandhotmailcom@209.1.14.29...
    > Hi Jason
    >
    > Thanks for the response, makes sense. Got the VS.Net-Pro 60 day trial

    CDROM
    > in the mail the other day ... think I'll actually give it a spin - when I
    > get a chance.


    No problem. If you are a VB6 developer I would _strongly_ recommend you get
    the Wrox book "Professional VB.NET, 2nd edition":
    http://www.wrox.com/books/1861007167.htm

    It is an excellent book for those who want to skip the "What is a variable?"
    type books and get straight down to understanding the key differences
    between VB.NET and VB6, and it covers a vast range of material with
    excellent examples. I bought about 4 books on VB.NET and this was by far
    the best.

    Cheers,
    Jason



  9. #9
    Arthur Wood Guest

    Re: add numbers from listbox to a textbox


    Dave,

    "Got the VS.Net-Pro 60 day trial CDROM" ===> you may be in for a VERY big
    surprise, as what you have received is a DVD, not a CD, and unless you have
    a DVD reader on your PC, you will NOT be able to read the disc.

    Arthur Wood





    Dave Keighan <dkeighan@hotmail.com> wrote:
    >Hi Jason
    >
    >Thanks for the response, makes sense. Got the VS.Net-Pro 60 day trial CDROM


    >in the mail the other day ... think I'll actually give it a spin - when

    I
    >get a chance.
    >
    >--
    >Dave



  10. #10
    Mike Mitchell Guest

    Re: add numbers from listbox to a textbox

    On Mon, 4 Nov 2002 08:38:08 +1100, "Jason Sobell \(iGadget\)"
    <iGadget_@hotmail.com> wrote:

    >It is an excellent book for those who want to skip the "What is a variable?"


    All variables are objects in VB.Net, aren't they? So surely the
    question should be: "What is an object?" (in those books that deal
    with the noddy stuff).

    MM

  11. #11
    Mike Mitchell Guest

    Re: add numbers from listbox to a textbox

    On 3 Nov 2002 17:52:02 -0700, "Arthur Wood" <wooda@nospam.com> wrote:

    >"Got the VS.Net-Pro 60 day trial CDROM" ===> you may be in for a VERY big
    >surprise, as what you have received is a DVD, not a CD, and unless you have
    >a DVD reader on your PC, you will NOT be able to read the disc.


    Big deal. LG DVD players are cheap cheap cheap! He could whip his
    CD-ROM drive out and stick in a DVD replacement in about half-an-hour
    for around 40 quid (60 bucks).

    MM

  12. #12
    Dave Keighan Guest

    Re: add numbers from listbox to a textbox

    Hi Arthur

    > Dave,
    >
    > "Got the VS.Net-Pro 60 day trial CDROM" ===> you may be in for a VERY
    > big surprise, as what you have received is a DVD, not a CD, and unless
    > you have a DVD reader on your PC, you will NOT be able to read the
    > disc.


    Indeed, you are correct. After reading your post I had a real good look at
    the cover (peeled back the delivery address wrapper) and sure enough the
    upper left hand corner has DVD-ROM printed there. Fortunately the lap top I
    use as a dev machine is fitted with a DVD-CDROM but I had intended to try
    it on a machine that doesn't. I actually should have expected it, my MSDN
    subscription is for DVD - but that may be irrelevant in this instance.

    Thanks for heads up. VS.Net lesson one - assume NOTHING.

    --
    Dave

  13. #13
    Dave Keighan Guest

    Re: add numbers from listbox to a textbox

    Hi Mike

    > Big deal. LG DVD players are cheap cheap cheap! He could whip his
    > CD-ROM drive out and stick in a DVD replacement in about half-an-hour
    > for around 40 quid (60 bucks).


    Horrors, upgrade to the latest tools! Surely you jest! (gd&r)

    --
    Dave

  14. #14
    Mike Mitchell Guest

    Re: add numbers from listbox to a textbox

    On 4 Nov 2002 08:17:21 -0700, Dave Keighan <dkeighan@hotmail.com>
    wrote:

    >Hi Mike
    >
    >> Big deal. LG DVD players are cheap cheap cheap! He could whip his
    >> CD-ROM drive out and stick in a DVD replacement in about half-an-hour
    >> for around 40 quid (60 bucks).

    >
    >Horrors, upgrade to the latest tools! Surely you jest! (gd&r)


    No, I never jest. I am always deadly serious, as are all the English.
    As you obviously weren't listening or couldn't find anyone to read it
    for you, I said only get what you need and no more. As, surprise
    surprise, you cannot read a DVD in a CD-ROM drive, what you need is a
    DVD drive that, by the way, can also read CD-ROMs.

    Clear enough for ya now?!!

    MM

  15. #15
    Dave Keighan Guest

    Re: add numbers from listbox to a textbox

    Hi Mike

    >> (gd&r)


    grin, duck & run.


    > No, I never jest. I am always deadly serious, as are all the
    > English.


    Yes, I can tell. In the footsteps of Mr. Bean, Monty Python, Faulty
    Titties and the likes, terrible burden what.


    > As you obviously weren't listening or couldn't find
    > anyone to read it for you, I said only get what you need and no
    > more. As, surprise surprise, you cannot read a DVD in a CD-ROM
    > drive, what you need is a DVD drive that, by the way, can also
    > read CD-ROMs.
    >
    > Clear enough for ya now?!!


    Sheesh Mike, bad day? Lighten up.

    --
    Dave

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center