NullReferenceException was unhandled VB.NET - vb.net

I am trying to assign to an array a record that has to be split into more elements for search criteria reasons. For example, if criteria is set to First Name, then search for matching keyword in array element 0 because that's where first name will be placed after the currently selected record is Split().
Source for the search form:
Public Class frmSearch
Dim arrayCurrentRecord(6) As String
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.DialogResult = DialogResult.Cancel
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
'fill array with list box items
Dim arraySearch(frmMain.numberOfRecords) As String
For i = 0 To frmMain.numberOfRecords - 1
arraySearch(i) = frmMain.lstListBox.Items(i)
Next i
If rbtnFirstName.Checked = True Then
For i = 0 To UBound(arraySearch)
arrayCurrentRecord = arraySearch(i).Split(" ")
If InStr(arrayCurrentRecord(0), txtSearch.Text) = True Then
lstSearch.Items.Add(arraySearch(i))
End If
Next i
MsgBox("search complete")
End If
End Sub
End Class

Changing For i = 0 To UBound(arraySearch) to
For i = 0 To UBound(arraySearch) - 1 fixed the issue, but it won't find anything just says that search was complete.

Changing line
If InStr(arrayCurrentRecord(0), txtSearch.Text) = True
to
If InStr(arrayCurrentRecord(0), txtSearch.Text)
resolved the problem.

Related

Create a undo/redo textboxes algorithm in VB.Net

I would love to create a type algorithm Undo/Redo in VB.Net, first at least i want to make the undo.
if we have the example Textbox1.Text = a,b,c
then change to = a,b,c,e
then change to = a,b,c,f
when I click on undo I want to display (that is, the value before it. before after a,b, c, f, was a, b, c,e
a,b,c,e
and when I click once again I want to display
a,b,c
that is, an undo / redo algorithm of added values
I first created a global list.
Public Module Globals
Public Undo As New List(Of String)
End Module
if Textboxes value is not exists in the list, then I will add it.
Private Sub Button1(sender As Object, e As EventArgs) Handles Button1.Click
If Undo IsNot TextBox1.Text Then
Undo.Add(TextBox1.Text)
End If
End Sub
hey well here's the tricky part
Private Sub Undo_Click(sender As Object, e As EventArgs) Handles Undo.Click
Dim i as integer = Undo.Count
While TextBox1.Text IsNot Undo(i)
TextBox1.Text = Undo(i)
End While
End Sub
For i As Integer = Undo.Count To 0 Step -1
TextBox1.Text = Undo(i)
Next
unfortunately no such loop works in this case.
Try this:
Private Undos As List(Of String) = New List(Of String)()
Private AddUndo As Boolean = True
Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
If AddUndo = True Then
Undos.Add(textBox1.Text)
End If
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If Undos.Count <> 0 Then
AddUndo = False
If Undos.Count > 1 Then
textBox1.Text = Undos(Undos.Count - 2)
Undos.RemoveAt(Undos.Count - 1)
Else
textBox1.Text = ""
Undos.RemoveAt(0)
End If
AddUndo = True
End If
End Sub

How to print certain indexes based off of certain check boxes being checked

Below I have an array and in my design I have a check list box with 10 options. For example, if boxes 1 and 2 were checked, I would only want to print Indexes 0 and 1 ONLY. I have a button that prints all of the array members (included below) and that is what I want to make print only selected items. I have tried using a switch but that file had gotten corrupted and I am lost. Thank you. (Language is VB)
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btn1.Click
Dim strDecimal(9) As String
strDecimal(0) = FormatPercent(0.0146175)
strDecimal(1) = FormatPercent(0.0345324585)
strDecimal(2) = FormatPercent(0.09324543575)
strDecimal(3) = FormatPercent(0.07346475)
strDecimal(4) = FormatPercent(0.0772346615)
strDecimal(5) = FormatPercent(0.42234234654)
strDecimal(6) = FormatPercent(0.6246264664)
strDecimal(7) = FormatPercent(0.4524642234)
strDecimal(8) = FormatPercent(0.6876543534)
strDecimal(9) = FormatPercent(0.6876543534)
For num As Integer = 0 To strDecimal.Length - 1
listArrays.Items.Add(strDecimal(num))
Next
End Sub
Private Sub clearList()
listArrays.Items.Clear()
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
clearList()
End Sub
Assuming you're using a CheckedListBox and want to know which items are Checked:
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
For Each itm As String In listArrays.CheckedItems
Debug.Print(itm)
Next
End Sub

'StartIndex cannot be less than zero.' when trying to load a form in vb

I have recently made some changes to one of the subs within a form of my vb project and I am now getting an error whilst attempting to load the form. I have little idea what start index or Parameter name are with relation to a form, so don't where to start looking to solve this issue. Here is the error message I get:
An error occurred creating the form. See Exception.InnerException for details. The error is: StartIndex cannot be less than zero.
Parameter name: startIndex
The Sub that I have made changes to is the last sub in the code below called TextBox1changed_textchanged. I have added all but the last 5 lines as to limit the characters that can be put into the text box. This new code is edited from another forum page so I assume it should work correctly, but I can't be sure as the form will no longer run.
Public Class frmAddQuantity
Private Sub frmFieldMaster_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
cmbStateRateSumRatio.Items.Clear()
cmbStateRateSumRatio.Items.Insert(0, "State")
cmbStateRateSumRatio.Items.Insert(1, "Rate")
cmbStateRateSumRatio.Items.Insert(2, "Sum")
cmbStateRateSumRatio.Items.Insert(3, "Ratio")
End Sub
Private Sub bttAddQUAtoDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttAddQUAtoDatabase.Click
AddQuantity("'" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & cmbStateRateSumRatio.Text & "'")
InitialiseAll()
frmFieldMaster.InitialiseNewParameter()
Me.Close()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text <> "Enter SI Units" Then
If cmbStateRateSumRatio.SelectedIndex <> -1 Then
bttAddQUAtoDatabase.Enabled = True
End If
End If
End Sub
Private Sub cmbStateRateSumRatio_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbStateRateSumRatio.SelectedIndexChanged
If TextBox1.Text <> "Enter Quantity" Then
If TextBox2.Text <> "Enter SI Units" Then
bttAddQUAtoDatabase.Enabled = True
End If
End If
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890^-*()."
Dim Text As String = TextBox2.Text
Dim Letter As String
Dim SelectionIndex As Integer = TextBox2.SelectionStart
Dim Change As Integer
Letter = TextBox2.Text.Substring(SelectionIndex - 1, 1)
If Letter = "/" Then
Text = Text.Replace(Letter, "^(-")
SelectionIndex = SelectionIndex - 1
End If
Letter = TextBox2.Text.Substring(SelectionIndex - 1, 1)
If charactersAllowed.Contains(Letter) = False Then
Text = Text.Replace(Letter, String.Empty)
Change = 1
End If
TextBox2.Text = Text
TextBox2.Select(SelectionIndex - Change, 0)
If TextBox1.Text <> "Enter Quantity" Then
If cmbStateRateSumRatio.SelectedIndex <> -1 Then
bttAddQUAtoDatabase.Enabled = True
End If
End If
End Sub
End Class
Tell the debugger to stop when the exception is thrown. Debug + Exceptions, tick the Thrown checkbox for CLR exceptions.
There's a good candidate in your snippet for this exception:
Letter = TextBox1.Text.Substring(SelectionIndex - 1, 1)
You'll need to deal with the possibility that the textbox is empty or the SelectionStart property is 0. Which will always bomb your code with "StartIndex cannot be less than zero".
Private Const AllowedChars = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890^-*()."
Private Sub TxtSIUnit_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSIUnit.KeyPress
If e.KeyChar >= " "c AndAlso Not AllowedChars.Contains(e.KeyChar) Then e.Handled = True
End Sub
Private Sub TxtSIUnit_KeyPress2(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSIUnit.KeyPress
Dim selectstart
If e.KeyChar = "/" Then
e.Handled = True
selectstart = txtSIUnit.SelectionStart
txtSIUnit.SelectedText = "*^-1"
txtSIUnit.Select(selectstart + 1, 0)
End If
End Sub
I have solved the issue above using this code. Instead of using the .textchanged handle I have used .keypress which stopped the issue I was having previously. The text in the textbox was changing as the form was loading, leading to a problem with the start index of the text. Using .keypress mean only user inputs let the code run, avoiding errors on loading.

Windows Form won't pass a variable to another?

my problem is that is have 4 forms, 3 of these forms allow me to pass variables between them.. however one form the Booking form won't allow me to pass the txtTotal variable to the Confirmation Form.
All the other forms all me to do this, im not doing anything different i can see... im thinking that perhaps another part of the form is prohibiting me from passing that txtTotal to the Confirmatin form.
The following is for the Confirmation form, it should display the txtTotal in lblprice from the Booking form but shows nothing
Public Class Confirmation
Private Sub btnBack_Click(sender As System.Object, e As System.EventArgs) Handles btnBack.Click
Dim FrmPayment As New Payment
FrmPayment.Show()
Me.Hide()
End Sub
Private Sub btnHome_Click(sender As System.Object, e As System.EventArgs) Handles btnHome.Click
Dim FrmSelection As New Selection
FrmSelection.Show()
Me.Hide()
End Sub
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles lblshow.Click, lbltime.Click, lbldate.Click, lblcust.Click, lblprice.Click
End Sub
Private Sub Confirmation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'lblprice displays nothing and the rest of the labels display the correct values
lblprice.Text = Booking.txtTotal.Text
lblshow.Text = Selection.cboShowSelect.Text
lbldate.Text = Selection.cboDateSelect.Text
lbltime.Text = Selection.cboTimeSelect.Text
End Sub
End Class
Here is all the code in the Booking form if it helps
Public Class Booking
Private Sub Booking_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'labels 1,4,6 display information from the comboboxes on the Selection Form
Label1.Text = Selection.cboShowSelect.Text
Label4.Text = Selection.cboDateSelect.Text
Label6.Text = Selection.cboTimeSelect.Text
Dim i As Integer
For i = 1 To 4
cboAdult.Items.Add(i)
cboChild.Items.Add(i)
cboSenior.Items.Add(i)
cboStudent.Items.Add(i)
Next i
End Sub
Public Sub ComboBoxes_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboAdult.SelectedIndexChanged, cboChild.SelectedIndexChanged, cboSenior.SelectedIndexChanged, cboStudent.SelectedIndexChanged
'Assigned an evet handler to all of the comboboxes then calculates the price and puts in total box
Dim Totalcombo1, Totalcombo2, Totalcombo3, Totalcombo4, Price As Decimal
Dim valuecombo1 = (cboAdult.SelectedIndex + 1) 'finds position of option selected & adds one to get number of tickets
Dim valuecombo2 = (cboChild.SelectedIndex + 1)
Dim valuecombo3 = (cboSenior.SelectedIndex + 1)
Dim valuecombo4 = (cboStudent.SelectedIndex + 1)
'if the submit button is selected without there being a value selected from any combobox then error should appear, saying at least 1 ticket should be purchased.
If (cboChild.SelectedIndex = -1) Then
Totalcombo2 = 0
Else
Price = 6.5
Totalcombo2 = valuecombo2 * Price
End If
'determines the ticketprice of combobox 1
If (cboAdult.SelectedIndex = -1) Then
Totalcombo1 = 0
Else
Price = 9
Totalcombo1 = valuecombo1 * Price
End If
'determines the ticketprice of combobox 2
If (cboSenior.SelectedIndex = -1) Then
Totalcombo3 = 0
Else
Price = 6.5
Totalcombo3 = valuecombo3 * Price
End If
'determines the ticketprice of combobox 3
If (cboStudent.SelectedIndex = -1) Then
Totalcombo4 = 0
Else
Price = 6.5
Totalcombo4 = valuecombo4 * Price
End If
'determines the ticketprice of combobox 4
If (cboAdult.SelectedIndex = -1 And cboChild.SelectedIndex = -1 And cboSenior.SelectedIndex = -1 And cboStudent.SelectedIndex = -1) Then
MessageBox.Show("Please make at least one ticket selection before continuing. ")
End If
txtTotal.Text = Totalcombo1 + Totalcombo2 + Totalcombo3 + Totalcombo4
'adds the totals of the ticketprices and then inserts into the Total label
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboChild.SelectedIndexChanged
End Sub
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Dim FrmSelection As New Selection
FrmSelection.Show()
Me.Hide()
End Sub
Sub Form_OpenBooking(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If (cboChild.SelectedIndex >= 0 And (cboSenior.SelectedIndex = -1 And cboAdult.SelectedIndex = -1)) Then
MessageBox.Show("A child must be accompanied by at least one adult", "Invalid Selection")
ElseIf (txtTotal.Text > 0) Then 'if the total label is greater than zero then this means at least one ticket selection has been made
Dim Form3 As New Payment ' if above is true open Booking Form
Form3.Show()
Me.Hide()
End If
End Sub
Private Sub Resetbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
cboAdult.SelectedIndex = -1
cboChild.SelectedIndex = -1
cboSenior.SelectedIndex = -1
cboStudent.SelectedIndex = -1
txtTotal.Text = 0
End Sub
End Class
Thanks in advance!
Try this
Public frm as new frmBooking
frm.txtTotal.Text=
It should work
You need to use the actual instance of the Form that you created, not the Class name. You should also make sure that you turn on Option Strict.
Try frmBooking.txtTotal.Text instead of Booking.txtTotal.Text

vb.net array for slideshow button

Hey all, i am in the middle of trying to figure out how to go about doing this...
I have a form that i call like so:
call frmSlideShow.showWhat("1,4,8,9,11,22")
Each of the numbers represents a different image slide (slide1.png, slide4.png, etc..). The problem i am having is trying to create a "previous" and "next" button to flip through them all. Trying to figure out what number the user is on and going from there and seeing what numbers are still left from the list above that was sent, etc.
If anyone has an idea how i would go about doing that then that would be awesome! :)
UPDATE
here is the code..
Public Sub showWhat(ByVal theNumbers As String)
Dim theNums As String() = theNumbers.Split(New Char() {","c})
Dim theCurNum As String
For Each theCurNum In theNums
Debug.Print(theCurNum)
Next
End Sub
David
Put theNums string array one level up in your code along with an integer variable that keeps track of the current position in the array. Then your next button would check the upperbounds of theNums array to make sure you weren't on the last one. If you weren't, then it would increase the integer variable by one and then you could theNums(intTracker).
Public Class Form1
Dim theNums As String()
Dim intTracker As Integer = 0
Public Sub showWhat(ByVal theNumbers As String)
theNums = theNumbers.Split(New Char() {","c})
intTracker = 0
MsgBox("Currently showing " & theNums(intTracker))
If theNums.GetUpperBound(0) < 1 Then
btnNext.Enabled = False 'Only one number was passed, so disable the next button
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call showWhat("1,2,3")
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
btnNext.Enabled = True
intTracker -= 1
MsgBox("Now on " & theNums(intTracker))
If intTracker = 0 Then
btnLast.Enabled = False 'Disable the button because you're at the beginning
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
btnLast.Enabled = True
intTracker += 1
MsgBox("Now on " & theNums(intTracker))
If theNums.GetUpperBound(0) = intTracker Then
btnNext.Enabled = False 'On the last slide, so disable the next button
End If
End Sub
End Class