For some odd reason I am getting an error when I am using my loop to display the elements of my array. I can't seem to understand what it is I am doing or not doing right. This is the code so far. This is not for a class, I am learning myself.
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private strExams(49, 2) As String
Dim count As Integer = 0
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim strStudent As String = txtStudent.Text
Dim strTest As String = txtTest.Text
Dim strScore As String = txtScore.Text
If count <= 49 Then
strExams(count, 0) = strStudent
strExams(count, 1) = strTest
strExams(count, 2) = strScore
count += 1
End If
txtStudent.Text = String.Empty
txtTest.Text = String.Empty
txtScore.Text = String.Empty
txtStudent.Focus()
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim intHighRow As Integer = strExams.GetUpperBound(0)
Dim intHighCol As Integer = strExams.GetUpperBound(1)
Dim intR As Integer
Dim intC As Integer
Do While intC <= intHighCol
intR = 0
Do While intR <= intHighRow
lstMessage.Items.Add(strExams(intR, intC))
intR += 1
Loop
intC += 1
Loop
End Sub
This is the error I am getting when I click my display button.
An unhandled exception of type 'System.ArgumentNullException' occurred in System.Windows.Forms.dll
Additional information: Value cannot be null.
Try this. This makes much more sense to me. The reason you're getting a null error is that you haven't filled everything up in your array and your listbox can't list null items. So a workaround would be to enumerate only items that already have values, thus, just loop until the last value of count.
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim intR As Integer
lstMessage.Items.Clear()
Do While intR < count
lstMessage.Items.Add(strExams(intR, 0) & " - " & strExams(intR, 1) & " - " & strExams(intR, 2))
intR += 1
Loop
End Sub
Related
Hello im having trouble with my code !
we are asked to organize a list of names from a text.txt file the make them show up into a lits box (got that part down :) ) . then from the list box we are asked to create an array and sort that array (using our own sorting method) and organize the names using a button in assending order and another button organizing the array in decending order. the results from the orders names should appear in another list box .
i have gotten only the last name in the list to show up in the second list box but my code has no errors it just wont order the names properly! Help!!!!!
here is my code :)
Public Class FileSort
Dim sr As IO.StreamReader = IO.File.OpenText("C:\Users\Inspiron 15\documents\visual studio 2010\Projects\assigment4 EL\assigment4 EL\names.txt")
Structure names
Dim c As Integer
Dim fullname As String
End Structure
Dim allNames(99) As names
Private Sub btnName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnName.Click
Do While sr.Peek <> -1
Name = sr.ReadLine
LstNames.Items.Add(Name & " ")
Loop
sr.Close()
End Sub
Private Sub bubbelsort(ByRef names() As System.String, ByVal c As Integer)
c = 0
names(c) = sr.ReadLine()
c = c * 1
For c = 1 To 99 Step +1 '~~~ Addding (Z to A) to the the Listbox
lstOrderedNames.Items.Add(Name & "")'
Next
End Sub
Private Sub BtnAssend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAssend.Click
Dim names(99) As String
Dim c As Integer
c = 0
Dim A As Integer
A = 99
names(c) = sr.ToString
c = c + 1
For c = 1 To 99 Step +1 '~~~ Addding (Z to A) to the the Listbox
lstOrderedNames.Items.Add(Name & "")
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDessend.Click
Dim names(99) As String
Dim c As Integer
c = 0
Dim A As Integer
A = 99
names(c) = sr.ToString
names(A) = sr.ToString
A = A - 1
For A = 99 To 0 Step -1 '~~~ Addding (Z to A) to the the Listbox
lstOrderedNames.Items.Add(Name & "")
Next
End Sub
enter image description here
Since your problem is the sorting algorithm (if I understand this correctly).
At first we need an array.
Dim arr(ListBox1.Items.Count - 1) As String
For i As Integer = 0 To arr.Length - 1
arr(i) = CStr(ListBox1.Items(i))
Next
Next the sorting algorithm. Since you wanted to go with BubbleSort:
Private Sub StringBubbleSort(arr As String)
For i As Integer = 0 To arr.Length - 1
For j As Integer = 0 To arr.Length - 2 - i
If String.Compare(arr(j), arr(j + 1)) > 0 Then
Dim temp As String = arr(j)
arr(j) = arr(i)
arr(i) = temp
End If
Next
Next
End Sub
Then you use this function and copy the array to your second ListBox.
StringBubbleSort(arr)
ListBox2.Items.AddRange(arr)
String.Compare: https://msdn.microsoft.com/de-de/library/84787k22(v=vs.110).aspx
you could use linq
ListBox1.Items.Add("Battle")
ListBox1.Items.Add("Cattle")
ListBox1.Items.Add("apple")
ListBox2.DataSource = (From l In ListBox1.Items
Select l Order By l Ascending).ToList
The application should store the pay codes and rates in a two-dimensional array. It should also display the pay codes from the array in a list box. The btnCalc_Click procedure should display the gross pay, using the number of hours worked and the pay rate corresponding to the selected code.
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Const strPROMPT As String = "Please enter the amount of hours"
Const strTITLE as string = "Hours Amount"
Private strPayGrades(,) As String = {{"A07", "8.50"},
{"A10", "8.75"},
{"B03", "9.25"},
{"B24", "9.90"},
{"C23", "10.50"}}
Dim strHours As String
Dim dblGross As Double
Dim dblHours As Double
Dim dblGradeWage As Double
Dim intR As Integer
Dim intIndex As Integer
Dim strGradeWage As String
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
' fills list box with codes and then selects the first item
For intR = 0 To 4 Step 1
lstCodes.Items.Add(strPayGrades(intR, 0))
Next intR
lstCodes.SelectedIndex = 0
End Sub
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
' calculates and displays the gross pay
strHours = InputBox(strPROMPT, strTITLE)
If strHours Like "##" Or strHours Like "#" Then
Double.TryParse(strHours, dblHours)
Integer.TryParse(lstCodes.SelectedIndex.ToString, intIndex)
strGradeWage = strPayGrades(intR + intIndex, 1)
Double.TryParse(strGradeWage, dblGradeWage)
dblGross = dblHours * dblGradeWage
lblGross.Text = dblGross.ToString("c0")
Else
MessageBox.Show("Invalid format please try again",
"Try Again",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
strHours = InputBox(strPROMPT, strTITLE)
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub lstCodes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstCodes.SelectedIndexChanged
lblGross.Text = String.Empty
End Sub
End Class
change strGradeWage = strPayGrades(0 + intIndex, 1)
strGradeWage = strPayGrades(0 + intIndex, 1)
looks like that was the problem
So i'm working on a project and i've hit a bit of a roadblock. i'm trying to compare two arrays that've been populated with read-in and split text files, find the matches of individual elements (names), then populate a listbox with the resulting matches. here's the code i have so far:
Dim sr As IO.StreamReader
Dim sw As IO.StreamWriter
Dim guest As String()
Dim gift As String()
Private Sub frmTYC_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strFile1 As String
Dim strFile2 As String
Dim intCount As Integer
strFile1 = "Guests.txt"
strFile2 = "Gifts.txt"
intCount = 0
sr = IO.File.OpenText(strFile1)
Do While sr.Peek <> -1
guest = sr.ReadLine.Split(",")
lstGuests.Items.Add(guest(0))
intCount += 1
Loop
sr.Close()
intCount = 0
sr = IO.File.OpenText(strFile2)
Do While sr.Peek <> -1
gift = sr.ReadLine.Split(",")
lstGifts.Items.Add(gift(1) & " " & "gifted" & " " & gift(0))
intCount += 1
Loop
sr.Close()
End Sub
Private Sub btnList_Click(sender As Object, e As EventArgs) Handles btnList.Click
End Sub
i'm trying to compare guest(0) and gift(1) as those are the names, with guest having all the names and gift having all the names of the people in guest who gave a gift. i need the listbox to be populated on the list button click
I am trying to write my contents of my array to a file. Its an array as a structure. I seem to be having an issue. I can't seem to read the file after it has been written. The app freezes up and if I check to see if my txt file has any info in it, it locks up as well.
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Structure Person
Public strName As String
Public dblHeight As Double
Public dblWeight As Double
End Structure
Private peopleDescription(49) As Person
Dim count As Integer = 0
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If count <= 49 Then
peopleDescription(count).strName = txtName.Text
Double.TryParse(txtHeight.Text, peopleDescription(count).dblHeight)
Double.TryParse(txtWeight.Text, peopleDescription(count).dblWeight)
count += 1
End If
txtName.Text = String.Empty
txtHeight.Text = String.Empty
txtWeight.Text = String.Empty
txtName.Focus()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim outFile As IO.StreamWriter
Dim intC As Integer
outFile = IO.File.AppendText("persons.txt")
Do While intC < count
outFile.WriteLine(peopleDescription(intC))
Loop
outFile.Close()
End Sub
Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
Dim inFile As IO.StreamReader
Dim strInfo As String
If IO.File.Exists("persons.txt") Then
inFile = IO.File.OpenText("persons.txt")
Do Until inFile.Peek = -1
strInfo = inFile.ReadLine
Loop
inFile.Close()
lblMessage.Text = strInfo
Else
MessageBox.Show("Can't find the persons.txt file", "Person Data", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
I don't know what I am missing. If anyone can help I would appreciate it.
You fail to initialize intC before starting the while loop, and you're not incrementing it within the loop, so it never changes to exit it (presuming it gets inside the loop in the first place), which would make it seem to "lock up".
Set it to a starting value first, before you use it.
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim outFile As IO.StreamWriter
Dim intC As Integer = 0
outFile = IO.File.AppendText("persons.txt")
Do While intC < count
outFile.WriteLine(peopleDescription(intC))
intC += 1
Loop
outFile.Close()
End Sub
Ok so I am having problems adding elements into my 2d array. I am using 3 textboxes to allow a user to input items into my array. My problem is I cant seem to get the array to go past (0,2). I want the user to be able to add a row of inputs with each click of the add button. This is so far what I have in my code. Can anyone help? This is not for a class I am learning on my own.
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private strExams(49, 2) As String
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim strStudent As String = txtStudent.Text
Dim strTest As String = txtTest.Text
Dim strScore As String = txtScore.Text
Dim count As Integer = 0
If count <= 49 Then
strExams(count, 0) = strStudent
strExams(count, 1) = strTest
strExams(count, 2) = strScore
count += 1
End If
txtStudent.Text = String.Empty
txtTest.Text = String.Empty
txtScore.Text = String.Empty
txtStudent.Focus()
End Sub
Try this... Your count variable must be placed outside the btnAdd_Click sub or it will always reset back to 0, thus, you won't get past (0,2).
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private strExams(49, 2) As String
Dim count As Integer = 0
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim strStudent As String = txtStudent.Text
Dim strTest As String = txtTest.Text
Dim strScore As String = txtScore.Text
If count <= 49 Then
strExams(count, 0) = strStudent
strExams(count, 1) = strTest
strExams(count, 2) = strScore
count += 1
End If
txtStudent.Text = String.Empty
txtTest.Text = String.Empty
txtScore.Text = String.Empty
txtStudent.Focus()
End Sub