我正在寻找一种简单明了的方法:
示例:
if textbox5.text = "1234" then textbox6.text= "10" (1+2+3+4)
If TextBox5.TextLength = 1 Then
TextBox6.Text = TextBox5.Text.Substring(0, 1)
ElseIf TextBox5.TextLength = 2 Then
TextBox6.Text = Val(TextBox5.Text.Substring(0, 1)) + Val(TextBox5.Text.Substring(1, 1))
ElseIf TextBox5.TextLength = 3 Then
TextBox6.Text = Val(TextBox5.Text.Substring(0, 1)) + Val(TextBox5.Text.Substring(1, 1)) + Val(TextBox5.Text.Substring(2, 1))
ElseIf TextBox5.TextLength = 4 Then
TextBox6.Text = Val(TextBox5.Text.Substring(0, 1)) + Val(TextBox5.Text.Substring(1, 1)) + Val(TextBox5.Text.Substring(2, 1)) +
Val(TextBox5.Text.Substring(3, 1))
ElseIf TextBox5.TextLength = 5 Then
TextBox6.Text = Val(TextBox5.Text.Substring(0, 1)) + Val(TextBox5.Text.Substring(1, 1)) + Val(TextBox5.Text.Substring(2, 1)) +
Val(TextBox5.Text.Substring(3, 1)) + Val(TextBox5.Text.Substring(4, 1))
ElseIf TextBox5.TextLength = 6 Then
TextBox6.Text = Val(TextBox5.Text.Substring(0, 1)) + Val(TextBox5.Text.Substring(1, 1)) + Val(TextBox5.Text.Substring(2, 1)) +
Val(TextBox5.Text.Substring(3, 1)) + Val(TextBox5.Text.Substring(4, 1)) + Val(TextBox5.Text.Substring(5, 1))
答案 0 :(得分:1)
你想要做的是抓住字符串中的每个字符,并将其转换为数字并加上它,而不是特定的if / elses用于不同的字符串长度。
Dim i as integer
For Each c As Char in TextBox5.Text
i= i + Integer.Parse(c)
Next
TextBox6.Text = i
我习惯使用C#,因此语法可能不准确,但这通常就是你要找的。 p>