length > 1 but only done once in for loop - vb.net

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.

Related

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

vb.net readalllines fill form replace certain lines coding advice

What I'm trying to accomplish is reading a text file and selecting certain lines to modify by filling in text from a second form. Here is an example the code I'm currently using. What's happening is I'm looking for a line that starts with 718 and then somewhere after that line there will be a line that starts with 720. I need both lines to fill in the second form. The only way I can think of is to just keep adding 1 to the line until it reaches the line I need. I'm still new to this and I'm sure there's an easier way to do this maybe using Try or While but I'm not sure how. Appreciate any help.
Dim lines() As String = File.ReadAllLines(tempsave)
For i As Integer = 0 To lines.Length - 1
If lines(i).StartsWith("718") Then
If lines(i + 1).StartsWith("720") Then
Dim array() As String = lines(i).Split("*"c, "~"c)
Dim array2() As String = lines(i + 1).Split("*"c, "~"c)
FormFill.TextBox1.Text = array(3)
FormFill.TextBox2.Text = array(9)
FormFill.ShowDialog()
lines(i) = lines(i).Replace(array(3), FormFill.TextBox1.Text)
lines(i + 1) = lines(i + 1).Replace(array(9), FormFill.TextBox2.Text)
Else
If lines(i + 2).StartsWith("720") Then
Dim array() As String = lines(i).Split("*"c, "~"c)
Dim array2() As String = lines(i + 2).Split("*"c, "~"c)
FormFill.TextBox1.Text = array(3)
FormFill.TextBox2.Text = array(9)
FormFill.ShowDialog()
lines(i) = lines(i).Replace(array2(3),FormFill.TextBox1.Text)
lines(i + 2) = lines(i + 2).Replace(array(9), FormFill.TextBox2.Text)
End If
End If
End If
Next
Example Data:
Input:
123*test*test*test~
718*test*test*test~
543*test*test*test~
720*test*test*test~
Output:
123*test*test*test~
718*test*test*newdata~
543*test*test*test~
720*test*test*newdata~
Here, try this:
Public Sub Lines()
Dim _
aNextLines,
aAllLines As String()
Dim _
s718Line,
s720Line As String
aAllLines = IO.File.ReadAllLines("D:\Logs\Data.log")
For i As Integer = 0 To aAllLines.Length - 1
If aAllLines(i).StartsWith("718") Then
s718Line = aAllLines(i)
aNextLines = aAllLines.Skip(i + 1).ToArray
s720Line = aNextLines.FirstOrDefault(Function(Line) Line.StartsWith("720"))
' Process data here
End If
Next
End Sub
--UPDATE--
Here's a modified version that both reads and writes:
Public Sub Lines()
Dim oForm As FormFill
Dim _
aNextLines,
aAllLines As String()
Dim _
i718Index,
i720Index As Integer
Dim _
s718Line,
s720Line As String
oForm = New FormFill
aAllLines = IO.File.ReadAllLines(oForm.FilePath)
s718Line = String.Empty
s720Line = String.Empty
For i718Index = 0 To aAllLines.Length - 1
If aAllLines(i718Index).StartsWith("718") Then
s718Line = aAllLines(i718Index)
aNextLines = aAllLines.Skip(i718Index + 1).ToArray
For i720Index = 0 To aNextLines.Length - 1
If aNextLines(i720Index).StartsWith("720") Then
s720Line = aNextLines(i720Index)
Exit For ' Assumes only one 720 line in the file
End If
Next
Exit For ' Assumes only one 718 line in the file
End If
Next
oForm.TextBox718.Text = s718Line
oForm.TextBox720.Text = s720Line
oForm.TextBox718.Tag = i718Index
oForm.TextBox720.Tag = i720Index
End Sub
Now, in your Save button's Click event handler:
Private Sub SaveButton_Click(Sender As Button, e As EventArgs) Handles SaveButton.Click
Dim aAllLines As String()
Dim _
i718Index,
i720Index As Integer
Dim _
s718Line,
s720Line As String
s718Line = Me.TextBox718.Text
s720Line = Me.TextBox720.Text
i718Index = Me.TextBox718.Tag
i720Index = Me.TextBox720.Tag
aAllLines = IO.File.ReadAllLines(Me.FilePath)
aAllLines(i718Index) = s718Line
aAllLines(i720Index) = s720Line
IO.File.WriteAllLines(Me.FilePath, aAllLines)
End Sub
That should do it.

Split string and put it in a textbox

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" ))

How to generate all combinations of partitioning a string in VB.Net

Given a string, how do you generate all partitions of it (shown as smaller strings separated by commas)?
Also, what is the total number of partitions for a string of length n?
The following will give the result, but is not good on long strings.
String: CODE
C,O,D,E
C,O,DE
C,OD,E
C,ODE
CO,D,E
CO,DE
COD,E
String: PEACE
P,E,A,C,E
P,E,A,CE
P,E,AC,E
P,E,ACE
P,EA,C,E
P,EA,CE
P,EAC,E
PE,A,C,E
PE,A,CE
PE,AC,E
PE,ACE
PEA,C,E
PEA,CE
Sub getAllComb()
oriStr = TextBox1.Text
Dim tmp = ""
Dim k = 0
For i = 0 To oriStr.Length
For j = 1 To 3
'tmp = Mid(oriStr, i, j)
Try
tmp1(k) = oriStr.Substring(i, j)
k = k + 1
'tmp = oriStr.Substring(i, j)
'Debug.Print(tmp)
Catch ex As Exception
'Debug.Print("Error>>>>" + ex.Message)
Exit For
End Try
Next
Next
tmp = ""
For i = 0 To k
Debug.Print(i.ToString + "<i " + tmp1(i))
tmp = tmp & tmp1(i) & vbCrLf
Next
'MessageBox.Show(tmp)
Dim tmpAll1 = ""
tmpAll1 = addFunclen4(k)
MessageBox.Show(tmpAll1)
Debug.Print(tmpAll1)
TextBox1.Text = oriStr & vbCrLf & vbCrLf & tmpAll1
End Sub
Function addFunclen4(k As Integer) As String
Dim retVal = ""
Dim tmp = ""
Dim tmpAll = ""
Dim tmpStr = ""
Dim tmpAll1 = ""
For i = 0 To k
For i1 = 0 To k
For i2 = 0 To k
For i3 = 0 To k
For i4 = 0 To k
tmp = Form1.tmp1(i) + Form1.tmp1(i1) + Form1.tmp1(i2) + Form1.tmp1(i3) + Form1.tmp1(i4)
If Form1.tmp1(i) <> "" Then
If tmp = Form1.oriStr Then
tmpStr = Form1.tmp1(i) + "," + Form1.tmp1(i1) + "," + Form1.tmp1(i2) + "," + Form1.tmp1(i3) + "," + Form1.tmp1(i4)
Do While tmpStr.Contains(",,") = True
tmpStr = Replace(tmpStr, ",,", ",")
Loop
If Mid(tmpStr, tmpStr.Length, 1) = "," Then
tmpStr = Mid(tmpStr, 1, tmpStr.Length - 1)
End If
If tmpAll1.Contains(tmpStr) = False Then
tmpAll1 = tmpAll1 + tmpStr + vbCrLf
End If
End If
End If
Next
Next
Next
Next
Next
retVal = tmpAll1
Return retVal
End Function
I reckon [2^(n-1) - 1] in total:
(n-1) positions to put a comma, 2 "states" (comma or not comma), -1 for the trivial case with no commas.
A simpler algorithm would be to iterate through the number of cases and use the binary representation to determine whether to put a comma in each position.
For example (simple form with TextBox, Button and ListBox):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim s As String = TextBox1.Text
If s.Length < 2 Then
MessageBox.Show("Enter a longer string")
Return
End If
For i = 1 To Math.Pow(2, s.Length - 1) - 1
Dim result As String = s(0)
For j = 1 To s.Length - 1
result = result & CommaOrNot(i, j) & s(j)
Next
ListBox1.Items.Add(result)
Next
End Sub
Private Function CommaOrNot(i As Integer, j As Integer) As String
If (i And Math.Pow(2, j - 1)) = Math.Pow(2, j - 1) Then
Return ","
Else
Return ""
End If
End Function
I really liked Fruitbat's approach. Here's an alternate version using a slightly different mechanism for the representation of the binary number and how to determine if the comma should be included or not:
Public Class Form1
Private combinations As List(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As String = TextBox1.Text
If s.Length < 2 Then
MessageBox.Show("Enter a longer string")
Exit Sub
End If
Button1.Enabled = False
ListBox1.DataSource = Nothing
ListBox1.Items.Clear()
ListBox1.Items.Add("Generating combinations...")
BackgroundWorker1.RunWorkerAsync(s)
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim s As String = e.Argument
Dim combinations As New List(Of String)
Dim binary() As Char
Dim values() As Char = s.ToCharArray
Dim max As Integer = Convert.ToInt32(New String("1", s.Length - 1), 2)
Dim sb As New System.Text.StringBuilder
For i As Integer = 0 To max
sb.Clear()
binary = Convert.ToString(i, 2).PadLeft(values.Length, "0").ToCharArray
For j As Integer = 0 To values.Length - 1
sb.Append(If(binary(j) = "0", "", ","))
sb.Append(values(j))
Next
combinations.Add(sb.ToString)
Next
e.Result = combinations
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
combinations = e.Result
ListBox1.Items.Clear()
ListBox1.Items.Add("Generating combinations...Done!")
ListBox1.Items.Add("Adding Results...one moment please!")
Application.DoEvents()
ListBox1.DataSource = Nothing
ListBox1.DataSource = combinations
Button1.Enabled = True
MessageBox.Show("Done!")
End Sub
End Class

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.