Results 1 to 9 of 9

Thread: [RESOLVED] help listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    13

    Resolved [RESOLVED] help listbox

    hi, i'm new here. i started studying vb about 1 week and a half ago. i have a problem with the listbox: every time i add a name to the list , in a label/textbox i would like to display the count of the total names i insert in another text box.
    example: textbox(A)( insert MARK)---> save in listbox MARK
    textbox(B) = 1
    textbox(A)( insert john)---> save in listbox JOHN
    textbox(B) = 2( mark+john) etc...
    the problem is that it duplicates my A list box insertions.
    My book doesn't offer much help I am studying on.
    could you please tell me where i am wrong .
    ps.I know for many it could be a trifle..but for me it's not so simple... i understand italian and russian, sorry for my not good engl


    this is the code...


    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click

    Dim sum As Integer

    list1.Items.Add(t1.Text)
    sum = 0

    sum = sum + list1.Items.Add(t1.Text)
    lbl1.Text = ("sum of items " & sum)


    If t1.Text = "" Then
    MessageBox.Show("insert !")
    End If
    End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: help listbox

    This doesn't do what you think it does... This line is why you're getting duplicates.

    Code:
    sum = sum + list1.Items.Add(t1.Text)
    To get the count of the listbox items... ("sum of items" is incorrect, it's "count of items"

    Code:
    lbl1.Text = "sum of items " & list1.Items.Count.ToString
    Last edited by .paul.; Sep 28th, 2021 at 06:58 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    13

    Re: help listbox

    hi paul, good afternoon.the real problem is that of the duplication of the name entered in A then saved in the list box remains, I add in A:Mark and in the listbox gives me MARK, MARK. the count, however, does it right: 2 aha

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: help listbox

    I edited my answer after i first posted it...

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    13

    Re: help listbox

    sum = sum + list1.Items.Add(t1.Text)
    how come it doesn't do what I think it does?
    (sorry for the trivial question, if I understand at least then I do not repeat the error )

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: help listbox

    list1.Items.Add(t1.Text)

    It adds t1.Text to your ListBox a second time. Remove that line and the sum code

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    13

    Re: help listbox

    si yes yes then I did, I realized that I had to remove sum, thank you very much!!! I have a lot to learn

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: help listbox

    ListBox.Items.Add returns the ListBox index of the newly added item, which is not what you wanted. Calling Add twice, adds t1.Text twice...

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    13

    Re: help listbox

    ah okay , thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width