[英]Parsing data from textbox into multiple textboxes - VB.NET
我希望能够将制表符分隔的数据(如下所示)粘贴到多行文本框中。
资料 :
Fat 119.3g 62.1%
Saturated Fat 26.4g
Cholesterol 442.5mg
Sodium 3,824.8mg
Carbohydrates 80.1g 18.5%
Fiber 12.1g
Sugars 26.6g
Protein 83.7g 19.4%
注意 :每行是:
当我单击一个按钮时,我希望发生以下情况:
fatInput.text = "119.3"
satfatInput.text = "26.4"
cholesteralInput.text = "442.5"
sodiumInput.text = "3824.8"
carbInput.text = "80.1"
fiberInput.text = "12.1"
sugarInput.text = "26.6"
proteinInput.text = "83.7"
因此,总而言之,它会将这些数字从我粘贴到其中的文本框中拉出,然后将其输入到单独的文本框中。 逗号“ g”和“ mg”不应包括在内。 这三个百分比无关紧要,可以忽略不计。 营养素名称,顺序和标签不会更改。
这有可能吗?
谢谢您的帮助!!!
免责声明 :不是程序员,只要具备一些有趣的程序的基本知识和思想,即可帮助我跟踪自己的饮食。 我已经完成了该程序,但是决定通过以下内容对其进行增强,因此我不必手动输入这些数字。 如果这太复杂了,我就不需要答案了,只需要一个方向就可以了。 我不知道从哪里开始!
TextBox具有Lines属性,使您可以访问输入的文本行。
然后,您可以使用String.Split方法进一步分割行
Dim parts() As String = line.Split(ControlChars.Tab)
VB具有方便的Val函数,该函数获取字符串中包含的数字。 但它不能处理逗号,因此在获取数字之前,请使用“ 替换”功能将其删除
Dim d As Double = Val(parts(1).Replace(",", ""))
然后,您可以使用“ 向右”功能通过查看最右边的两个字符来获取单位,并查看其是否为“ mg”,如果没有,请检查最右边的字符以查看其是否为“ g”。
感谢Olivier使我的大脑以正确的顺序思考:
可能令人讨厌,但这是我使用的代码:
Dim msgStr As String
Dim newStr As String = ""
Dim tempArray() As String
tempArray = pasteInput.Lines
msgStr = (tempArray(0))
For Each value As Char In msgStr
If value >= "0" And value <= "9" Or value = "." Then
newStr &= value
ElseIf value = "g" Or value = "mg" Then
fatInput.Text = newStr
newStr = ""
Exit For
End If
Next
msgStr = (tempArray(1))
For Each value As Char In msgStr
If value >= "0" And value <= "9" Or value = "." Then
newStr &= value
ElseIf value = "g" Or value = "mg" Then
satfatInput.Text = newStr
newStr = ""
Exit For
End If
Next
msgStr = (tempArray(2))
For Each value As Char In msgStr
If value >= "0" And value <= "9" Or value = "." Then
newStr &= value
ElseIf value = "g" Or value = "mg" Then
cholInput.Text = newStr
newStr = ""
Exit For
End If
Next
msgStr = (tempArray(3))
For Each value As Char In msgStr
If value >= "0" And value <= "9" Or value = "." Then
newStr &= value
ElseIf value = "g" Or value = "mg" Then
sodiumInput.Text = newStr
newStr = ""
Exit For
End If
Next
msgStr = (tempArray(4))
For Each value As Char In msgStr
If value >= "0" And value <= "9" Or value = "." Then
newStr &= value
ElseIf value = "g" Or value = "mg" Then
carbInput.Text = newStr
newStr = ""
Exit For
End If
Next
msgStr = (tempArray(5))
For Each value As Char In msgStr
If value >= "0" And value <= "9" Or value = "." Then
newStr &= value
ElseIf value = "g" Or value = "mg" Then
fiberInput.Text = newStr
newStr = ""
Exit For
End If
Next
msgStr = (tempArray(6))
msgStr = Replace(msgStr, "Sugars", "")
For Each value As Char In msgStr
If value >= "0" And value <= "9" Or value = "." Then
newStr &= value
ElseIf value = "g" Or value = "mg" Then
sugarInput.Text = newStr
newStr = ""
Exit For
End If
Next
msgStr = (tempArray(7))
For Each value As Char In msgStr
If value >= "0" And value <= "9" Or value = "." Then
newStr &= value
ElseIf value = "g" Or value = "mg" Then
proteinInput.Text = newStr
newStr = ""
Exit For
End If
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.