VB.NET - How to delete multiple consecutive lines in a text file? - vb.net

I am making a customer management system for a school project and I need to be able to delete customers.
Each customer takes up 8 lines in the text file.
I have got it to delete one line using:
Dim lines() As String
Dim outputlines As New List(Of String)
Dim searchstring1 As String = lblName.Text
lines = IO.File.ReadAllLines("Customers.text")
For Each line As String In lines
If line.Contains(searchstring1) = False Then
outputlines.Add(line)
FileClose(1)
System.IO.File.Delete("Customers.text")
IO.File.WriteAllLines("Customers.text", outputlines)
FileClose(1)
End If
Next
But I'm not sure how to repeat this another 7 times, any ideas?

Instead of the for-each-loop, I would use a classical for-next, with a step of 8 - If we can be sure that each dataset consists of 8 lines, and the searchstring is found in the first:
...
For LineNo As Integer=0 to lines.count()-1 step 8
If lines(LineNo).Contains(searchstring1) = False
...
End If
Next lineNo
Additionally, you should close the input file directly after reading before going into the for-loop, and you should probably write the outputlines only after the for-next-loop is done, otherwise you will remove all customers after the searched one.
Next step is to check for errors which may occur during file operations...

If your file has mixed format and cannot use Step 8 because it not contains only N-8-rows of data, you can use a counter.
Set it to 8 when find customer and decrement it each loop.
The creation of new line in output file will be only when counter = 0.
Dim countMatchingLines As Integer = 0
For Each line As String In lines
If line.Contains(searchstring1) Then
countMatchingLines = 8
End If
If countMatchingLines = 0 Then
outputlines.Add(line)
FileClose(1)
System.IO.File.Delete("Customers.text")
IO.File.WriteAllLines("Customers.text", outputlines)
FileClose(1)
Else
countMatchingLines -= 1
End If
Next
Another solution, is just skip next 8 lines by manipulation for-var-counter:
For i as Integer = 0 to lines.Count() - 1
If line.Contains(searchstring1) Then
i = Math.Min(i + 8, lines.Count()-1)
Else
outputlines.Add(line)
FileClose(1)
System.IO.File.Delete("Customers.text")
IO.File.WriteAllLines("Customers.text", outputlines)
FileClose(1)
End If
Next
It will avoid necessary iterates and Ifs..

Try This...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = RemoveParagraphOfText("GARBAGE RECORD", Line)
End Sub
Public Function RemoveParagraphOfText(ByVal textTobeRemoved As String, ByVal Message As String) As String
RemoveParagraphOfText = Message.Replace(textTobeRemoved, "")
End Function

Related

Serial Communication stops working when loop is started

I have problem with serial data communication if I started a loop it is not updating the data from my weighing scale. I can't figure out how to continue the communication as well as to run the loop. The logic of my code will be in the loop so I could check the value from my integer and compare it to the data from serial data (Weighing Scale)
Private Sub conWeight_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles conWeight.DataReceived
receivedText(conWeight.ReadExisting())
End Sub
Private Sub receivedText(ByVal [text] As String)
If Me.lblWeight.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf receivedText)
Me.Invoke(x, New Object() {(text)})
Else
Dim reverseString As String = [text]
Dim revString As String = StrReverse(reverseString)
Dim clean As String
clean = revString.Replace("=", "")
Me.lblWeight.Text = clean 'append text
End If
End Sub
'this is part with loop
If binWeight = 0 Then
targetweight = CInt(txtbSilo1.Text) + CInt(txtbSilo2.Text) + CInt(txtbSilo3.Text) + CInt(txtbSilo4.Text)
If CInt(txtbSilo1.Text) > 0 Then
currentWeight = CInt(txtbSilo1.Text)
frmAutomaticControl.conControl.Open()
frmAutomaticControl.conControl.Write("motr1")
frmAutomaticControl.conControl.Close()
MsgBox("check")
Do
If binWeight >= currentWeight Then
frmAutomaticControl.conControl.Open()
frmAutomaticControl.conControl.Write("moto1")
frmAutomaticControl.conControl.Close()
Exit Do
End If
Loop
Else
End If
BunifuFlatButton1.Enabled = True
Else
MsgBox("Empty The Bin")
End If
just a couple of ideas.
1. Throw that part of the code into a background worker.
2. Cheese it and throw in an application.doevents.
3. Create a global variable that'll capture the output of each iteration of your loop that'll then feed it where it needs to go.
Just one line of code is all you need. In your loop put this above everything else.
Application.DoEvents()

vb.net Line numbering in multi line Rich Text Box if line starts with specific condition

Let me start by saying that I'm new to any language of coding besides G-code, and I've researched this until my fingers hurt. I've actually been working on this project for a little over a year now on my own, and this is the first hurdle I haven't been able to find my way around.
I'm creating an editor for cnc G-code, and i'm trying to add a Re-number function to it. I'm using a multi line richtextbox to display the the G-code to the user. I'm trying to edit each line of code that starts with the character "N", and if a line doesn't start with that character then it's left alone.
I figured the best way to do this would be to loop thru the RTB and pass each line into an array. Then I could use an If statement to see if a cell in the array started with the char "N" or in my case "blockLetter". Then use the replace function to correct the line Number.
This is what I have so far.
Dim increment As Integer = txtLNIncrement.Text
Dim blockLetter As String = txtLNStartTxt.Text
Dim count As Integer = 0
Dim block As Integer = count + increment
For Each cell As String In frmNC.NcTextBox.Lines
If cell.StartsWith(blockLetter) Then
Dim newCell As String = cell.Replace(blockLetter, block)
block = block + increment
MessageBox.Show(newCell)
End If
Next
Example of G-code that needs to be renumbered:
N50 M01
N60 T0101 (TOOL NAME)
N70 M41
N80 G96 S350
N90 M03
N100 M08
This is what I want:
N10 M01
N20 T0101 (TOOL NAME)
N30 M41
N40 G96 S350
N50 M03
N60 M08
This is what I'm getting when I run the code above:
1050 M01
2060 T0101 (TOOL NAME)
3070 M41
4080 G96 S350
5090 M03
60100 M08
I believe my issue is that the cell.replace is splitting each cell at the "N" character, and dropping it all together. Thus adding what I want to see in front of the existing numbers, minus the "N" character. How can I overwrite the existing block number to the correct ascending block number, and retain the "N" character? Am I going about this in the correct way, or is there a better way? Any help is greatly appreciated.
Try something like this out:
Private increment As Integer = 10
Private blockLetter As String = "N"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim newLine As String
Dim values() As String
Dim lineNumber As Integer = 0
Dim lines As New List(Of String)(NcTextBox.Lines)
For i As Integer = 0 To lines.Count - 1
If lines(i).TrimStart().StartsWith(blockLetter) Then
values = lines(i).TrimStart(" " & blockLetter.ToCharArray).Split(" ")
lineNumber = lineNumber + increment
values(0) = lineNumber
newLine = blockLetter & String.Join(" ", values)
lines(i) = newLine
End If
Next
NcTextBox.Lines = lines.ToArray
End Sub
it's very simple:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim blockLetter As String = "N"
Dim increment As Integer = 1
For i As Integer = 0 To RichTextBox1.Lines.Length - 1 Step 1
Dim fullLine As String = RichTextBox1.Lines(i)
If fullLine.StartsWith(blockLetter) Then
Dim numbering As String = fullLine.Remove(RichTextBox1.Lines(i).IndexOf(" "))
Dim block As Integer = numbering.Substring(1)
Dim newCell As String = blockLetter & block + increment
MessageBox.Show(newCell)
End If
Next
End Sub
Result:
The Label1.Text will increase with button click.
It's all about Substring() starting from index 1 after 'N', so you will get the number.
Good luck with your coding!

Loop through contents in the text file

New TechGuy on this site. Thought i'd make this resourceful since im getting ready to graduate and want more programming practice. I have a question:
I tried looping through a text file I created and I wanted to replace each multiple of 3 with the word "changed". I created a text file and entered numbers 1-15 on each line. My code is below but for some reason it would only change the number 3 and 13. I tried using this link as a resource (Loop through the lines of a text file in VB.NET) but that wasnt to helpful. Anyway here is my code, can someone help with what i'm doing wrong?
Public Class numbers
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim OpenFilePrompt As New OpenFileDialog
openFilePrompt.ShowDialog()
Dim currentReader As New System.IO.StreamReader(OpenFilePrompt.FileName)
txtInput.Text = currentReader.ReadToEnd
Dim a As String
Dim numbers As New List(Of String)
a = txtInput.Text
numbers.Add(a)
Dim b As Integer
For i = 0 To numbers.Count Step 3
b = b + 3
TextBox2.Text = (numbers.Item(i).Replace(b.ToString, "Changed"))
Next
End Sub
End Class
You are setting numbers (the first item) to a, which is txtInput.Text.
Then you have a one size list which is completely useless!
You should use Mod instead.
Divides two numbers and returns only the remainder.
So just check if Integer Mod 3 equals 0.
Also consider using File.ReadAllLines(String), Val(String), String.Join(String, String()) and File.WriteAllLines(String, String()).
Dim Content() As String = File.ReadAllLines(OpenFilePrompt.FileName)
For Index As Integer = 0 To Content.Length - 1
Dim Number As Integer = Val(Content(Index))
If Number Mod 3 = 0 Then
Content(Index) = "changed"
End If
Next
txtInput.Text = String.Join(vbCrLf, Content)
File.WriteAllLines(OpenFilePrompt.FileName, Content)

VB "Index was out of range, must be non-negative and less than the size of the collection." When trying to generate a random number more than once

So I'm trying to generate a random number on button click. Now this number needs to be between two numbers that are inside my text file with various other things all separated by the "|" symbol. The number is then put into the text of a textbox which is being created after i run the form. I can get everything to work perfectly once, but as soon as i try to generate a different random number it gives me the error: "Index was out of range, must be non-negative and less than the size of the collection." Here is the main code as well as the block that generates the textbox after loading the form. As well as the contents of my text file.
Private Sub generate()
Dim newrandom As New Random
Try
Using sr As New StreamReader(itemfile) 'Create a stream reader object for the file
'While we have lines to read in
Do Until sr.EndOfStream
Dim line As String
line = sr.ReadLine() 'Read a line out one at a time
Dim tmp()
tmp = Split(line, "|")
rows(lineNum).buybutton.Text = tmp(1)
rows(lineNum).buyprice.Text = newrandom.Next(tmp(2), tmp(3)) 'Generate the random number between two values
rows(lineNum).amount.Text = tmp(4)
rows(lineNum).sellprice.Text = tmp(5)
rows(lineNum).sellbutton.Text = tmp(1)
lineNum += 1
If sr.EndOfStream = True Then
sr.Close()
End If
Loop
End Using
Catch x As Exception ' Report any errors in reading the line of code
Dim errMsg As String = "Problems: " & x.Message
MsgBox(errMsg)
End Try
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rows = New List(Of duplicate)
For dupnum = 0 To 11
'There are about 5 more of these above this one but they all have set values, this is the only troublesome one
Dim buyprice As System.Windows.Forms.TextBox
buyprice = New System.Windows.Forms.TextBox
buyprice.Width = textbox1.Width
buyprice.Height = textbox1.Height
buyprice.Left = textbox1.Left
buyprice.Top = textbox1.Top + 30 * dupnum
buyprice.Name = "buypricetxt" + Str(dupnum)
Me.Controls.Add(buyprice)
pair = New itemrow
pair.sellbutton = sellbutton
pair.amount = amounttxt
pair.sellprice = sellpricetxt
pair.buybutton = buybutton
pair.buyprice = buypricetxt
rows.Add(pair)
next
end sub
'textfile contents
0|Iron Sword|10|30|0|0
1|Steel Sword|20|40|0|0
2|Iron Shield|15|35|0|0
3|Steel Shield|30|50|0|0
4|Bread|5|10|0|0
5|Cloak|15|30|0|0
6|Tent|40|80|0|0
7|Leather Armour|50|70|0|0
8|Horse|100|200|0|0
9|Saddle|50|75|0|0
10|Opium|200|500|0|0
11|House|1000|5000|0|0
Not sure what else to add, if you know whats wrong please help :/ thanks
Add the following two lines to the start of generate():
Private Sub generate()
Dim lineNum
lineNum = 0
This ensures that you don't point to a value of lineNum outside of the collection.
I usually consider it a good idea to add
Option Explicit
to my code - it forces me to declare my variables, and then I think about their initialization more carefully. It helps me consider their scope, too.
Try this little modification.
I took your original Sub and changed a little bit take a try and let us know if it solve the issue
Private Sub generate()
Dim line As String
Dim lineNum As Integer = 0
Dim rn As New Random(Now.Millisecond)
Try
Using sr As New StreamReader(_path) 'Create a stream reader object for the file
'While we have lines to read in
While sr.Peek > 0
line = sr.ReadLine() 'Read a line out one at a time
If Not String.IsNullOrEmpty(line) And Not String.IsNullOrWhiteSpace(line) Then
Dim tmp()
tmp = Split(line, "|")
rows(lineNum).buybutton.Text = tmp(1)
rows(lineNum).buyprice.Text = rn.Next(CInt(tmp(2)), CInt(tmp(3))) 'Generate the random number between two values
rows(lineNum).amount.Text = tmp(4)
rows(lineNum).sellprice.Text = tmp(5)
rows(lineNum).sellbutton.Text = tmp(1)
lineNum += 1
End If
End While
End Using
Catch x As Exception ' Report any errors in reading the line of code
Dim errMsg As String = "Problems: " & x.Message
MsgBox(errMsg)
End Try
End Sub

How to remove vb.net Richtextbox lines that not contains specific text?

I use the next code to remove lines from Richtextboxes but that way i can only tell what line to remove. I need to remove all lines that not contains specific text, can this be done with some edits of my code?
1st piece:
Private Property lineToBeRemovedlineToBeRemoved As Integer
2nd piece:
Dim lineToBeRemoved As Integer = 0
lineToBeRemovedlineToBeRemoved = lineToBeRemoved - 0
Dim str As String = RichTextBox1.Lines(lineToBeRemoved)
RichTextBox1.Find(str & vbCr)
RichTextBox1.SelectedText = ""
This code will remove any line from a richtextbox RichTextbox1 that does not contain "Test" on it. Remember to add Imports System.Text.RegularExpressions to the top of your code.
Private Sub RemoveLines()
Dim lines As New List(Of String)
lines = RichTextBox1.Lines.ToList
Dim FilterText = "Test"
For i As Integer = lines.Count - 1 To 0 Step -1
If Not Regex.IsMatch(lines(i), FilterText) Then
lines.RemoveAt(i)
End If
Next
RichTextBox1.Lines = lines.ToArray
End Sub
You code is not close. You should start over. Use a for loop to go through the RichTextBox lines. If the text is not in a line, then delete it. Tip: It may be easier to go from the last line to the first to avoid problems when deleting.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RTB.Select(RTB.GetFirstCharIndexFromLine(2), RTB.Lines(2).Count)
RTB.SelectionLength = RTB.Lines(2).Length + 1
RTB.SelectedText = ""
End Sub
Try this code, I applied to my prog and it work good.
When use, just ... call dels("unwanted") ==> Line which contain unwanted word will disappear.
Private Sub dels(sw As String)
Dim ud As String = "" 'for keep all we need
Dim cn As Integer = 0 'for avoid empty line
For Each line As String In RichTextBox1.Lines 'for every line in reichtextbox
If Len(line) > 5 Then 'if that line got more than 5 character
If InStr(line.ToLower, sw.ToLower) < 1 Then 'transform them to lower case for better resulted
If cn = 1 Then ud = ud + vbCrLf 'not place new-line if it is first
ud = ud + line 'keep this line if not match ne want delete
cn = 1 'turn-off first line signal
End If
End If
Next
RichTextBox1.Clear() 'empty richtextbox
RichTextBox1.AppendText(ud) 'update richtextbox with the unwanted
End Sub