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
@Shark_1, [qty is the number of times you click the button/ ordered]
heres the pic of my form
Edited 1 Year Ago by jez9
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.
Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...
Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...
Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click Dim txtB1 As New TextBox Dim i For i = ...