Vb.net – How to make a counter in Visual Basic


So I want to count not how many letters/numbers are entered into a text field
But how many times something has been entered into the field itself, I was thinking of just making my "Calculate" button a click count but that would still allow some one to just click the button and it would +1 to the counter with nothing in the field. So I was wondering if there was a way to do it that you would have to input text into the field to add to the counter.

Here is what I have so far:

Private Sub CalculateButton_Click(sender As System.Object, e As System.EventArgs) Handles CalculateButton.Click
    Answer = Minutes * TIME_RATE
    TotalChargeBox.Text = Answer.ToString("N")
    NumGroupsBox.Text= Counter.ToString("N")
    AvgAns = Answer / Counter
    AvggroupBox.Text = AvgAns.ToSting("N")

Everything works but when I click calculate the NumGroupsBox will only display 0
at the moment til I learn how to get the counter function to work.

Can some one please help advise me on how I can go about doing this?

Best Solution

WebForms applications can use the TextChanged event handler for TextBox controls to detect when the value has been modified. Each time the field is changed, the web browser does a PostBack to the web server and the "changed count" can be calculated. Here is an ultra basic example:

ASPX page:

Enter value:
<asp:TextBox ID="TrackedField" runat="server" AutoPostBack="true" />
<hr />
This field has been changed
<asp:Label ID="TimesChanged" runat="server" Text="Enter value" />

ASPX.VB page:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If ViewState("TimesChanged") Is Nothing Then ViewState("TimesChanged") = 0
End Sub

Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
    TimesChanged.Text = DirectCast(ViewState("TimesChanged"), Integer).ToString()
End Sub

Protected Sub TrackedField_TextChanged(sender As Object, e As EventArgs) Handles TrackedField.TextChanged
    ViewState("TimesChanged") = DirectCast(ViewState("TimesChanged"), Integer) + 1
End Sub
Related Question