How to append lines of text into a Richtextbox at specific location/index? - vb.net

I have a lua script file in which I would like to add some new lines at specific locations.
So let's say we have the following text as an example:
line 1
line 2
line 3
line 8
line 9
line 10
I would like to insert some new lines after line 3 and also a few additional lines before line 8
So far I've tried to index the lines but didn't found a way to use those indexes in order to write new lines of text.
For i As Integer = 0 To textbox.Lines.Count - 1
Dim x As Integer = i + 1
Dim y As Integer = i - 1
If textbox.Lines(i).Contains("line 3") Then
textbox.Lines(x).Append("Line 4")
End If
Next

In case someone else is facing the same obstacle,I found out that the required result is obtained by using the Input/Output library and threading library Imports System.IO,Imports System.Threading, combined with the following for loop:
For i As Integer = 0 To textbox.Lines.Length - 1
Dim s As String = textbox.Lines(i)
Dim index As Integer = s.IndexOf("line 3")
If index > -1 Then
Dim length As Integer = s.Length - index
index += textbox.GetFirstCharIndexFromLine(i)
textbox.Select(index + length, 1)
Thread.Sleep(1)
SendKeys.SendWait("{ENTER}")
textbox.Text = textbox.Text.Insert(textbox.GetFirstCharIndexOfCurrentLine,
"line 4" & vbCrLf &
"line 5 " & vbCrLf &
"line 6" & vbCrLf &
"line 7" & vbCrLf)
End If
Next

Related

two textbox intersect each line vbcrlf

Textbox1.Lines =
2
4
11
13
19
21
Textbox2.Lines =
3
5
14
17
26
29
In output (Textbox3.text) I want the following issue:
2 3
4 5
11 14
13 17
19 26
21 29
so how do I get this to work?
Dim linesx() As String = TxtStringNumP1.Lines
Dim linesy() As String = TxtStringNumP2.Lines
For x As Integer = 1 To linesx.Length - 1
For y As Integer = 1 To linesx.Length - 1
Dim strWords1 = linesx(y).Split(",")
Dim strWords2 = linesy(y).Split(",")
TextBoxO1.Text &= Val(strWords1(0)) + vbCrLf + Val(strWords2(0) & vbNewLine)
Next
Next
Remove the vbCrLf as you don't want a new line between the first value & the second. Then, you also don't want a second loop, otherwise you are looping through the values multiple times. You also don't need to split the values by commas if they aren't going to contain commas
Finally, the value needs to be appended to the text already in TextBox01 otherwise its value is just being replaced. The TextBox01.Clear() ensures that it is empty to begin with.
TextBox01.Clear()
Dim linesx() As String = TxtStringNumP1.Lines
Dim linesy() As String = TxtStringNumP2.Lines
For x As Integer = 0 To linesx.Length - 1
Dim strWords1 As String = linesx(x)
Dim strWords2 As String = linesy(x)
TextBoxO1.Text = TextBoxO1.Text & strWords1 & " " & strWords2 & vbCrLf
Next
The above code assumes both text boxes will have the same amount of lines. If this isn't the case, you will need to check that a line exists before trying to assign it to one of your strWords variables. You will also need to loop to the larger of the 2 Lines.Length

word vba - remove manually typed list number

I have lot of documents with list number typed manually, so my intent is to remove those manual list number and the tab or space following it.
e.g
1. Text 1
1.1 Text 2
1.1.1 Text 3
1.1.1.1 Text 4
become
Text 1
Text 2
Text 3
Text 4
I'm not sure how to do this with vba and I'd really appreciate your help.
I have figured it out by using below code:
Sub RemoveManualListNumber()
Dim strInitialPar As String
Dim intSpace As Integer, intTab As Integer
Dim strFinalPar As String
Dim iPar
For iPar = 1 To ActiveDocument.Paragraphs.Count
strInitialPar = ActiveDocument.Paragraphs(iPar).Range.Text
intSpace = InStr(1, strInitialPar, " ")
intTab = InStr(1, strInitialPar, Chr(9))
Debug.Print "Paragraph " & iPar & ": " & "Index space = " & intSpace & ", Index tab = " & intTab
If intTab > 0 And intTab < intSpace Then
strFinalPar = Right(strInitialPar, Len(strInitialPar) - intTab)
Else
strFinalPar = Right(strInitialPar, Len(strInitialPar) - intSpace)
End If
ActiveDocument.Paragraphs(iPar).Range.Text = strFinalPar
Next iPar
End sub

VB - Index outside of bounds?

Oh my god i hate this thing, i tried millions of ways but couldn't find a working one. Let me explain:
I'm testing each line and checking the first word to be "copy" alright ? After the word copy i want to see if the next word is "1" , the third is "<" and the last is ">" , if all these conditions are fullfilled then the text between "<" and ">" needs to be stored in the variable "copy1" (even if there is more than 1 word between them).
What my code is:
For i = 0 To lstCode.Items.Count - 1
Dim str As String = lstCode.Items.Item(i)
Dim strA() As String = Split(str)
Dim copy1 as string
Dim copy2 as string
Select Case strA(0)
Case copy
If strA(1) = "1" And strA(2) = "<" And strA(strA.Count - 1) = ">" Then
copy1 = ""
For lr As Integer = 3 To strA.Count - 2
copy1 &= (strA(lr) & " ")
Next
End if
End select
And, when i debug it i get the error: Index was outside the bounds of the array ... Does anybody have any idea ?
There is something important i forgot to add, this is the whole code:
Case "copy"
If strA(1) = "1" And strA(2) = "<" And strA(strA.Count - 1) = ">" Then
copy1 = ""
For lr As Integer = 3 To strA.Count - 2
copy1 &= (strA(lr) & " ")
Next
ElseIf strA(1) = "2" And strA(2) = "<" And strA(strA.Count - 1) = ">" Then
copy2 = ""
For lrs As Integer = 3 To strA.Count - 2
copy2 &= (strA(lrs) & " ")
Next
ElseIf strA(1) = "run" Then
Try
IO.File.Copy(copy1, copy2)
Catch ex As IO.IOException
End Try
End If
End Select
So everything works like a charm: copy 1 < c:\csb.log > , copy 2 < c:\blabla.txt > but when the " copy run " statement comes in it gives me the error...
You need to change the operator And with AndAlso.
The second one applies Short Circuit Evaluation to your expression, meaning if the first expression is false the second, third and so on expressions on the same line are not evaluated.
In your line
If strA(1) = "1" And strA(2) = "<" And .......
when the value is "Run" you still evaluate the expression strA(2) = "<" but there is no element at index 2 so you get the error.

adding vertical text each item in text file writeline with some text

I am populating a listbox with some text & saving the output to textfile (sObj.txt)
'Saving items of lb1 in a file under C:\temp
Dim i As Integer
W = New IO.StreamWriter("C:\temp\sObj.txt")
For i = 0 To lb1.Items.Count - 1
W.WriteLine(lb1.Items.Item(i))
Next
W.Close()
This text file contains 3 (for example) entries, let's say abc in 1st line, def in 2nd line & ghi in the 3rd line.
Now I want to append another text file (MPadd.txt) using sObj.txt entries such that I get something like the following:
'Appending listbox items to the file MPadd.txt
Using SW As New IO.StreamWriter("c:\temp\MPadd.txt", True)
SW.WriteLine("some text" & abc & "some text")
SW.WriteLine("some text" & def & "some text")
SW.WriteLine("some text" & ghi & "some text")
End Using
Please help in getting it correctly. thanks.
Just read all the lines from the first file (just three lines so it is not a problem) and then loop over these lines adding prefix and postfix text as you like
EDIT
Following your last example
Dim commands() =
{
"cdhdef -t ftpv2 -c r -f {0} -x ",
"cdhdsdef -v CPUSRG {0} ",
"cacls K:\AES\data\Cdh\ftp\{0}\Archive /E /G OSSUSER:C"
}
Dim counter As Integer = 0
Dim objLines = File.ReadAllLines("C:\temp\sObj.txt")
Using SW As New IO.StreamWriter("c:\temp\MPadd.txt", True)
' Loop only for the number of strings in commands (max 3 now)
for x = 0 to commands.Length - 1
line = objeLines(x).Trim
' This check will prevent empty lines to be used for the output
If line <> string.Empty Then
SW.WriteLine(string.Format(commands(counter), line))
counter += 1
End If
Next
End Using
This example use composite formatting where you define a format string and a progressive placeholder where you want to insert another value.
Of course this will work only if you have just 3 lines in your input file

Textbox Hell - Delete 3 Lines, Skip 1, Repeat

I have a textbox that reads like so:
Line 1
Line 2
Line 3
**Line 4**
Line 1
Line 2
Line 3
**Line 4**
(repeats...)
How can I use VB to loop through the textbox, deleting Lines 1, 2, and 3, skipping the fourth, and repeat? Or, rather, record every fourth line into a new textarea?
I'd probably get the contents, split on the newline character to create an array of strings (one string per line), then loop through the array outputting only the ones i wanted.
If this is VB6 then bear in mind that the variable length String type is a reference type meaning that operations will involve taking a deep copy i.e. concatenation is expensive.
Dim lines() As String
lines = VBA.Split(TextBox1.Text, vbCrLf)
Dim counter As Long
For counter = 3 To UBound(lines) Step 4
lines(counter) = Chr$(22)
Next
TextBox1.Text = _
Replace$( _
Replace$( _
VBA.Join(lines, vbCrLf), _
vbCrLf & Chr$(22), vbNullString), _
Chr$(22) & vbCrLf, vbNullString, 1)
This is code for the previous answer.
Private Function EveryFourthLine(ByVal input As String) As String
Dim newtxt As String = ""
Dim oldtxt As String() = input.Split(vbCrLf)
For i As Integer = 1 To oldtxt.Count
If i Mod 4 = 0 Then
newtxt = newtxt & oldtxt(i - 1)
If i <> oldtxt.Count Then
'add a vbcrlf to all but the last line
newtxt = newtxt & vbCrLf
End If
End If
Next
Return newtxt
End Function
If this is VB.Net and you are using a Textbox - you don't need to split anything yourself. You can just access the .Lines property. You'll get back an array of strings
You certainly can loop through the rows, like others have shown; but another approach is to use LINQ to do that work for you.
txtBox1.Lines = (From curLine In txtBox1.Lines _
Where Array.IndexOf(txtBox1.Lines, curLine) Mod 4 = 3).ToArray
What you are saying here is that you want the Lines in the textbox to be equal to all of the lines that are already in the text box - as long as the index of that particular line, divided by 4 has a remainder of three.
That sounds complicated when you type it out like that, but really, all it's going to do is give you every fourth line, and set that back into the textbox.