且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在同一文本框VB 2010中分隔和添加两个值

更新时间:2022-05-01 16:51:00

您好,



首先,您需要正确标记问题以识别您正在使用的技术。



正如您在问题中所述,它是VB项目,因此您可以使用文本框的Key Up事件来获取价值和处理。希望你知道如何编写代码来添加9 + 9并在下一个文本框中设置。



祝你好运
Hi,

First of all, you need to tag question properly to identify technology you are using.

As you stated in your question, it''s VB project so you can use Key Up event of textbox to get value and process on it. hope you have idea how to write code to add 9+9 and set in the next text box.

best luck


因为这听起来很像你的作业,我不会给你任何代码!



但这并不困难:



1)创建一个total变量来保存结果。将它设置为零。

2)循环输入字符串中的每个字符 - For Each循环将非常容易地完成。

2.1)检查字符是否为数字即''0''到''9''

2.2)如果是,请将其转换为数值,然后将其添加到总数中

2.3)如果它不是,要么忽略它,要么报告问题并退出方法 - 你的选择。

3)循环之后,将第二个文本框的Text属性设置为total,使用ToString方法。
Since this sounds a LOT like you homework, I won''t give you any code!

But it isn''t difficult at all:

1) Create a "total" variable to hold the result. Set it to zero.
2) Loop though each character in the input string - a For Each loop will do that very easily.
2.1) Check if the character is a digit i.e. ''0'' to ''9''
2.2) If it is, convert it to a numeric value, and add it to your total
2.3) If it isn''t, either ignore it, or report the problem and exit the method - your choice.
3) After the loop, set the Text property of teh second text box to the total, using the ToString method.


Dim i As Integer = 0
For Each digit In (TextBox1.Text)
           i += Val(digit)
       Next
       TextBox2.Text = i.ToString