ListBox Count Problem

dan463va

Member
Joined
Sep 25, 2006
Messages
7
Programming Experience
Beginner
Hello all,
I am having one problem with an app i'm trying to work on for school. In the app we have a textbox, listbox and an add button. The user will type in a statement in the textbox and when the add button is pressed it will be displayed in the listbox. With each statement added the statements will be counted 1. through however many statements there are.
So far this is the code I have:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnAdd.Click

Dim Tasks As String
Dim intCount As Int16 = 1

Tasks = txtTask.Text

intCount = lstTask.Items.Count

lstTask.Items.Add(intCount & Tasks)

The two problems i have is when i debugg the counter starts at 0 not 1 and i've tried everything to fix it. Also I can't get the spacing right, like it should be:

1. Wake Up
2. Eat Breakfast
3. Go to work

can anyone give me help to get this? Thank you in advance for any help!



 
try something like this:
VB.NET:
[FONT=Tahoma] Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim intCount As Int16 = [/FONT][FONT=Tahoma]lstTask.Items.Count + [/FONT][FONT=Tahoma]1
[/FONT][FONT=Tahoma]        Dim Tasks As String[/FONT][FONT=Tahoma] = intCount.ToString & ". " & txtTask.Text

        lstTask.Items.Add([/FONT][FONT=Tahoma]Tasks[/FONT][FONT=Tahoma])
[/FONT]
End Sub

what that above code does is, it declares the Short variable (Int16 variable) and sets it to the number of items in the listbox + 1 (this is why you're getting the "the counter is always 0" problem, if there are no items in the listbox then the Count property will give you 0) then it makes declares the string variable and adds the counter variable with a period and a space and then with the contents of the textbox

an even shorter way of doing this would be:
VB.NET:
[FONT=Tahoma] Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
[/FONT][FONT=Tahoma]        lstTask.Items.Add([/FONT][FONT=Tahoma]([/FONT][FONT=Tahoma]lstTask.Items.Count + [/FONT][FONT=Tahoma]1).ToString & [/FONT][FONT=Tahoma] ". " & txtTask.Text[/FONT][FONT=Tahoma])
[/FONT]End Sub
 
Try this

I wasn't able to understand ur problem well, but still i ve concluded ur problem like this:

Private Sub btnAdd(byVal sender as object, byVal e as .....)

Dim str as String
Dim i as integer

i=lstBox.Items.Count
i+=1 'index for the next item

str= i & " " & TxtBox.text
lstBox.Items.Add(str)

End Sub

If this fixes ur problem i ll b glad, else try to describe ur problem better.
Oh! i ve named ur controls wrongly, hope u ll manage that.
 
Back
Top