two textbox intersect each line vbcrlf - vb.net

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

Related

Concatenate lines into one string

I need to split and edit text from string to a certain formula.
The text String is:
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
And I need it to be:
1;2;3;4;5
<br>
1;2;3;4;5
<br>
1;2;3;4;5
<br>
1;2;3;4;5
<br>
1;2;3;4;5
<br>
1;2;3;4;5
<br>
In 1 string i have like 50 of those I need it to be lines of that formula.
Ive tried to use split by new lines and adding chars to every word and then split by char but it doesnt helped me.
Assuming that the line breaks are given as a CR/LF sequence
Dim result = input.Replace(vbCrLf, ";")
or
Dim result = input.Replace(Environment.NewLine, ";")
See: https://dotnetfiddle.net/pB08ay
Your updated more complex input string can be processed like this
Dim result = input.Replace(Environment.NewLine, ";").
Replace(";;", Environment.NewLine & "<br>" & Environment.NewLine)
We begin as before. This produces a string looking like 1;2;3;4;5;;1;2;3;4;5;;1;2;3;4;5;;1;2;3;4;5;;1;2;3;4;5.
Then we replace the double semicolon by the <br> embedded between two line breaks.
Try this function below:
Private Function FormatString(ByVal InputString As String) As String
InputString = InputString.Replace(vbCrLf, "")
For i = 0 To InputString.Length - 1
InputString = InputString.Insert(i + i, ";")
Next
InputString = InputString.Remove(0, 1)
Return InputString
End Function

How to append lines of text into a Richtextbox at specific location/index?

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

Substring a Word from a Multiline Textboxes

I want to do the following thing: if I have a Textbox1.Lines:
2
5
15
21
45
and I want to for example get the number left 15 and get the number right 15.
Output: Textbox2.Lines 15 21
5 15
How should I do this? example for the number 2 want substring 2 5
Module Module1
Public Function FirstWords(input As String,
count As Integer) As String
Dim words = count
For i As Integer = 0 To input.Length - 1
' Decrement word count when we reach a space.
If input(i) = " " Then
words -= 1
End If
' When no words remaining, return a substring to this point.
If words = 0 Then
Return input.Substring(0, i)
End If
Next
Return ""
End Function
how should i do this to work?
You can separate them into an array of strings with the Split function, like in this example:
Dim input as String = "2 5 15 21 45"
Dim numbers as String() = input.Split(" ")
For Each s as String In numbers
Console.WriteLine(s)
Next
You just have to figure out how to extract the numbers you want from the lot. Have fun!

Fill combobox with time values

I want fill my combobox with time values like (08:00, 08:10, 08:20 until 09:50) step=10 minutes but the result is like (8:00, 8:10, 8:20, 8:30, 9:-20,9:-10, 9:00, 9:10, 9:20).
My code doesn't show the value like 8:40, 8:50 and he also show negative value like 9:-20, 9:-10).
So please how can I resolve this problem?
Heure_rdv.Items.Clear()
Dim nbr_minute2 As String
For i = 480 To 590 Step 10
Dim nbr_heure As Integer = cint(i / 60)
Dim nbr_minute As Integer = (i - (nbr_heure * 60))
nbr_minute2 = CStr(nbr_minute) + "0"
If ((i - (nbr_heure * 60)) = 0) Then
Heure_rdv.Items.Add(CStr(nbr_heure) + ":" + nbr_minute2)
Else
Heure_rdv.Items.Add(CStr(nbr_heure) + ":" + CStr(nbr_minute))
End If
Next
This is how I should do try it in VBA.
Should be similar in VB.NET
DIM i As Integer 'Counter 1
Dim ii As Integer 'Counter 2
' make 1st loop for hours
for i = 8 To 9
' mkae 2nd loop for minutes
for ii = 0 To 50 Step 10
'
Heure_rdv.Items.Add(i & ":" & ii)
next
next
This could be done with fewer lines of code using linq
Dim steps = Enumerable.Range(0,6)
Dim items as List(Of String) = new List(Of String)()
items.AddRange(steps.Select(Function(x) "08:" & (x * 10).ToString("D2")))
items.AddRange(steps.Select(Function(x) "09:" & (x * 10).ToString("D2")))
Heure_rdv.DataSource = items
First we create a list of integers from 0 to 5 (6 elements), then using these elements we create the strings required multiplying each element of the integer list by ten and converting the result to a two digit string. We do this one time for the 8 hour and one time for the 9. Finally we could set the combo datasource to the resulting list of strings.

Looping through only columns containing values for specific row in dataTable

I am developing an application using Visual Basic 2010 for hydraulic calculations of a pipe network.
This application uses a lot of iterations and loops, depending on the user input and size of network. Most of the results have to be saved temporarily to be used for the next step of calculations.
Firstly, I used a DataGridView to save the results but as the number of iterations increased, the application became very slow.
Now I am trying to create a DataTable, then populate it with some initial results (this part was successful). The obtained DataTable has some columns that are not populated like so:
22 24 10
3 16 22 9 15
16 12 24 13
14 21 10 23 12 1
24 18 23 2 1
Other calculations are performed and a certain value (X) is obtained.
Now I am trying to loop through the columns of a specific row to check if the calculated value (X) equals to one of the values in those columns.
My question is: How can I loop through only the columns that have values (avoiding the columns containing NULL values) for a specific row?
I am a beginner in VB.net. I hope my question is clear as I didn't provide any code.
Thanks in advance for you help.
This is the initial code I used:
Results.DGVInitial.Rows.Clear()
Results.DGVFinal.Rows.Clear()
For m As Integer = 0 To NetworkLayout.DGVNetworkLayout.Rows.Count - 1
Results.DGVInitial.Rows.Add()
Next
Dim I As Integer = NetworkLayout.DGVNetworkLayout.Rows.Count - 1
Dim Sec(I), Ini(I) As Integer
Dim Hyd(I), Dia(I), Len(I) As Single
Dim Qsec(I), Qini(I), Vsec(I) As Single
Dim U(I), Y(I) As Single
Do
I = I - 1
Sec(I) = NetworkLayout.DGVNetworkLayout.Rows(I).Cells(0).Value
Ini(I) = NetworkLayout.DGVNetworkLayout.Rows(I).Cells(1).Value
Hyd(I) = NetworkLayout.DGVNetworkLayout.Rows(I).Cells(6).Value
Dia(I) = NetworkLayout.DGVNetworkLayout.Rows(I).Cells(4).Value
Len(I) = NetworkLayout.DGVNetworkLayout.Rows(I).Cells(3).Value
Dim V As Integer
V = Results.DGVRandomGen.Rows(TotalNum_Runs - 1).Cells(I).Value
Qsec(I) = 0
Dim q As Single = 0
For n As Integer = 0 To Results.DGVInitial.Rows.Count - 1
If Results.DGVInitial.Rows(n).Cells(1).Value = Sec(I) Then
q = Results.DGVInitial.Rows(n).Cells(0).Value
Qsec(I) = Qsec(I) + q
Else
Qsec(I) = Qsec(I)
End If
Next
If V = 1 Then ' if the hydrant is open
Qini(I) = Hyd(I) + Qsec(I)
Else ' if the hydrant is close
Qini(I) = Qsec(I)
End If
Results.DGVInitial.Rows(I).Cells(0).Value = Qini(I)
Results.DGVInitial.Rows(I).Cells(1).Value = Ini(I)
Results.DGVSectionDischarges.Rows(TotalNum_Runs - 1).Cells(I).Value = ini(I).ToString("F2")
Now instead of using
V = Results.DGVRandomGen.Rows(TotalNum_Runs - 1).Cells(I).Value
I would like to replace the "DGVRandomGen" with a DataTable called "DT_Random"
Like I said I am a beginner so I am not sure how to code it but it will be something like this:
For DT_Random.Rows (TotalNum_Runs - 1)
For Each col As DataColumn In DT_Random.Columns
If DT_Random.Rows(TotalNum_Runs - 1).Item(col) = I Then
Qini(I) = Hyd(I) + Qsec(I)
Else
Qini(I) = Qsec(I)
End If
Next
But I want to avoid Null values as not all columns are populated
Thanks
Maybe this will help you:
Dim myXvalue = 24
Dim myDataTable As New DataTable
myDataTable.Columns.Add("Col1")
myDataTable.Columns.Add("Col2")
myDataTable.Columns.Add("Col3")
myDataTable.Columns.Add("Col4")
myDataTable.Rows.Add(22, 24, 10, DBNull.Value)
myDataTable.Rows.Add(3, 16, 22, DBNull.Value)
myDataTable.Rows.Add(24, 18, DBNull.Value, 24)
For Each column As DataColumn In myDataTable.Columns
If IsDBNull(myDataTable.Rows(0).Item(column)) Then
MsgBox("DB Null Found At: " & column.ColumnName)
Continue For
End If
If myDataTable.Rows(0).Item(column) = myXvalue Then
MsgBox("Match: " & myDataTable.Rows(0).Item(column) & " found at " & column.ColumnName)
End If
Next column
Just a quick example, you may need to restructure it a bit, but at least it shows you how to access the values in your datatable by columns. I would do a function that passes a row index as a parameter and returns a boolean. Create two booleans inside the sub, one for dbnull existing in the row, and one for finding a matching value. If dbnull bool is false, and match value is true, then return true. Just make sure you loop all the columns and dont exit early.
If you need me to elaborate let me know.