hi, I want to Count how many times i click a button and display the number in listview.
but i dont know the syntax for counting a button, can someone help me with this problem?
If you intend to do it with .Net then you would need to post back the button click to the server side code. There you would instantiate a class variable or session variable that held the current count and on each postback when the button was clicked you would increment the variable by.
To display it you just pass the current count variable to whatever control you want to use to display the result, a listview in your case.
@hericles, sorry bu i really dont understand what youre tryin to say :( im only a biggener in vb.net
Declare an integer at the class level as in
Private ButtonClickCount As Integer = 0
Then in the button click event handler do
ButtonClickCount += 1 Me.Text = "Current count = " & ButtonClickCount
This will display the count in the title bar. Replace the last line to copy the value to a ListView.
If you try to hold the number, how many times you clicked a button, then Reverend Jim's technic is the perfect one.
But, if you want to show the numbers as an item in a listview like a serial no, simply add the ListView.Items.Count() + 1 as a item of the listview control.
Dim litm As New ListViewItem litm = ListView1.Items.Add((ListView1.Items.Count() + 1).ToString)
@Shark_1, @Reverend Jim, Thank you, its working now. my only problem now is 1 just have a 1 product but instead of increasing the qty of it , it just duplicating.. heres my code:
Private Sub clickMe(sendr As Object, ByVal e As EventArgs) Dim btn As Button Dim qty As Integer = ((lvorder.Items.Count() + 1).ToString) btn = CType(sendr, Button) Dim item As ListViewItem With lvorder On Error Resume Next item = lvorder.Items.Add(btn.Name) item.SubItems.Add(qty) item.SubItems.Add(btn.Tag) lblstotal.Text = "0.00" End With End Sub
What do you mean by "Qty" ? Is this the ordered/stock quantity?
(lvorder.Items.Count() + 1).ToString
You can use it to generate serial no, but not for quantity.
Take a textbox to input the ordered quantity and the add it to the listview.
Do not understand, why do you use With lvorder at line no 7. No need to use it.
More consizely the codes should be
Private Sub clickMe(sender As Object, ByVal e As EventArgs) Dim btn As Button = CType(sender, Button) Dim item As ListViewItem item = lvorder.Items.Add(btn.Name) item.SubItems.Add((lvorder.Items.Count() + 1).ToString) item.SubItems.Add(btn.Tag) lblstotal.Text = "0.00" End Sub
Dim count As Integer
As global variable then under the buttonClick event handler at the very beginning of it add this.
count = count +1 lblDisplay.Text = count
That will keep on adding the number as you click the button starting from 0 upwards.
NOTE when you terminate the program and start again the count will also start afresh to keep the count going don't close the program.
Hope that answer your question.
As you say "qty" is the order number then you can use lvorder.Items.Count() + 1).ToString.
No, she didn't write qty is order number but she wrote "qty is the number of times...."
Since your original questions has been answered by Jim you should close this thread as solved and open a new one with the new problem you are facing.
@ All of us
Since a lot of us are not native Englis speakers please write your questions as clearly as possible and also read questions and answer carefully. Otherwise threads will get endlessly long and confusing. Thanks
This is simple to count how many times you clicked each individual button. To do this you will have to make a loop through the listview item count. It should be
Private Sub clickMe(sender As Object, ByVal e As EventArgs) Dim btn As Button = CType(sender, Button) Dim ProdMatch As Boolean = False For i As Integer = 0 To lvorder.Items.Count() - 1 If lvorder.Items(i).Text = btn.Text Then 'change the click value lvorder.Items(i).SubItems(1).Text = (Val(lvorder.Items(i).SubItems(1).Text) + 1).ToString ProdMatch = True Exit For End If Next If Not ProdMatch Then 'Add the item at first time Dim item As ListViewItem item = lvorder.Items.Add(btn.Name) item.SubItems.Add("1") item.SubItems.Add(btn.Tag.ToString) 'lblstotal.Text = "0.00" End If End Sub
Hope it can solve your click counting problem.
@Minimalist, Im sorry about that. i just carried away by my question :) ,
thank you for helping me guys :D
@Shark_1, i will try your code, thank you for helping me again.