简体   繁体   English

将数据从文本框解析为多个文本框-VB.NET

[英]Parsing data from textbox into multiple textboxes - VB.NET

I want to be able to paste the tab-delimited data (shown below) into a multi-line text box. 我希望能够将制表符分隔的数据(如下所示)粘贴到多行文本框中。

Data : 资料

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%

NOTE : Each row is: [nutrient name][tab][g/mg amount][tab][% (only on 3)] 注意 :每行是: [营养素名称] [标签] [g / mg量] [标签] [%(仅适用于3)]

When I click a button, I would like the following to happen: 当我单击一个按钮时,我希望发生以下情况:

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"

So to summarize, it pulls those numbers out of the textbox I pasted it into and enters them into the separate textboxes. 因此,总而言之,它会将这些数字从我粘贴到其中的文本框中拉出,然后将其输入到单独的文本框中。 Commas, "g" and "mg" should not be included. 逗号“ g”和“ mg”不应包括在内。 The three percentages are not relevant and may be ignored. 这三个百分比无关紧要,可以忽略不计。 The nutrient names, order, and tabs do not change. 营养素名称,顺序和标签不会更改。

Is this even possible?! 这有可能吗?

THANKS FOR THE HELP!!! 谢谢您的帮助!!!

Disclaimer : Not a programmer, just have basic knowledge and thought of a fun program to make to help me track what I eat. 免责声明 :不是程序员,只要具备一些有趣的程序的基本知识和思想,即可帮助我跟踪自己的饮食。 I've completed the program but decided to enhance it with the below so I don't have to manually type in these numbers. 我已经完成了该程序,但是决定通过以下内容对其进行增强,因此我不必手动输入这些数字。 If this is too complicated I don't need the answer, just some direction and I can figure it out. 如果这太复杂了,我就不需要答案了,只需要一个方向就可以了。 I have no clue where to start! 我不知道从哪里开始!

The TextBox has a Lines property giving you access to the text lines entered. TextBox具有Lines属性,使您可以访问输入的文本行。

Then you can split the line further with the String.Split method 然后,您可以使用String.Split方法进一步分割行

Dim parts() As String = line.Split(ControlChars.Tab)

VB has handy Val function that gets a number contained in a string. VB具有方便的Val函数,该函数获取字符串中包含的数字。 but it does not handle the comma, so remove it using the Replace function before getting the number 但它不能处理逗号,因此在获取数字之前,请使用“ 替换”功能将其删除

Dim d As Double = Val(parts(1).Replace(",", ""))

Then you can get the unit by looking at the two right most characters using the Right function and see whether it is "mg", if not check the right most character to see if it is a "g". 然后,您可以使用“ 向右”功能通过查看最右边的两个字符来获取单位,并查看其是否为“ mg”,如果没有,请检查最右边的字符以查看其是否为“ g”。

Thanks to Olivier for getting my brain thinking in the right order: 感谢Olivier使我的大脑以正确的顺序思考:

  1. use Lines property to put it an an array 使用Lines属性将其放入数组
  2. grab a single line from the array 从数组中抓一行
  3. grab only numbers from the line (I used an if/then statement since Val kept returning "0") 仅从行中抓取数字(我使用了if / then语句,因为Val一直返回“ 0”)
  4. cut it off at "g" or "mg" (which was easy since I already had an if/then statement) 切成“ g”或“ mg”(这很容易,因为我已经有了一个if / then语句)
  5. repeat for each of the 8 lines 对8行中的每行重复

Probably nasty, but here's the code I used: 可能令人讨厌,但这是我使用的代码:

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM