I am loading a large textfile into a variable and into a invisible textbox.
I have values loaded from this text box into other textboxes. I wait it so if someone edits something in one of the boxes to change it in the invisible textbox and/or the variable.
I want to know I can search line by line for certain text in either the textbox or variable.

Howto with textbox and variable
TechedRonan
This should be a better piece of code for the search. It uses the lines collection and see if it contains a string. This will find the first Item. You could continue iterating through all of them to find all lines with the match in, which would be a simple change.
Private Sub BtnSearchtextboxToDetermineLine(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 1
Dim bfound As Boolean = False
For Each s As String In TextBox1.Lines
If s.Contains(TxtSearch.Text) Then
bfound = True
Exit For
End If
i = i + 1
Next
If bfound = True Then
MsgBox("Found On Line" & i.ToString)
Else
MsgBox("Not Found")
End If
End Sub
CaptainSmudge
Like this is what I have to search a text file line by line:
Dim VStream As New IO.FileStream(openFileDialog1.FileName, IO.FileMode.Open, IO.FileAccess.Read)
Dim VReader As New IO.StreamReader(VStream)
Dim VLine As String = VReader.ReadLine
'Loop until the "Config settings" string is found
Do Until VLine.Contains("Config settings = ")
VLine = VReader.ReadLine
Loop
If VLine.Contains("Config settings = ") Then
Dim GetName() As String = VLine.Split("=")
Dim GotName As String = GetGet(1)
Dim Split1() As String = GotNAme.Split(" ")
Dim Split2 As String = Split1(1)
Split2 = Split2.Replace(Chr(34), "")
ChainCombo.Items.Add(Split2)
End If
Is there something like this I can do for a textbox.text
Praveen.Yarlagadda
This example is assuming your textbox multiline property is set to True. It's also done in VB 2003, so there may be a simpler way in 2005.
For index As Integer = 0 To TextBox1.Lines.GetUpperBound(0)
If InStr(UCase(TextBox1.Lines(index)), UCase("Config settings = ")) Then
Dim GetName() As String = TextBox1.Lines(index).Split("=")MsgBox(Replace(Trim(GetName(1)), Chr(34), ""))
End If NextAiran
Danny Tsai
Loading the textfile into a variable
Dim StrContent as string = My.Computer.Filesystem.ReadAllText("Test.txt")
Loading this into a textbox
Textbox1.Text = strContent
To update another textbox when something in a textbox changes use the TextChnaged Event
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
TextBox1.Text = TextBox2.Text
End Sub
If the contents of this textbox is constructed from the other textboxes then you might have a function that builds a string, something like the following
Public Class Form1
Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged
BuildTextBox()
End Sub
Sub BuildTextBox()
Dim s As String
s = TextBox2.Text & vbCrLf & TextBox3.Text & vbCrLf & TextBox4.Text
TextBox1.Text = s
End Sub
End Class
Searching a string/textbox for a string, simply use the instr method
An example
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 1
For Each s As String In TextBox1.Text.Split(vbCrLf)
If s.Replace(vbLf, "") = TextBox3.Text Then
Exit For
End If
i = i + 1
Next
MsgBox("Found On Line" & i.ToString)
End Sub
john82
A snippet from Section 1 looks like this:
Config settings = "Bob"
Company Name = "LNS"
State Location = "GA"
A snippet from Section 2 looks like this:
Config Settings = "John"
Company Name = "PLS"
State Location = "VA"
I already coded where I pull all the "Config Settings" and add each one to a dropdown box. Then as I select the drop down for "Bob" I built code that will grab each line after "Bob" and fill in the proper text box for that setting the stops wantes it reaches the last line.
So when I select "Bob" textbox1 (CompanyName) will have LNS filled in and textbox2 (StateLocation) will have GA filled in.
What I want to be able to do is if I edit some of the settings for "Bob" and click save it will only edit the lines for "Bob" in the config file (Which is all text and the format can not be changed)
I thought this would be easiest to load the entire config file into an invisible textbox or into a variable and find the lines and edit them there then save the entire config file back to the file on my drive.
Is this possible
LoveCode
it is possible, try what spotty suggested but I would also consider using Xml/datatables perhaps for easy managability and the added performance :-)
I would also suggest looking at xml serialization/deserialization in this situation, perhaps alot easier to manage and simple!
Onlinemercenary
Trev72
Dim i As Integer = 0
For Each s As String In FileStream.Lines
If s.Contains("Config Settings = " & ConfigSettings.Text) Then
For Each d As String In FileStream.Lines
If d.Contains("Company Name=" & SectionsCombo.Text) Then
Dim GetName() As String = d.Split(",")
Dim GotName As String = GetName(1)
Dim Split1() As String = GotName.Split("Chr(34)")
Dim Split2 As String = Split1(0)
Replace(d, CompanyName.Text, "Company Name=" & SectionsCombo.Text)
End If
i = i + 1
Next
Exit For
End If
i = i + 1
Next
It is probably ovious that I don't know how to do this or what I am doing in this area but basically this is what I am trying to do.
I select a name in the "ConfigSettings" dropdown box and it loads (done with code for the dropdown box) all the settings for the name selected in the dropdown into a couple text boxes. Then as text is typed in the text boxes I loop (Using "For Each") through each line in the invisible FileStream box until I find the first line containing ""Config Settings = " & ConfigSettings.Text" (The selected name in the ConfigSettings drop down) then I do another loop (Using "For Each") to find the the first textbox "CompanyName" and replace the line with whatever is in the box weather it be the same or not. I do this for each box until I reach the last setting for the name selected (done by using a if statement)
I cant figure out how to replace the line found using "For Each" with the text entered into the text boxes. Thanx for any help in advance.
Andrew Mercer
to me, what you are doing is VERY ineffecient.
however if you need to search line by line for certain text, you can go through the collection, which contains your large textfile data.
for each currentElement as String in yourCollection
if currentElement.Trim().ToLower().Equals(Input.Trim().ToLower()) then
'Do stuff
end if
next
if you need to directly access your textfile and read it line by line and compare your value:
Dim theFileStream as new FileStream(PathToFile, FileMode.Open)
Dim theStreamReader as new StreamReader(theFileStream)
if theStreamReader.Peek() > -1 then
for each currentLine as String in theStreamReader.ReadLine()
if currentLine.Trim().ToLower().Equals(Input.Trim().ToLower()) then
'do stuff
end if
next
end if
theFileStream.Close()
theStreamReader.Close()
best practice would really be to redesign your current system, if its some database type of system you have, you can serialize and deserialize the data/collection to xml, or perhaps loading/modifying/saving data to Dataset and querying your dataset for values for a match, like a database system but in memory
hope this helps!