使用Visual Basic在For Next循环中添加数字 [英] Adding Numbers in a For Next Loop using Visual Basic

查看:138
本文介绍了使用Visual Basic在For Next循环中添加数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个文本框,可以输入数字。



按下回车键后输入数字。



没有限制可以添加很多数字,因为我使用的是柜台。



我不确定如何使用For Next Loop添加这些数字,然后答案显示在消息框中。



我是Visual Basic的新手,不知道如何在代码中执行此操作,非常感谢任何帮助。

< br $>


到目前为止我所拥有的:



I have a textbox on a form in which numbers can be entered.

Once the enter key is pressed the number is entered.

There is no limit to however many numbers can be added, as I am using a counter.

I am unsure how to add these numbers using a For Next Loop, which then the answer is displayed in a message box.

I'm fairly new to Visual Basic and do not know how to do this in code and would really appreciate any help.


What I have so far:

Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        If Total.Text = "" Then
            MsgBox("Enter a valid number")
        Else
           	Counter+= 1
		Sum = 0 

For i = 1 To Counter
                Total =
                lblTotal.Text = Sum 
            Next i

            If Counter = 30 Then
                MsgBox("Maximum Data entered, Reset Data then enter additional data.")
            End If
        End If

    End Sub

推荐答案

第一个答案

您尝试过什么?

你被困在哪里?



你有没有将问题分成几部分?



A)您知道如何为编辑框准备事件处理程序吗?你需要生成一个事件处理程序来捕获该编辑框上的返回键,然后对其内容作出反应。



B)你是否限制了只在该编辑框中输入数字?请参阅此链接 [ ^ ]。



C)你知道如何创建一个列表,有任何添加方法来捕获数字并允许它增长? (请参阅此链接 [ ^ ])。



D)你知道如何创建一个消息框吗?请参阅此链接 [ ^ ]。



E)关于for循环......好吧,请参阅链接 [ ^ ]。



全部你的问题可以通过尝试将问题理解为小部件然后一点一点地实现它们来解决。不要试图一次完成任何事情。



你应该更多地解释一下for循环的最后一部分...并在一个结果中显示结果消息框...



我无法理解为什么你需要一个循环输入值... GUI将处理你按下的每次返回你按回车并处理了一个好的条目你应该能够在同一个回车键处理程序中增加计数器。



然后我的问题是:你的结果是什么想要表演?什么时候应用这个结果?

如果在每次输入键后按下你想要显示一个消息框,那么你不需要任何循环。



第二个答案

要将字符串转换为整数,您可以使用:

FIRST ANSWER
What have you tried?
Where are you stuck?

Have you divided the problem in parts?

A) do you know how to prepare an event handler for your edit box? you will need to generate an event handler to catch the return key on that edit box and then react to the content of it.

B) have you limited the possibility to enter only numbers into that edit box? see this link[^].

C) do you know how to create a list that have any add method to capture the numbers and allow it to grow? (see this link[^]).

D) do you know how to create a message box? See this link[^].

E) about the for loop... well, see this link[^].

All your questions can be solved by trying to understand the problem as small parts and then implement them little by little. Don't try to make everything at once.

You should definitely explain a little bit more the last part about the for loop... and showing the result in a message box...

I can't understand why you need a loop to enter the values... the GUI will handle as many returns you press and each time you press return and a good entry has been processed you should be able to increase the counter inside the same enter key handler.

Then my question is: which result do you want to show? and when has this result to be applied?
If after each enter key press you want to show a message box then you don't need any loop here.

SECOND ANSWER
To convert a string to an integer you can use this:
Dim str1 As String = Nothing
Dim int1 As Integer = Nothing

str1 = "10"

int1 = Convert.ToInt32(str1)





第三回答

鉴于您的代码,您已经多次欺骗过所有人......:叹息:



看到你在你的问题中说:一旦按下回车键按键事件,现在你说如果你点击按钮就可以了。



你没有提供代码,当你完成代码时,代码告诉我不同​​于你的东西问题...



好​​吧,我会这样做:



THIRD ANSWER
Given your code you've lied to everyone several times... :sigh:

See that you said in your question that: "Once the enter key is pressed" keypress event, now you say that you do that on the event of a button click.

You've not presented code and when you've done it the code is telling me different things than you question...

Well, I would do this:

Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
  ' in order to make this work your textbox name has to replace "TextBoxValue"
  if (IsNumeric(TextBoxValue.Text))
  then
    lblTotal.Text = Convert.ToString(Convert.ToInt32(lblTotal.Text) + Convert.ToInt32(TextBoxValue.Text))
  end if
End Sub



改善提问方式,获得更好的结果。由于你的不好问题,在完成我所做的所有步骤之前,我不可能回答我在最后所说的内容。看到我花了一些时间在互联网上搜索链接(你可以找到它),最后和很多评论之后我给你一个完全不同的解决方案。

我说这一切都是因为在CP中你可以找到许多愿意帮助的人,但这是一个很好的问题的明显例子。

通常这类问题直接被遗忘到期低票。

下次再做得更好。



查看代码。试着理解它。如果你有疑虑,那就回来吧。 ;)



PS:我没有编译它,我从未使用VB.NET,但这是一件非常容易实现的事情。



祝你好运。


Improve the way you make questions and you get a better result. It was impossible to answer what I've put here at the end before making all the steps I've made due to your bad question. See that I've spent time searching the Internet to find links (which you could have found), and that at the end and after a lot of comments I've given you a completely different solution.
I'm saying all this because here in CP you can find lot's of people willing to help, but this has been a clear example of a bad question.
Typically this kind of questions go directly to oblivion due to low votes.
Do it better the next time.

See the code. Try to understand it. If you have doubts then come back. ;)

PS: I've not compiled it, I've never worked with VB.NET, but this is a really easy thing to achieve.

Good luck.


获取一个全局变量并初始化为零。然后,当输入值时,只需将该值添加到该全局值。最后在MessageBox中显示结果。
Take a global variable and intialize with zero. Then when a value is entering just add that value to that global value. Finally show that result in the MessageBox.


这篇关于使用Visual Basic在For Next循环中添加数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆