-
Sep 28th, 2021, 06:45 AM
#1
Thread Starter
New Member
[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
-
Sep 28th, 2021, 06:53 AM
#2
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 28th, 2021, 07:01 AM
#3
Thread Starter
New Member
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
-
Sep 28th, 2021, 07:04 AM
#4
Re: help listbox
I edited my answer after i first posted it...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 28th, 2021, 07:06 AM
#5
Thread Starter
New Member
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 )
-
Sep 28th, 2021, 07:10 AM
#6
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 28th, 2021, 07:12 AM
#7
Thread Starter
New Member
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
-
Sep 28th, 2021, 07:15 AM
#8
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 28th, 2021, 07:22 AM
#9
Thread Starter
New Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|