exceeding bounds of array in VB - vb.net

I have an assignmemnt for school, "Create a txt file with to fill a 5x8 array. I can read from the file fine, however, i keep getting System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' I know this means my index is exceeding my array bounds, however it should not be, at least that i can tell. any help would be appreciated. Code listed below in full...Error is coming from the private sub stansgrocery_load
Public Class StansGrocery
Dim fileNameString As String = "C:\Users\lisht\Dropbox\Lish\Week 4\Stans\Grocerys.txt" 'Setting the preloaded location for my grocery.txt file
Dim Food$(4, 7) 'setting the boundaries of the array
Private Sub ReadFile(ByVal fileNameString As String, ByRef recordData() As String)
fileNameString = "C:\Users\lisht\Dropbox\Lish\Week 4\Stans"
Dim ptr, hell, cold As Int32
itemsComboBox.Items.Clear()
Dim currentRecordString, fileDataString As String
Dim fileNumberInt As Integer = FreeFile()
'Reads in a file with the first argument being it's location, and the second an array. The functions FileOpen, and Input use the filename to
'identify the file location, and filenumber, which is set by freefile, to identify the file.
Do
Try
FileOpen(1, fileNameString, OpenMode.Input) 'Opening the file
hell = 0
Catch ex As Exception
hell = 1
Me.OpenFileDialog1.FileName = fileNameString
Me.OpenFileDialog1.ShowDialog()
fileNameString = OpenFileDialog1.FileName
End Try
Loop Until hell = cold
recordData = Split(fileDataString, "$$") 'splitting the words for each deliminator of "$$"
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles searchTextbox.TextChanged
'This is the only way to close the program, by having the textbox check for change and seeing
'"zzz" the form will close.
If searchTextbox.Text = "zzz" Then Me.Close()
End Sub
Private Sub StansGrocery_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'On load reads in the file with ReadFile Sub.
Dim originalFileName As String = "C:\Users\lisht\"
Dim inventoryIn() As String
'The first read in information is an empty string, so by Dimensioning i at 1 the first blank is always skipped, and also resets with each load.
Dim i As Integer
ReadFile(originalFileName, inventoryIn)
For aisle = 0 To 4
For item = 0 To 7
Food(aisle, item) = inventoryIn(i)
itemsComboBox.Items.Add(inventoryIn(i))
i += 1
Next
Next
End Sub
Private Sub InventoryComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles itemsComboBox.SelectedIndexChanged
'Loops through each array position and checks to see if the item is equal to the Combo box selected item. Then shows its aisle and number,
'by adding one to it's array index row and column. If it is found it displays a message with the location.
For aisle = 0 To 4
For item = 0 To 7
If Food(aisle, item) Is itemsComboBox.SelectedItem Then
Label1.Text = ("You will find " & Food(aisle, item) & " on aisle #" & aisle + 1 & ", item # " & item + 1)
End If
Next
Next
End Sub
Private Sub searchTextBoxButton_Click(sender As Object, e As EventArgs) Handles searchTextBoxButton.Click
'Initiaties a search, and sets isFound to it's default "Item not found" message.
Dim isFound As String = "The item you are searching for is not in the store"
'Loops through each array position and checks to see if the item is equal to the read in text.
'Then shows its aisle and number, by adding one to it's array index row and column. If it is found it displays a message with the location.
For aisle = 0 To 4
For Item = 0 To 7
If Food(aisle, Item).ToLower = searchTextbox.Text.ToLower Then
'updates isFound if the item is found.
isFound = "You will find " & Food(aisle, Item) & " on aisle #" & aisle + 1 & ", item #" & Item + 1
End If
Next
Next
'Displays the location of the item, or the "Item not found message.
Label1.Text = (isFound)
searchTextbox.Clear()
End Sub
End Class

Related

vb.net statusbar text doesn't show

I have a code that searches for words in documents and fills a Listview with the found documents. Because the process can be rather lengthly I put a warning in the statusbar.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim foundList As Boolean
Dim docImage As VariantType
Dim PresetName As String
ListView1.Items.Clear()
ToolStripLabel1.Text = "Searching your documents, please wait."
Try
For Each row As DataRowView In CheckedListBox1.CheckedItems
SearchRegX = row("RegX")
Next
If TextBoxFreeText.Text <> "" Then
'look for a space in the search criteria, meaning there are more words
Dim counter As Integer = TextBoxFreeText.Text.IndexOf(" ")
If counter <> -1 Then
'more words were entered to search
Dim varSplit As Object
varSplit = Split(TextBoxFreeText.Text, " ")
'if more than two words are entered our regex doesnt work, so we exit the sub
If varSplit.length > 2 Then
MsgBox("Your search criteria are to complex, use a maximum of two words",, Title)
Exit Sub
End If
iWords = NumericUpDown1.Value
SearchRegX = "(?i)\b(?:" + varSplit(0) + "\W+(?:\w+\W+){0," + iWords + "}?" + varSplit(1) + "|" + varSplit(1) + "\W+(?:\w+\W+){0," + iWords + "}?" + varSplit(0) + ")\b"
Else
'just one word was entered
SearchRegX = "(?i)\b" + TextBoxFreeText.Text + "\b"
End If
End If
If SearchRegX = "" Then
MsgBox("No Keyword was selected",, Title)
Exit Sub
End If
If ListBox1.SelectedIndex > -1 Then
For Each Item As Object In ListBox1.SelectedItems
Dim ItemSelected = CType(Item("Path"), String)
SearchFolder = ItemSelected
'check if the folder of the archive still exists
If (Not System.IO.Directory.Exists(SearchFolder)) Then
Dim unused = MsgBox("The archive " + SearchFolder.Substring(SearchFolder.Length - 5, Length) + " was not found",, Title)
Continue For
End If
Dim dirInfo As New IO.DirectoryInfo(SearchFolder)
Dim files As IO.FileInfo() = dirInfo.GetFiles()
Dim file As IO.FileInfo
docImage = ImageList1.Images.Count - 1
Dim items As New List(Of ListViewItem)
For Each file In files
Dim filename As String = file.Name.ToString
If file.Extension = ".pdf" Or file.Extension = ".PDF" Then
foundList = PDFManipulation.GetTextFromPDF2(SearchFolder + filename, SearchRegX)
If foundList = True Then
If ListView1.FindItemWithText(filename.ToString) Is Nothing Then
items.Add(New ListViewItem(New String() {"", filename.ToString, SearchFolder.ToString}, docImage))
End If
End If
End If
Next
ListView1.Items.AddRange(items.ToArray)
Next
ToolStripLabel1.Text = ListView1.Items.Count.ToString + " Documents found."
Else
MsgBox("No archive was selected",, Title)
End If
SearchRegX = ""
SearchFolder = ""
'now save the search word to our textfile
PresetName = TextBoxFreeText.Text
If PresetName <> "" Then
AddSearchWord()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Somehow the text doesn't show in the statusbar before the process starts.
What should I change?
I thought about a BackgroundWorker showing an image like waiting but the BackgroundWorker doesn't work because inside my Sub I call a function elsewhere.
This happens because the status strip control is not rendered properly before the pending workload is finished. To do so, you can refresh the status strip manually:
StatusStrip1.Refresh()
That is, if your only goal is to set the text. If you thought about running the code in the background so that the form is still responsive to user input, you'll need asynchronous programming using System.Threading.Tasks or System.Threading.Thread to run the code as a seperate thread. Be aware though that you may face difficulties when trying to access controls outside of the main thread.
Add Async to your button handler declaration:
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Now change your call to PDFManipulation.GetTextFromPDF2() so that it is within a Task:
Await Task.Run(Sub()
foundList = PDFManipulation.GetTextFromPDF2(SearchFolder + filename, SearchRegX)
End Sub)
If foundList = True Then
...

vb.NET You are not allowed to perform an operation across different threads

I am running code to read text from pdf files and than create a WordCloud. To inform the user the process is on going I add a BackgroundWorker to my form that shows an image saying Loading. I get an error for operating across different threads.
Private Sub ButtonCreate_Click(sender As Object, e As EventArgs) Handles ButtonCreate.Click
bTextEmpty = False
ListView1.Items.Clear()
ResultPictureBox.Image = Nothing
If ListBox1.SelectedIndex > -1 Then
For Each Item As Object In ListBox1.SelectedItems
Dim ItemSelected = CType(Item("Path"), String)
Dim myTempFile As Boolean = File.Exists(ItemSelected + "\Words.txt")
If myTempFile = False Then
'when we load the form we first call the code to count words in all files in a directory
'lets check if the folder exists
If (Not System.IO.Directory.Exists(ItemSelected)) Then
Dim unused = MsgBox("The archive " + ItemSelected.Substring(ItemSelected.Length - 5, Length) + " was not found",, Title)
Exit Sub
Else
Call CreateWordList(ItemSelected)
End If
End If
'if the words file is empty we cant create a cloud so exit the sub
If bTextEmpty = True Then Exit Sub
'then we fill the wordcloud and Listview from the created textfile
Call CreateMyCloud(ItemSelected + "\Words.txt")
Next
Else
Dim unused = MsgBox("You have to choose an Archive to create the Word Cloud",, Title)
End If
Size = New Drawing.Size(1400, 800)
End Sub
I put above code in a Private Sub and called it from my BackgroundWorker. The error occured at this line: If ListBox1.SelectedIndex > -1 Then
After trying the suggested code I get the same error again:
Public Sub CreateMyCloud(ByVal sourcePDF As String)
Dim WordsFreqList As New List(Of WordsFrequencies)
For Each line As String In File.ReadLines(sourcePDF)
Dim splitText As String() = line.Split(","c)
If splitText IsNot Nothing AndAlso splitText.Length = 2 Then
Dim wordFrq As New WordsFrequencies
Dim freq As Integer
wordFrq.Word = splitText(0)
wordFrq.Frequency = If(Integer.TryParse(splitText(1), freq), freq, 0)
WordsFreqList.Add(wordFrq)
End If
Next
If WordsFreqList.Count > 0 Then
' Order the list based on the Frequency
WordsFreqList = WordsFreqList.OrderByDescending(Function(w) w.Frequency).ToList
' Add the sorted items to the listview
WordsFreqList.ForEach(Sub(wf)
error -> ListView1.Items.Add(New ListViewItem(New String() {wf.Word, wf.Frequency.ToString}, 0))
End Sub)
End If
Dim wc As WordCloudGen = New WordCloudGen(600, 400)
Dim i As Image = wc.Draw(WordsFreqList.Select(Function(wf) wf.Word).ToList, WordsFreqList.Select(Function(wf) wf.Frequency).ToList)
ResultPictureBox.Image = i
End Sub
Should look something more like:
Private Sub ButtonCreate_Click(sender As Object, e As EventArgs) Handles ButtonCreate.Click
If ListBox1.SelectedItems.Count > 0 Then
Dim data As New List(Of Object)
For Each Item As Object In ListBox1.SelectedItems
data.Add(Item)
Next
bTextEmpty = False
ListView1.Items.Clear()
ResultPictureBox.Image = Nothing
BackgroundWorker1.RunWorkerAsync(data)
Else
MessageBox.Show("You have to choose an Archive to create the Word Cloud",, Title)
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim data As List(Of Object) = DirectCast(e.Argument, List(Of Object))
For Each Item As Object In data
Dim ItemSelected = CType(Item("Path"), String)
Dim myTempFile As Boolean = File.Exists(ItemSelected + "\Words.txt")
If myTempFile = False Then
'when we load the form we first call the code to count words in all files in a directory
'lets check if the folder exists
If (Not System.IO.Directory.Exists(ItemSelected)) Then
MessageBox.Show("The archive " + ItemSelected.Substring(ItemSelected.Length - 5, Length) + " was not found",, Title)
Exit Sub
Else
Call CreateWordList(ItemSelected)
End If
End If
'if the words file is empty we cant create a cloud so exit the sub
If bTextEmpty = True Then Exit Sub
'then we fill the wordcloud and Listview from the created textfile
Call CreateMyCloud(ItemSelected + "\Words.txt")
Next
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Size = New Drawing.Size(1400, 800)
End Sub
To fix the error in your second, edited post:
WordsFreqList.ForEach(Sub(wf)
ListView1.Invoke(Sub()
ListView1.Items.Add(New ListViewItem(New String() {wf.Word, wf.Frequency.ToString}, 0))
End Sub)
End Sub)
You cannot simply access controls outside of the current thread. You first need to call the Invoke method on the target control and then pass a delegate containing the instructions intended to modify the control outside of the current thread. See this article on MSDN on how to do this: https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls?view=netframeworkdesktop-4.8

increment text box name by 1 in a loop

ok so I've done numerous searches and come up with the following code to increment up the text box name, but it just does not work. The text box is within a tab control does this matter? I've tried to reference it within the tab control but no joy.
Public Cpv_Coeffs As New List(Of Decimal)
Dim i As Integer
For i = 0 To 6
'Cpv_Coeffs.Add(txt_Cp_Coef_A1.Text) 'this line works fine
Cpv_Coeffs.Add(Me.Controls("txt_Cp_Coef_A" & 1).Text)
Next i
i just get a null reference exception
where am I going wrong?
If you want it to work, no matter which container the control is in, then use the Controls.Find() fucntion. It can recursively search for the control no matter how deeply nested it is:
Public Cpv_Coeffs As New List(Of Decimal)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ctlName As String
For i As Integer = 0 To 6
ctlName = "txt_Cp_Coef_A" & i
Dim ctl As Control = Me.Controls.Find(ctlName, True).FirstOrDefault
If Not IsNothing(ctl) AndAlso TypeOf ctl Is TextBox Then
Dim tb As TextBox = DirectCast(ctl, TextBox)
Dim dcml As Decimal
If Decimal.TryParse(tb.Text, dcml) Then
Cpv_Coeffs.Add(dcml)
Else
MessageBox.Show("Value: " & tb.Text, "Invalid Decimal")
End If
Else
MessageBox.Show("Name: " & ctlName & vbCrLf & "Could Not Find Control, or it was not a TextBox.", "Error")
End If
Next i
End Sub
The above example is verbose, but it shows you exactly where all the failure points could occur.

Calculate cost of several items with tax and a discount

Can anyone help with this school task I have
The task is to ask the user for items and the cost of the items until they chose to stop. Then combine all the costs and take 20% VAT and 10% off from 2 randomly selected items.
Here is the code I have so far (I have 2 buttons and a listbox)
Public Class Form1
Dim CurrentA As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Items(CurrentA) As String
Dim Coins(CurrentA) As Single
Dim Stay As String
CurrentA = 0
Do Until CurrentA = 20
Items(CurrentA) = InputBox("Please Enter The Item")
Coins(CurrentA) = InputBox("Please Enter The Cost Of The Item")
Stay = InputBox("Type Yes If More Items or Type No if no More")
Stay = Stay.ToLower
If Stay = "yes" Then
End If
If Stay = "no" Then
Exit Do
End If
ListBox1.Items.Add(Items(CurrentA) & " " & Coins(CurrentA))
CurrentA += 1
Loop
End Sub
End Class
First, a few comments on the code you presented.
Dim CurrentA As Integer
'An Integers default value is zero, I don't see why this is a class level variable
'always declare variables with as narrow a scope as possible
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim Items(CurrentA) As String 'Declares an Array of Type String with an Upper Bound of 0
'Upper Bound is the highest index in the array
'Arrays start with index 0
'So your array will have 1 element at index 0
Dim Coins(CurrentA) As Single
Dim Stay As String
CurrentA = 0 'unnecessary because the default of CurrentA is already 0, but OK for clarity because it could have been changed elsewhere
'This is behaving like a console application with the code repeating in a loop.
'In Windows Forms it would be more likely to do this in a button click event (btnAddItem)
Do Until CurrentA = 20
'On the first iteration CurrentA = 0
'On the second iteration CurrentA = 1 - this exceeds the size of your array
'and will cause an index out of range error
Items(CurrentA) = InputBox("Please Enter The Item")
'With Option Strict on you must change the input to a Single
Coins(CurrentA) = CSng(InputBox("Please Enter The Cost Of The Item"))
Stay = InputBox("Type Yes If More Items or Type No if no More")
Stay = Stay.ToLower 'Good! The user might no follow directions exactly
If Stay = "yes" Then
'This is kind of silly because it does nothing
End If
'Lets say I say no on the first iteration
'This avoids the index out of range error but
'nothing is added to the list because you Exit the loop
'before adding the item to the ListBox
If Stay = "no" Then
Exit Do
End If
ListBox2.Items.Add(Items(CurrentA) & " " & Coins(CurrentA))
CurrentA += 1
Loop
End Sub
We could use arrays but not knowing how many items will be added means either making the array bigger than needed or using Redim Preserve on every addition. A much better choice is a List(Of T). They work a bit like arrays but we can just add items without the ReDim stuff.
Private lstCost As New List(Of Single)
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'Pretend this button is called btnAdd, and you have 2 test boxes
lstCost.Add(CSng(TextBox2.Text))
'The $ introduces an interpolated string. It is a step up form String.Format
ListBox2.Items.Add($"{TextBox1.Text} - {CSng(TextBox2.Text):C}") 'The C stands for currency
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Pretend this button is called btnTotal
'Dim total As Single = (From cost In lstCost
' Select cost).Sum
Dim total As Single = lstCost.Sum
Label1.Text = total.ToString("C") 'C for Currency
End Sub

How To Clear CheckedListBox From Previous Selection - VB.NET

Okay so I have a checkedlistbox that when a user selects a item it will print out that item into word. That works perfect. However, I want to give the user the option to not select anything at all, but when the user does not select an item in the checkboxlist, it still prints out the previous selected item into MS word.
Below is my code:
Private Sub ExportContactOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportContactOkButton.Click
Dim i As Integer
Dim array_Counter_Contacts As Integer
Dim array_size_Contacts As Integer
array_Counter_Contacts = 0
For i = 0 To ExportContactCheckedListBox.Items.Count() - 1
If ExportContactCheckedListBox.GetItemCheckState(i) = CheckState.Checked Then
array_size_Contacts = UBound(SelectedContacts)
ReDim Preserve SelectedContacts(array_size_Contacts + 1)
SelectedContacts(array_Counter_Contacts) = ExportContactCheckedListBox.Items(i)
If Search.debugging = True Then
MsgBox(ExportContactCheckedListBox.Items(i))
End If
array_Counter_Contacts += 1
ExportContactCheckedListBox.Items(i) = ExportContactCheckedListBox.Items(i).ToString.Replace("'", "''")
If array_Counter_Contacts = 1 Then
ContactNames = "" & ExportContactCheckedListBox.Items(i) & ""
Else
ContactNames = String.Concat(ContactNames & "', '" & ExportContactCheckedListBox.Items(i) & "")
End If
End If
Next
If Search.debugging = True Then
MsgBox(ContactNames)
End If
sConnection.Close()
Dispose()
Me.Close()
End Sub
I have tried remove, clear, and I even tried this
Dim i As Integer
For i = 0 To ExportContactCheckedListBox.CheckedIndices.Count - 1
ExportContactCheckedListBox.SetItemChecked(ExportContactCheckedListBox.CheckedIndices(0), False)
Next i
But nothing is working. Can anyone help me? All I want is to be able to have the checkedlistbox forget or clear the checked item after the "OK" button is pressed and the text has already been printed into word.
Use a List(Of String) to store the selection and, of course remember, to reinitialize the list when you hit the ExportContactOkButton
' Declared at the form level....
Dim SelectedContacts as List(Of String)
.....
Private Sub ExportContactOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportContactOkButton.Click
Dim i As Integer
SelectedContacts = new List(Of String)()
For i = 0 To ExportContactCheckedListBox.Items.Count() - 1
If ExportContactCheckedListBox.GetItemCheckState(i) = CheckState.Checked Then
SelectedContacts.Add(ExportContactCheckedListBox.Items(i))
.....
End If
Next
End Sub
In this way, every time you hit the ExportContactOKButton you reinitialize your list of contacts, loop through the checked items, add the checked one to your list.
A List(Of String) is better because you don't need to know in advance how many items your user selects in the CheckedListBox and continuosly resize the array. You simply add new items to it. And you could always use it like an array
Dim Dim array_Counter_Contacts = SelectedContacts.Count
For i = 0 to array_Counter
Console.WriteLine(SelectedContacts(i))
Next