Split string and put it in a textbox - vb.net

How to split the string "V237P023F50.5" into different text boxes.
I am trying this code:
Dim input As String = "V237P023F50.5"
TextBox1.Text = input
Dim parts As String() = input.Split(New String() {"V"}, StringSplitOptions.RemoveEmptyEntries)
Dim parts1 As String() = input.Split(New String() {"V", "P"}, StringSplitOptions.RemoveEmptyEntries)
Dim parts2 As String() = input.Split(New String() {"V", "P", "F"}, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 0 To parts.Length - 1
If i > 0 OrElse input.StartsWith("V") = True Then
parts(i) = "" & parts(i)
TextBox2.Text = parts(i)
End If
Next
For i As Integer = 0 To parts1.Length - 1
If i > 0 OrElse input.StartsWith("P") = True Then
parts1(i) = "" & parts1(i)
TextBox4.Text = parts1(i)
End If
Next
For i As Integer = 0 To parts2.Length - 1
If i > 0 OrElse input.StartsWith("F") = True Then
parts2(i) = "" & parts2(i)
TextBox5.Text = parts2(i)
End If
Next
Expected output
V237
P023
F50.5
Please help me.

If this is fixed length then use Substring().
Dim oldstring As String = "V237P023F50.5"
TextBox2.Text = oldstring.Substring(0, 4)
TextBox4.Text = oldstring.Substring(4, 4)
TextBox5.Text = oldstring.Substring(8)
If the alphacharacters are the same then you can use IndexOf() with Substring.
Dim oldstring As String = "V237P023F50.5"
TextBox2.Text = oldstring.Substring(oldstring.IndexOf("V" ), (oldstring.IndexOf("P") - oldstring.IndexOf("V")))
TextBox4.Text = oldstring.Substring(oldstring.IndexOf("P" ), (oldstring.IndexOf("F") - oldstring.IndexOf("P")))
TextBox5.Text = oldstring.Substring(oldstring.IndexOf("F" ))

Related

Combining two string and insert element VB.Net

I tried to build a combination algorithm between 2 strings, unfortunately it has some errors.
Dim strWordsA() As String = TextBox1.Text.Split(",")
Dim strWordsB() As String = TextBox2.Text.Split(",")
Dim str As String = TextBox1.Text
Dim arr As String() = TextBox1.Text.Split(","c)
For i As Integer = 0 To TextBox1.Text.Split(",").Length - 1
Dim index As Integer = str.IndexOf(strWordsA(i))
TextBox1.Text = str.Insert(index + 2, "," & strWordsB(i))
str = TextBox1.Text
Next
so if we have Textbox1.Text = 1,2,3,4,5,6,7,8,9 and Textbox2.Text = a,b,c,f,d,b,i,h, and so on... I need to display this in a 3rd textbox
Textbox3.Text = 1,a,2,b,3,c,4,f and so on
so do I combine these 2 strings?
the first element in the index displays it incorrectly, otherwise it seems to work ok.
Try this:
Private Function MergeStrings(s1 As String, s2 As String) As String
Dim strWordsA() As String = s1.Split(","c)
Dim strWordsB() As String = s2.Split(","c)
Dim i As Integer = 0
Dim OutputString As String = String.Empty
While i < strWordsA.Length OrElse i < strWordsB.Length
If i < strWordsA.Length Then OutputString &= "," & strWordsA(i)
If i < strWordsB.Length Then OutputString &= "," & strWordsB(i)
i += 1
End While
If Not OutputString = String.Empty Then Return OutputString.Substring(1)
Return OutputString
End Function
Usage:
Dim s As String = MergeStrings("1,2,3,4,5,6,7,8,9", "a,b,c,f,d,b,i,h")
You will need to add your own validation to allow for trailing commas or no commas etc but it should work with different length input strings
EDIT: amended as per Mary's comment

Read text file and name buttons VB.NET

What I wan't to do is to read a text file with 1, 2, 3, 4 or 5 lines.
If the text file is completely full (5 lines) then the code works.
But the code needs to work when there are only 3 lines in the text file.
So eventually you get 3 buttons with names and 2 without.
The code:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Lines() As String = IO.File.ReadAllLines(file)
Dim Ordernummer1 As String
Dim Ordernummer2 As String
Dim Ordernummer3 As String
Dim Ordernummer4 As String
Dim Ordernummer5 As String
Dim Regels As New List(Of String)
Select Case Lines.Count
Case 1
Lines(1) = ""
Lines(2) = ""
Lines(3) = ""
Lines(4) = ""
Case 2
Lines(2) = ""
Lines(3) = ""
Lines(4) = ""
Case 3
Lines(3) = ""
Lines(4) = ""
Case 4
Lines.add(4) = ""
Case 5
'nothing
End Select
MsgBox(Lines(0))
last = Lines(0).Substring(Lines(0).LastIndexOf("\") + 1)
MsgBox(last)
Ordernummer1 = last.Substring(0, last.IndexOf(" "))
MsgBox(Ordernummer1)
Button12.Text = Ordernummer1
MsgBox(Lines(1))
last = Lines(1).Substring(Lines(1).LastIndexOf("\") + 1)
MsgBox(last)
Ordernummer2 = last.Substring(0, last.IndexOf(" "))
MsgBox(Ordernummer2)
Button13.Text = Ordernummer2
MsgBox(Lines(2))
last = Lines(2).Substring(Lines(2).LastIndexOf("\") + 1)
MsgBox(last)
Ordernummer3 = last.Substring(0, last.IndexOf(" "))
MsgBox(Ordernummer3)
Button14.Text = Ordernummer3
MsgBox(Lines(3))
last = Lines(3).Substring(Lines(3).LastIndexOf("\") + 1)
MsgBox(last)
Ordernummer4 = last.Substring(0, last.IndexOf(" "))
MsgBox(Ordernummer4)
Button15.Text = Ordernummer4
MsgBox(Lines(4))
last = Lines(4).Substring(Lines(4).LastIndexOf("\") + 1)
MsgBox(last)
Ordernummer5 = last.Substring(0, last.IndexOf(" "))
MsgBox(Ordernummer5)
Button16.Text = Ordernummer5
One option:
Dim lines = File.ReadAllLines(filePath)
Dim buttons = {Button12, Button13, Button14, Button15, Button16}
'Process lines and corresponding Buttons.
For index = 0 To lines.GetUpperBound(0)
Dim line = lines(index)
Dim btn = buttons(index)
'Use line and btn here.
Next
'Process remaining Buttons if desired.
For index = lines.Length To buttons.GetUpperBound(0) Step 1
'E.g.
buttons(index).Hide()
Next

adding color, depending on item value in list view

I have a list view like this:
It shows item description and how much quantity delivered.
If all quantity delivered then, I need to show line in green.
If any quantity delivered(partial) then, I need to show in orange.
If no quantity delivered then, I need to show yellow.
I have given code like this,and it is not working.
ListView1.Font = New System.Drawing.Font("Tahoma", 8.0!, System.Drawing.FontStyle.Bold)
ListView1.Items.Clear()
If ds.Tables(0).Rows.Count > 0 Then
Dim dt As DataTable = ds.Tables(0)
Dim str(ds.Tables(0).Columns.Count) As String
Dim lvi As ListViewItem
Dim rr As DataRow
Dim highlight As String = ""
Dim temp1 As String = ""
Dim temp2 As String = ""
Dim temp1Sum, temp2Sum As Integer
Dim diffCount As Integer = 0
Dim newColumn As Integer = 0
Dim strTemp As String
For Each rr In dt.Rows
For col As Integer = 0 To ds.Tables(0).Columns.Count - 1
str(col) = rr(col).ToString()
If col > 4 And col > newColumn Then
Dim qtyVal As String
qtyVal = rr(col).ToString
strTemp = qtyVal
If temp1 = "" Then
temp1 = IIf(qtyVal = "", "0.00", qtyVal)
Else
temp2 = temp1
temp1 = IIf(qtyVal = "", "0.00", qtyVal)
newColumn = col + 1
End If
If temp1 <> "" And temp2 <> "" Then
If temp1 <> temp2 Then
diffCount = diffCount + 1
End If
temp1 = ""
temp2 = ""
End If
End If
Next
lvi = New ListViewItem(str)
ListView1.Items.Add(lvi)
If diffCount = 0 Then
lvi.BackColor = Color.Green
noofdeliver = noofdeliver + 1
txtdelivercount.Text = noofdeliver
ElseIf diffCount > 0 Then
If temp2Sum = 0 Then
lvi.BackColor = Color.Yellow
Else
lvi.BackColor = Color.Orange
End If
End If
temp2Sum = 0
diffCount = 0
newColumn = 0
Next
End If
Put this line ListView1.Items.Add(lvi) after the color changing
TEST CODE
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lvi = New ListViewItem("foo " & ListView1.Items.Count + 1)
lvi.BackColor = Color.Red
ListView1.Items.Add(lvi)
End Sub
here the actual problem is...
you are using str variable as array but you are assigning to ListViewItem as lvi = New ListViewItem(str) without specifying the index for str array. As you specified in code it always takes the first value of the array only..
solution: when you are assigning array str value to ListViewItemlvi specify the corresponding index value.

length > 1 but only done once in for loop

Code :
Private Sub RefreshToDoList() Handles MyBase.Load
If FileExists(NewEvent.ToDoItems_Path) And FileExists(NewEvent.ToDoDates_Path) And FileExists(NewEvent.ToDoContents_Path) Then
If Not ContentOf(NewEvent.ToDoItems_Path).ToString = "" And Not ContentOf(NewEvent.ToDoDates_Path).ToString = "" And Not ContentOf(NewEvent.ToDoContents_Path).ToString = "" Then
ToDoListItems = ContentOf(NewEvent.ToDoItems_Path).ToString.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
ToDoListDates = ContentOf(NewEvent.ToDoDates_Path).ToString.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
ToDoListContents = ContentOf(NewEvent.ToDoContents_Path).ToString.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
End If
If ToDoListItems.Length = ToDoListDates.Length And ToDoListItems.Length = ToDoListContents.Length Then
For count As Integer = 0 To ToDoListItems.Length - 1
ToDoListBox.Items.Add(ToDoListItems(count))
ToDoListInfo(count) = "Date : " + ToDoListDates(count) + Convert.ToString(Environment.NewLine + Environment.NewLine) + "Content : " + ToDoListContents(count)
Next
End If
End If
End Sub
Checked, and ToDoListItems.Length is 3 but there is only one item in ToDoListBox.
What's the problem in this code? It seemed to be in the for loop.

Code always search the last record not the number I inputed

Why my code always search the last record not the one I searched, like this 8850338005909.
Here is code Sample Data in Textfile:
8501045891528,2271,"ADAMS CNDY HALLS
HNYLMN100S/36",57.25,36.00,4822,1,100,101,465
8850338001604,2271,"ADAMS CNDY HALLS
HNYLMN100S/36",57.25,36.00,4822,1,100,101,465
8850338005909,2271,"ADAMS CNDY HALLS
HNYLMN100S/36",57.25,36.00,4822,1,100,101,465
8850338002519,2312,"CLRTS CNDY COOL MINT
40S/60",27.75,60.00,7049,1,100,101,465 8850338001000,2313,"ADAMS GUM
CLRETSTWIN PK14GX2/1",66.50,1.00,4822,1,100,101,470
4804880224138,2315,"BGUIO OIL CKNG
16KG/1",978.00,1.00,6135,1,192,193,639
And my code:
Private Sub Data2()
Dim text As String = IO.File.ReadAllText("C:\ITEMRTV.txt")
Dim index As Integer = text.IndexOf(TextBox1.Text)
Dim Values() As String = Split(text, ",")
If index >= 0 Then
' String is in file, starting at character "index"
Label1.Text = Values(0) ' now contains first column value,
Label2.Text = Values(1) ' contains second column, etc.
Label3.Text = Values(2)
Label4.Text = Values(3)
Label5.Text = Values(4)
Label6.Text = Values(5)
End If
End Sub
Try this:
Dim text As String = IO.File.ReadAllText("C:\ITEMRTV.txt")
Dim Values() As String = text.Split(New String() {",", Environment.NewLine}, StringSplitOptions.None)
Dim index As Integer = Array.IndexOf(Values, TextBox1.Text)
If index >= 0 Then
Label1.Text = Values(index)
Label2.Text = Values(index + 1)
Label3.Text = Values(index + 2)
Label4.Text = Values(index + 3)
Label5.Text = Values(index + 4)
Label6.Text = Values(index + 5)
End If
But you should divide into lines and columns:
Dim text() As String = IO.File.ReadAllLines("C:\ITEMRTV.txt")
Dim s() As String
For Each line As String In text
s = line.Split(","c)
If s(0).Equals(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) _
AndAlso s.Length > 5 Then
Label1.Text = s(0)
Label2.Text = s(1)
Label3.Text = s(2)
Label4.Text = s(3)
Label5.Text = s(4)
Label6.Text = s(5)
Exit For
End If
Next
You could even create your own structure to hold the data, what would be the better option. For example, declarin a Dictionary(Of Integer, List(Of String)) for each file, because the first field looks like a ID, or declaring your own class to hold each field appropiately.