vb.net 如何在 Visual Basic 中制作计数器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28334783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a counter in Visual Basic
提问by ChrisHoward
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.
所以我不想计算在文本字段中输入了多少个字母/数字但是有多少次输入字段本身,我想只是让我的“计算”按钮点击计数,但这仍然允许一些一个只需单击按钮,它就会+1 到柜台,而该字段中没有任何内容。所以我想知道是否有办法做到这一点,您必须在字段中输入文本才能添加到计数器中。
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.
一切正常,但当我点击计算时,NumGroupsBox 将只显示 0,直到我学会如何让计数器功能工作。
Can some one please help advise me on how I can go about doing this?
有人可以帮助我建议我如何去做吗?
回答by laylarenee
WebForms applications can use the TextChangedevent handler for TextBoxcontrols to detect when the value has been modified. Each time the field is changed, the web browser does a PostBackto the web server and the "changed count" can be calculated. Here is an ultra basic example:
WebForms 应用程序可以使用控件的TextChanged事件处理程序TextBox来检测值何时被修改。每次更改字段时,Web 浏览器都会PostBack对 Web 服务器执行一次操作,并可以计算出“更改计数”。这是一个超基本的例子:
ASPX page:
ASPX 页面:
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" />
times.
ASPX.VB page:
ASPX.VB 页面:
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
回答by Nadeem_MK
If I am not mistaken, you want to keep the counter running as long as the application is running, right?
One of the simplest way to do it, is to create a textbox on your form (which you can Hide if needed by setting the Visibleproperty to 'False'), let's call it txtCounter.
如果我没记错的话,只要应用程序在运行,您就希望计数器一直运行,对吗?最简单的方法之一是在表单上创建一个文本框(如果需要,可以通过将Visible属性设置为 'False'来隐藏它),我们称之为txtCounter.
On initialisation of your form (FormShown event), you will initialise the counter (to zero most probably) as `txtcounter.Text = 0. Then comes your Click event, which you can maintain as you are already doing;
在初始化表单(FormShown 事件)时,您将计数器(最有可能为零)初始化为 `txtcounter.Text = 0。然后是您的 Click 事件,您可以像已经在做的那样维护它;
Private Sub CalculateButton_Click(sender As System.Object, e As System.EventArgs) Handles CalculateButton.Click
' Increment counter
Counter = CInt(Counter.text) + 1
Answer = Minutes * TIME_RATE
TotalChargeBox.Text = Answer.ToString("N")
NumGroupsBox.Text= Counter
AvgAns = Answer / Counter
AvggroupBox.Text = AvgAns.ToSting("N")
' Re-assign to textbox
Counter.text = Counter.ToString
This should do for you.
这应该对你有用。

