将行分隔到文本框(vb 2008)

时间:2014-01-06 21:53:26

标签: vb.net

enter image description here

我想这样设置

  • 在第一个文本框中,我想要第一行
  • 在第二个文本框中我想要第二行

等等...

        prezime.Text = "Vukmirovic" 'I want this to show in textbox
        ime.Text = ""
        jmbg.Text = ""
        pol.Text = ""
        daniz.Text = ""
        meseciz.Text = ""
        godinaiz.Text = ""
        danr.Text = ""
        mesecr.Text = ""
        godinar.Text = ""
        danvd.Text = ""
        mesecvd.Text = ""
        godinavd.Text = ""
        regbr.Text = ""
        brojtel.Text = ""
        adresa.Text = ""
        grad.Text = ""
        opstina.Text = "" <code>

好的,我写下了代码,以便Prezime:被替换为什么。我导入了

System.Text.RegularExpressions 

现在我只需设置为prezime.text设置第一行,为ime.text设置第二行,然后设置一个,帮助:)

RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, "Prezime: ", "") 

3 个答案:

答案 0 :(得分:2)

假设您使用的是.NET

     '...
     Dim i As Integer = 0
     Dim myFile As String = "C:\Temp\infile.txt"
     'it would be good to add a try/catch here in case you get an io error
     Dim lines() As String = System.IO.File.ReadAllLines(myFile)
     prezime.Text = splitter(lines(i))
     ime.Text = splitter(lines(++i))
     'do the same for other text boxes 
     'you need to code the lines in the same order as input file values
     lastRow.Text = splitter(lines(++i))
     '...

 Private Shared Function splitter(ByVal parmLine As String) As String
     'split the passed string into 2 srings using the : as a separator 
     Dim words() As String = parmLine.Split(":")
     'test to see you have 2 words returned. If not, error
     If (words.Length < 2) Then
         Return "Error reading from file - Expected 2 tokens spearated by : but found 1"
     End If
     'you don't care about the first word it is the field name. Return the 2nd
     Return words(1).Trim
 End Function

答案 1 :(得分:0)

假设您使用文本框来保存文件的内容。

Public Function whichtxt(ByVal line As Integer) As TextBox
    If line = 0 Then
        Return TextBox2
    ElseIf line = 1 Then
        Return TextBox3
'And so on... 
    Else
        Return Nothing
    End If
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    For i As Integer = 0 To TextBox1.Lines.Length - 1
        Dim s As String = TextBox1.Lines(i)
        whichtxt(i).Text = s
    Next

答案 2 :(得分:0)

首先,我们将编写一个函数来解析每行所需输入的实际部分:

Private Function GetDataFromLine(line As String) As String
    'Find everything before and including ': ' and remove it
    Return Regex.Replace(line, ".*: ", String.Empty)
End Function

注意:调用静态正则表达式方法与创建正则表达式实例有一些性能损失,有关详细信息,请参阅此answer

然后我们将读取文件中的所有行并将它们放在适当的字段中:

Dim lines = File.ReadAllLines("myfile.txt")
prezime.Text = GetDataFromLine(lines(0))
ime.Text = GetDataFromLine(lines(1))
jmbg.Text = GetDataFromLine(lines(2))
'...

解决此问题的另一种可能方法是将输入数据映射到如下所示的字典:

"Prezime" => "Vukmirovic"
"Ime" => "Nemanja"
...

然后执行以下操作:

prezime.Text = inputDictionary("Prezime")

您必须编写一个将输入文件映射到字典的函数。