VB Reading a text file into a 2D array? - vb.net

I am trying to create a sudoku game, with load and save game functions in VB and I was wondering how it would be possible to load a save file(the numbers on the sudoku board and time score) and make the numbers within the file correspond to their exact location on the new board. For saving the file I have this:
Private Sub saveBoard(fileName As String)
Dim f As StreamWriter = New StreamWriter(fileName)
For col = 0 To 8
Dim sudokuLine = ""
For row = 0 To 8
If (board(row, col).Text = "") Then
sudokuLine += "0"
Else : sudokuLine += board(row, col).Text
End If
sudokuLine += " "
Next
f.WriteLine(sudokuLine)
Next
f.WriteLine(lblMinutes.Text + " " + lblSeconds.Text)
f.Close()
End Sub
Also I know about StreamReader...

This could be the loadBoard that corresponds to your saveBoard.
Of course a bit of testing is required here and a more fool proof approach to handle critical errors (like a different file passed as input) .
Notice that I have added the using statement around the opening of the Stream. This should be done also in the saveBoard above to avoid problems with files locked in case of exceptions.
Private Sub loadBoard(fileName As String)
Using f = New StreamReader(fileName)
For col = 0 To 8
Dim sudokuColumn = f.ReadLine()
Dim cells() = sudokuColum.Split()
For row = 0 To 8
If cells(row, col) = "0") Then
boards(row, col).Text = ""
Else
board(row, col).Text = cells(row,col)
End If
Next
Next
lblMinutes.Text = f.ReadLine()
End Using
End Sub

Related

Splitting string every 100 characters not working

I am having a problem where I just can't seem to get it to split or even display the message. The message variable is predefined in another part of my code and I have debugged to make sure that the value comes through. I am trying to get it so that every 100 characters it goes onto a new line and with every message it also goes onto a new line.
y = y - 13
messagearray.AddRange(Message.Split(ChrW(100)))
Dim k = messagearray.Count - 1
Dim messagefin As String
messagefin = ""
While k > -1
messagefin = messagefin + vbCrLf + messagearray(k)
k = k - 1
End While
k = 0
Label1.Text = Label1.Text & vbCrLf & messagefin
Label1.Location = New Point(5, 398 + y)
You can use regular expression. It will create the array of strings where every string contains 100 characters. If the amount of remained characters is less than 100, it will match all of them.
Dim input = New String("A", 310)
Dim mc = Regex.Matches(input, ".{1,100}")
For Each m As Match In mc
'// Do something
MsgBox(m.Value)
Next
You can use LINQ to do that.
When you do a Select you can get the index of the item by including a second parameter. Then group the characters by that index divided by the line length so, the first character has index 0, and 0 \ 100 = 0, all the way up to the hundredth char which has index 99: 99 \ 100 = 0. The next hundred chars have 100 \ 100 = 1 to 199 \ 100 = 1, and so on (\ is the integer division operator in VB.NET).
Dim message = New String("A"c, 100)
message &= New String("B"c, 100)
message &= New String("C"c, 99)
Dim lineLength = 100
Dim q = message.Select(Function(c, i) New With {.Char = c, .Idx = i}).
GroupBy(Function(a) a.Idx \ lineLength).
Select(Function(b) String.Join("", b.Select(Function(d) d.Char)))
TextBox1.AppendText(vbCrLf & String.Join(vbCrLf, q))
It is easy to see how to change the line length because it is in a variable with a meaningful name, for example I set it to 50 to get the output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
You can use String.SubString to do that. Like this
Dim Message As String = "your message here"
Dim MessageList As New List (Of String)
For i As Integer = 0 To Message.Length Step 100
If (Message.Length < i + 100) Then
MessageList.Add(Message.SubString (i, Message.Length - i)
Exit For
Else
MessageList.Add(Message.SubString (i, 100))
End If
Next
Dim k = MessageList.Count - 1
...
Here is what your code produced with a bit of clean up. I ignored the new position of the label.
Private Sub OpCode()
Dim messagearray As New List(Of String) 'I guessed that messagearray was a List(Of T)
messagearray.AddRange(Message.Split(ChrW(100))) 'ChrW(100) is lowercase d
Dim k = messagearray.Count - 1
Dim messagefin As String
messagefin = ""
While k > -1
messagefin = messagefin + vbCrLf + messagearray(k)
k = k - 1
End While
k = 0 'Why reset k? It falls out of scope at End Sub
Label1.Text = Label1.Text & vbCrLf & messagefin
End Sub
I am not sure why you think that splitting a string by lowercase d would have anything to do with getting 100 characters. As you can see the code reversed the order of the list items. It also added a blank line between the existing text in the label (In this case Label1) and the new text.
To accomplish your goal, I first created a List(Of String) to store the chunks. The For loop starts at the beginning of the input string and keeps going to the end increasing by 10 on each iteration.
To avoid an index out of range which would happen at the end. Say, we only had 6 characters left from start index. If we tried to retrieve 10 characters we would have an index out of range.
At the end we join the elements of the string with the separated of new line.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BreakInto10CharacterChunks("The quick brown fox jumped over the lazy dogs.")
End Sub
Private Sub BreakInto10CharacterChunks(input As String)
Dim output As New List(Of String)
Dim chunk As String
For StartIndex = 0 To input.Length Step 10
If StartIndex + 10 > input.Length Then
chunk = input.Substring(StartIndex, input.Length - StartIndex)
Else
chunk = input.Substring(StartIndex, 10)
End If
output.Add(chunk)
Next
Label1.Text &= vbCrLf & String.Join(vbCrLf, output)
End Sub
Be sure to look up String.SubString and String.Join to fully understand how these methods work.
https://learn.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.8
and https://learn.microsoft.com/en-us/dotnet/api/system.string.join?view=netframework-4.8

Writing CSV file VB.NET

i'm just having troubles with a CSV file saving code in VB.NET
I'm trying to save a CSV file that will be read in a DataGridView. I just made the "reading" code, it works perfectly! What's wrong with saving of the file?
Public Class Builder
Sub Create_Records()
Dim i As Integer
For i = 0 To Manager.Grid.Rows.Count - 1
Manager.Grid.Rows.Add("ciao;", "comestai")
Next
End Sub
Sub Write()
If (Not System.IO.Directory.Exists("C:\MyPCBuilder")) Then
System.IO.Directory.CreateDirectory("C:\MyPCBuilder")
End If
Dim Write As New System.IO.StreamWriter("C:\MyPCBuilder\builds.csv")
For i = 0 To Manager.Grid.Rows.Count - 1
Text = ""
For j = 0 To 1
If Text = Text & Manager.Grid.Rows(i).Cells(j).Value & ";" Then
Else
Text = Text & Manager.Grid.Rows(i).Cells(j).Value
End If
Next
Write.WriteLine(Text)
Next
Write.Close()
End Sub
Private Sub btn_Confirm_Click(sender As Object, e As EventArgs) Handles btn_Confirm.Click
Create_Records()
Write()
End Sub
End Class
The If-statement in your inner loop is wrong. The Then part is empty and the condition is strange. Since you have only 2 columns, simply write
For i = 0 To Manager.Grid.Rows.Count - 1
Text = Manager.Grid.Rows(i).Cells(0).Value & ";" & Manager.Grid.Rows(i).Cells(1).Value
Write.WriteLine(Text)
Next
If you want to keep this inner loop (in case you have more columns or a variable number of columns), test for the index instead (outer loop not shown)
Text = ""
For j = 0 To 1
If j > 0 Then
Text = Text & ";"
End If
Text = Text & Manager.Grid.Rows(i).Cells(j).Value
Next

Parallel Loop & Stringbuilder

Intro: Hi all this is my first question, so please do let me know if I did or am doing anything wrong. I working on a project and one of its functions is to write a huge chunk of text to process the data and replace specific text at certain lines and output the file to a richtextbox/file.
Problem: The problem is that when I use a parallel foreach, my results in the richtextbox are all over the place, they are not according to lines (e.g. the first line may become the 15th line after the parallel foreach loop is run).
What the code does: It loops through a richtextbox and checks if the line matches the first line in a temporary richtextbox, if so it would stop the append and copy the text from the temporary richtextbox and let the loop run until the amount of lines which have past match the number of lines in the temporary richtextbox and then continue the append. It is basically a replace of text. Total output I am looking at about 20K lines.
Dim completertb4text As New StringBuilder("")
Parallel.ForEach(newrtb.Lines, Function() New StringBuilder(), Function(x, [option], sb)
If x.Contains(richtextboxl0) Then
startcount = True
sb.Append(vbNewLine & richtextbox3text & "111111")
End If
If startcount = True Then
If counter = temptextbox3count Then
startcount = False
Else
counter += 1
End If
End If
If sb.Length = 0 Then
sb.Append(vbNewLine & x & "222222")
End If
If sb.Length > 0 Then
sb.Append(vbNewLine & x & "3333333")
End If
Return sb
Function, Sub(sb)
SyncLock completertb4text
completertb4text.Append(sb.ToString())
End SyncLock
End Sub
Any help is kindly appreciated, thanks in advance!
Here is how I shrank my code from 08:00 seconds to 00:01 seconds.
Dim counter As Integer = 0
Dim countertrue As Integer = 0
Dim countintertal As Integer = 0
Dim newrtbstrarray As String() = newrtb.Lines
Dim rtb3array As String() = richtextbox3text.Lines
For Each line As String In newrtbstrarray
If line.Contains(richtextboxl0) Then
countertrue = counter
For Each element As String In rtb3array
newrtbstrarray(countertrue) = rtb3array(countintertal)
countertrue += 1
countintertal += 1
Next
End If
counter += 1
Next

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

Why does Microsoft Visual Basic skip over part of this code

I'm trying to make some labels on my Form to be visible, but i don't want to use a lot of if statements, but for some reason whenever i put Me.Controls(lbl).Visbel = True in a for or do loop it skips the whole loop. The code worked perfectly the way I wanted it until i got an error for calling Dim lbl = Controls("Label" & counter_3) for the whole class instead of in the From_load private sub. Sometimes i can get it to work, but only one label is visible
Dim chararray() As Char = word_list(random_word).ToCharArray
Dim lbl = "Label" & counter_3
For Each item In chararray
If item = Nothing Then
Else
word_list(counter_2) = item.ToString()
counter_2 += 1
End If
Next
For Each item In chararray
If item = Nothing Then
Else
counter_3 += 1
Me.Controls(lbl).Visible = True
MsgBox(item & " " & counter_3)
End If
Next
I've also tried. In both the loops are completely skipped over. I know this because the MsgBox's don't appear.
Dim chararray() As Char = word_list(random_word).ToCharArray
Dim lbl = Controls("Label" & counter_3)
For Each item In chararray
If item = Nothing Then
Else
word_list(counter_2) = item.ToString()
counter_2 += 1
End If
Next
For Each item In chararray
If item = Nothing Then
Else
counter_3 += 1
lbl.Visble = True
MsgBox(item & " " & counter_3)
End If
Next
The thing that I am noticing is that you are creating a Char array based on a random word returned from your word_list, you then iterate through the Char array using the count of the character in the array as an index into your word_list, if the amount of characters in your word exceeds the amount of words in your list you will get an error and since this error is in the Forms Load event it will be swallowed and all the code after it will be aborted. There are also other issues that I would change like making sure all declarations have a type and I would probably use the Controls.Find Method instead and check that it has an actual object. But what I would probably do first is move your code to a separate Subroutine and call it after your IntializeComponent call in the Forms Constructor(New) Method.
Something like this.
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
YourMethod
End Sub
Public Sub YourMethod()
Dim chararray() As Char = word_list(random_word).ToCharArray
Dim lbl As Control() = Controls.Find("Label" & counter_3, True)
For Each item In chararray
If item = Nothing Then
Else
word_list(counter_2) = item.ToString()
counter_2 += 1
End If
Next
For Each item In chararray
If item = Nothing Then
Else
counter_3 += 1
If lbl.Length > 0 Then
lbl(0).Visible = True
Else
MsgBox("Control not Found")
End If
MsgBox(item & " " & counter_3)
End If
Next
End Sub