hi every one
in my vb.net program I need to keep track if the buttom had been clicked befor or not or how many times it had beed clicked. I cant find the property
can u help me please.
thanks in advanced

Dani AI

Generated

There is no built-in "times clicked" property. You need to persist a counter yourself, and the right place depends on whether this is WinForms or ASP.NET. Given the tag and 's question, here is the ASP.NET Web Forms approach. As hinted, you can persist per-user, per-page state in ViewState. Increment the count only in the button's Click handler (not in Page_Load), otherwise any postback from any control will inflate the count.

' Code-behind (VB.NET), per-user per-page counter
Protected Property Button1Clicks As Integer
    Get
        If ViewState("Button1Clicks") Is Nothing Then Return 0
        Return CInt(ViewState("Button1Clicks"))
    End Get
    Set(value As Integer)
        ViewState("Button1Clicks") = value
    End Set
End Property

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Button1Clicks += 1
    lblCount.Text = "Clicked " & Button1Clicks.ToString() & " times."
End Sub

Notes:

  • ViewState persists across postbacks for that page and user. It resets on a fresh navigation, and if ViewState is disabled you will need another store.
  • If you want the count to follow the user across pages, use Session instead:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim count As Integer = If(Session("btn1"), 0)
    count += 1
    Session("btn1") = count
    lblCount.Text = "Clicked " & count.ToString() & " times."
End Sub
  • For cross-user/global counts, do not rely on a Shared/static variable; app recycles will reset it and concurrency can be wrong. Use a database or a single-row table and increment atomically. For WinForms (desktop), a simple form-level Integer field incremented in the click handler is sufficient.

Recommended Answers

All 3 Replies

try ,using viewstate or cookies to find the number of hits for a particular button in page_load()..
i.e, declare an global variable
and on button_click function ....

Is it a Vb Winform Windows app or a ASP.NET Web app using VB.NET?

'Try this

Dim intBtnClkCount as Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

intBtnClkCount+= 1

MessageBox.Show("Button1 has been clicked" & intBtnClkCount.ToString() & " times.")
End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.