VB parsing strings and placing into text boxes
I have a task I am trying to complete, but cant think how to approach it.
Here is the example:
I have a text file with preferences in it. I read the file and find text on a particular line like this:
If InStr(sLine, "avidDirectory") Then
This is my line in the text file:
avidDirectory "S:\Avid MediaFiles\" "D:\Avid MediaFiles\" "Z:\Avid MediaFiles\"
What I need to do is read each string between the quoations marks and place each one in a text box.
I have 5 texts 开发者_Go百科boxes to use if there are 5 different directories above (only three in the example above)
So I guess I need to capture the text between the quotation marks, create a new string from it, and place that string into a text box
ie; string 1 = textbox1.txt etc
How would I approach this?
Thanks.
Create a new Windows application and add a button and five text boxes to the form you will be started with and replace the code of the form with this code
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim txt As String = "avidDirectory ""S:\Avid MediaFiles\"" ""D:\Avid MediaFiles\"" ""Z:\Avid MediaFiles\"""
Dim insideAQuotation As Boolean = False
Dim array(5) As String
Dim currentString As Integer = 0
For i = 1 To Len(txt)
If Mid(txt, i, 1) = Chr(34) And insideAQuotation Then
insideAQuotation = False
currentString += 1
ElseIf Mid(txt, i, 1) = Chr(34) And insideAQuotation = False Then
insideAQuotation = True
End If
If insideAQuotation Then
If Mid(txt, i, 1) <> Chr(34) Then 'This is to avoid the quotation marks inside the text boxes.
array(currentString) &= Mid(txt, i, 1)
End If
End If
Next
Me.TextBox1.Text = array(0)
Me.TextBox2.Text = array(1)
Me.TextBox3.Text = array(2)
Me.TextBox4.Text = array(3)
Me.TextBox5.Text = array(4)
End Sub
End Class
精彩评论