I'm super stuck on this one guys and need some help.
I have a multiple arrays Name, age, height, weight which is created on another form where all their data is entered into the arrays but then I need to go to another form where there is text box that I enter one of the name into and click search and then in a text box or list box it will show age, height, weight corresponding to the index of that name search.
My main problem is trying to get the arrays data over to another form and the other problem is trying to get the computer to search through the array know what index it is and show the corresponding data to that index for the other arrays. Been stuck on this for very long now could some please give me suggestions on how to do this it would be heavily appreciated, Thank you.
This is the code/ arrays I have so far
Imports System.IO
Public Class DataEntry
Dim Surname(200)
Dim Firstname(200)
Dim Age(200) As String
Dim HeightA(200) As String
Dim Weight(200)
Dim index As Integer
Public filepath As String = "c:\Patients\All Patients File"
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
Dim di As DirectoryInfo = New DirectoryInfo("c:\Patients")
If di.Exists Then
MsgBox("File is Already There")
txtSur.Enabled = True
txtWeight.Enabled = True
txtHeight.Enabled = True
txtFirst.Enabled = True
txtAge.Enabled = True
btnAddPatient.Enabled = True
btnClear.Enabled = True
btnFileShow.Enabled = True
btnEdit.Enabled = True
btnSave.Enabled = True
txtReader.Enabled = True
Else
di.Create()
MsgBox("File is Already There")
txtSur.Enabled = True
txtWeight.Enabled = True
txtHeight.Enabled = True
txtFirst.Enabled = True
txtAge.Enabled = True
btnAddPatient.Enabled = True
btnClear.Enabled = True
btnFileShow.Enabled = True
btnEdit.Enabled = True
btnSave.Enabled = True
txtReader.Enabled = True
End If
End Sub
Private Sub btnAddPatient_Click(sender As Object, e As EventArgs) Handles btnAddPatient.Click
index = index + 1
lblNum.Text = index
Surname(index) = txtSur.Text
Firstname(index) = txtFirst.Text
Age(index) = txtAge.Text
Weight(index) = txtWeight.Text
HeightA(index) = txtHeight.Text
Dim textAppend As String
textAppend = txtSur.Text & ", " + txtFirst.Text & ", " + txtAge.Text & ", " + txtHeight.Text & "mm" & ", " + txtWeight.Text & "kg" & "."
Try
File.AppendAllText(filepath, textAppend)
MsgBox("Patient Added Successfully")
Catch ex As Exception
MsgBox("Error Adding Patient")
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnFileShow.Click
Dim objreader As New System.IO.StreamReader("c:\Patients\All Patients File")
txtReader.Text = objreader.ReadToEnd
objreader.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
Dim result As Integer = MessageBox.Show("Are You Sure You Want To Clear", "ALERT", MessageBoxButtons.YesNoCancel)
If result = DialogResult.Cancel Then
MessageBox.Show("Cancel pressed")
ElseIf result = DialogResult.No Then
MessageBox.Show("Not Clearing")
ElseIf result = DialogResult.Yes Then
MessageBox.Show("Clearing")
End If
Dim objwriter As New System.IO.StreamWriter("c:\Patients\All Patients File")
'adding text from textbox to text fil
objwriter.Write("")
objwriter.Close()
Array.Clear(Surname, 0, Surname.Length)
Array.Clear(Firstname, 0, Firstname.Length)
Array.Clear(Age, 0, Age.Length)
Array.Clear(Weight, 0, Weight.Length)
Array.Clear(HeightA, 0, HeightA.Length)
index = 0
txtAge.Text = ""
txtFirst.Text = ""
txtHeight.Text = ""
txtSur.Text = ""
txtWeight.Text = ""
txtReader.Text = ""
End Sub
Private Sub DataEntry_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtSur.Enabled = False
txtWeight.Enabled = False
txtHeight.Enabled = False
txtFirst.Enabled = False
txtAge.Enabled = False
btnAddPatient.Enabled = False
btnClear.Enabled = False
btnFileShow.Enabled = False
btnEdit.Enabled = False
btnSave.Enabled = False
txtReader.Enabled = False
End Sub
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
If txtReader.Text = "" Then
MsgBox("No Infomation To Edit")
Else
txtReader.Focus()
End If
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
If txtReader.Text = "" Then
MsgBox("Nothing To Save")
Else
Dim objwriter As New System.IO.StreamWriter("c:\Patients\All Patients File")
'adding text from textbox to text file
objwriter.Write(txtReader.Text)
objwriter.Close()
MsgBox("Saved Edit")
End If
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
Me.Hide()
End Sub
End Class
And this is the form I have to get the arrays over to for the search
Public Class SearchPatient
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
End Sub
Related
I want to make a little quiz about me using visual studio. My goal is to have 10 questions and 4 choices each. I've tried to stack buttons and then change 10 labels at different times and it's getting a little complicated... using arrays or/and loops how could i make this more efficient? This is what I have
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
Form1.Visible = False
Button2.Visible = False
Button3.Visible = False
Button4.Visible = False
Button5.Visible = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RadioButton4.Checked = True Then
Label3.Text = "1/10"
Label4.Text = "Nice! That was Correct!
Great job!"
Label4.ForeColor = Color.LimeGreen
End If
If Label3.Text = "1/10" Then
Button2.Visible = True
End If
If RadioButton1.Checked = True Or RadioButton2.Checked = True Or RadioButton3.Checked = True Then
Label4.ForeColor = Color.DarkRed
Label4.Text = "Oh no! that was incorrect!
Try Again!"
End If
Button2.Text = "next"
End Sub
Private Sub Button2_click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = "What is Luke Lopez's favorite color?"
RadioButton4.Checked = False
RadioButton1.Text = "Purple"
RadioButton2.Text = "Burgundy"
RadioButton3.Text = "Turqoise"
RadioButton4.Text = "Brown"
If RadioButton1.Checked = True Then
Label3.Text = "2/10"
Label4.Text = "Nice! That was Correct!
Great job!"
Label4.ForeColor = Color.LimeGreen
End If
If Label3.Text = "2/10" Then
Button4.Visible = True
End If
If RadioButton3.Checked = True Or RadioButton2.Checked = True Or RadioButton4.Checked = True Then
Label4.ForeColor = Color.DarkRed
Label4.Text = "Oh no! that was incorrect!
Try Again!"
End If
Button2.Text = "check"
Button4.Text = "next"
If Label3.Text = "2/10" Then
Button3.Text = "next"
Button4.Text = "Check"
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Button3.Visible = False
Label1.Text = "What is Luke Lopez's shoe size?"
RadioButton1.Checked = False
RadioButton1.Text = "7"
RadioButton2.Text = "8.5"
RadioButton3.Text = "7.5"
RadioButton4.Text = "8"
Button3.Visible = False
If RadioButton2.Checked = True Then
Label3.Text = "3/10"
Label4.Text = "Nice! That was Correct!
Great job!"
Label4.ForeColor = Color.LimeGreen
End If
If Label3.Text = "3/10" Then
Button5.Visible = True
Button4.Visible = False
Button3.Visible = False
Button2.Visible = False
Button1.Visible = False
End If
If RadioButton1.Checked = True Or RadioButton4.Checked = True Or RadioButton3.Checked = True Then
Label4.ForeColor = Color.DarkRed
Label4.Text = "Oh no! that was incorrect!
Try Again!"
End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
End Sub
It's my first three questions and there are all the buttons and labels and radiobuttons checking and changing every click but I need this to be more efficient because I don't want to waste a lot of time and computer memory on this mini project.
In comments I talked about storing your data in a text file. This is what my text file looks like. Each record is on a single line and each field is separated by a pipe character |. I chose a pipe because you won't have any of that character in the text of your questions and answers.
1|Largest planet?|Jupiter|Venus|Saturn|Mars|1
2|Smallest planet?|Earth|Uranus|Mercury|Neptune|3
3|Comes closest to Earth?|Mars|Venus|Mercury|Jupiter|2
4|What planet is called the red planet?|Venus|Mars|Saturn|Neptune|2
5|What planet is furthest from Earth?|Saturn|Jupiter|Uranus|Neptune|4
6|How many planets in our solar system?|7|8|9|10|2
7|How many planets between Earth and Sun?|1|2|3|4|2
8|Which planet is said to have rings?|Jupiter|Uranus|Saturn|Neptune|3
You have been working with classes all along because a Form is a class. Classes can have Properties, methods and Events among other things. Now to the class, QuestionAnswer. The properties are pretty straight forward. The Sub New creates a new instance of the class and uses parameters to set all the properties of the class.
In the Form.Load FillQuestionList is called. This method uses the File class in System.IO (need to add an Imports for this). ReadAllLines returns an array of the all the lines in the file. We can loop through these lines and then split each line into fields. The little c after the "|" tells compiler that you intend a Char not a String. Now we can call the Sub New of QuestionAnswer providing all the properties from the parts of the line. This new instance is then added to the QuestionList so we can use it in the program.
Next, the Load event calls DisplayQuestion. This method will also be called from the btnNext click event. We get the index we want to display from the form level variable CurrentQuestionIndex. See how handy the class is to set the values we need. Loop through all the radio buttons clearing the Tag from the last question and clearing the check. The file provided the number of the correct answer. This is stored in the radio button's Tag property.
When the user clicks btnAnswer we get which radio button was selected and check the Tag property to see if the answer was correct. If yes, increment Score and display the score.
The btnNext.Click simply checks if we have reached the end of the list before incrementing the index and displaying the next question.
Public Class QuestionGame
Private QuestionList As New List(Of QuestionAnswer)
Private CurrentQuestionIndex As Integer
Private Score As Integer
Private Sub QuestionGame_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
FillQuestionList()
DisplayQuestion()
End Sub
Private Sub DisplayQuestion()
Dim QA = QuestionList(CurrentQuestionIndex)
lblQuestion.Text = QA.Question
RadioButton1.Text = QA.Answer1
RadioButton2.Text = QA.Answer2
RadioButton3.Text = QA.Answer3
RadioButton4.Text = QA.Answer4
For Each rb In Controls.OfType(Of RadioButton)
rb.Tag = ""
rb.Checked = False
Next
Select Case QA.Correct
Case 1
RadioButton1.Tag = "Correct"
Case 2
RadioButton2.Tag = "Correct"
Case 3
RadioButton3.Tag = "Correct"
Case 4
RadioButton4.Tag = "Correct"
End Select
End Sub
Private Sub FillQuestionList()
Dim lines = File.ReadAllLines("C:\Users\xxxxxxxx\Questions.txt")
For Each line In lines
Dim parts = line.Split("|"c)
QuestionList.Add(New QuestionAnswer(CInt(parts(0)), parts(1), parts(2), parts(3), parts(4), parts(5), CInt(parts(6))))
Next
End Sub
Private Sub btnAnswer_Click(sender As Object, e As EventArgs) Handles btnAnswer.Click
Dim rb = Controls.OfType(Of RadioButton)().FirstOrDefault(Function(r) r.Checked = True)
If rb IsNot Nothing AndAlso rb.Tag.ToString = "Correct" Then
Score += 1
lblScore.Text = Score.ToString
MessageBox.Show("Correct!")
btnAnswer.Enabled = False 'The user can't increase his score by clicking answer several times
Else
MessageBox.Show("Sorry, wrong answer")
End If
btnNext.Enabled = True
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
btnNext.Enabled = False 'user can't move to next question until he answers current question
If CurrentQuestionIndex = QuestionList.Count - 1 Then
MessageBox.Show("This is the last question")
Else
CurrentQuestionIndex += 1
DisplayQuestion()
End If
End Sub
End Class
Public Class QuestionAnswer
Public Property QuestionNumber As Integer
Public Property Question As String
Public Property Answer1 As String
Public Property Answer2 As String
Public Property Answer3 As String
Public Property Answer4 As String
Public Property Correct As Integer
Public Sub New(Num As Integer, Ques As String, Ans1 As String, Ans2 As String, Ans3 As String, Ans4 As String, Cor As Integer)
QuestionNumber = Num
Question = Ques
Answer1 = Ans1
Answer2 = Ans2
Answer3 = Ans3
Answer4 = Ans4
Correct = Cor
End Sub
End Class
You can save questions, options and answers to a database and then read them to a DataTable, every time you want to show the question, just use a DataRow from the DataTable. That means you don't need to edit questions like:
Use 'Label.TextChanged' event to handle 'Label3.Text':
Private Sub Label3_TextChanged(sender As Object, e As EventArgs) Handles Label3.TextChanged
Select Case Label3.Text
Case "1/10"
Button2.Visible = True
Case "2/10"
Button4.Visible = True
Button3.Text = "next"
Button4.Text = "Check"
Case "3/10"
...
...
End Select
End Sub
Use 'If ... Else ...' statement. For Example:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'...
If RadioButton1.Checked = True Then
Label3.Text = "2/10"
Label4.Text = "Nice! That was Correct! Great job!"
Label4.ForeColor = Color.LimeGreen
Else
Label4.ForeColor = Color.DarkRed
Label4.Text = "Oh no! that was incorrect! Try Again!"
End If
'If RadioButton3.Checked = True Or RadioButton2.Checked = True Or RadioButton4.Checked = True Then
' Label4.ForeColor = Color.DarkRed
' Label4.Text = "Oh no! that was incorrect! Try Again!"
'End If
'...
End Sub
In the code below I am attempting to change the tag of an existing control that was created in the array 'pictureboxes(9, 9)'. When I try to do this, I get the error 'Object reference not set to an instance of an object.'. This is done in the checkdata sub, near the bottom of the code with the comment 'Object reference not set to an instance of an object.'.
The string that is passed through to the sub is a long string of numbers, and the pictureboxes are placed into a layoutpanel if that is helpful.
I understand what this error means, I am aware that pictureboxes(i, j) is = nothing when breakpointed; I just don't know how to fix it :s
Any help is greatly appreciated and hopefully I will be quick to answer any comments/answers.
Thank you
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Net
Public Class Form1
'0 as default tile, 1 as clicked, 3 as set mine (host perspective)
Dim tiles() As Integer = {0}
Public pictureboxes(9, 9) As PictureBox
Dim flagged() As Integer
Dim clicked As Integer()
Dim columns As Integer = 10
Dim rows As Integer = 10
Dim placedMinesCount As Integer = 0
Dim formattedTag As String()
Public turn As Boolean = False
Dim stringToSend As String
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Byte = 0 To 9
For j As Byte = 0 To 9
pictureboxes(i, j) = New PictureBox
pictureboxes(i, j).Height = 60
pictureboxes(i, j).Width = 60
pictureboxes(i, j).ImageLocation = "0.png"
pictureboxes(i, j).Tag = "0|" & i & ", " & j
AddHandler pictureboxes(i, j).Click, AddressOf Tile_Click
Dim column As Integer = j
Dim row As Integer = i
Panel.Controls.Add(pictureboxes(i, j), column, row)
Next
Next
If Login.isHost = True Then
Me.Text = "Set your mines (10)"
turn = True
ElseIf Login.isHost = False Then
Me.Text = "Await your turn"
btnEndTurn.Visible = False
btnEndTurn.Enabled = False
End If
End Sub
Protected Sub Tile_Click(ByVal sender As Object, ByVal e As EventArgs)
formatSenderTag(sender.tag)
If Login.isHost = True Then
Dim clickAction As String = formattedTag(0)
Select Case clickAction
Case "0"
If placedMinesCount < 10 Then
placedMinesCount = placedMinesCount + 1
sender.imagelocation = "3.png"
sender.tag = "3"
Me.Text = "Set your mines (" & 10 - placedMinesCount & ")"
ElseIf placedMinesCount >= 10 Then
MsgBox("You have placed all of your 10 Mines")
End If
Case "2"
MsgBox("You cannot set a mine here")
Case "3"
placedMinesCount = placedMinesCount - 1
Me.Text = "Set your mines (" & 10 - placedMinesCount & ")"
sender.imagelocation = "0.png"
sender.tag = "0"
End Select
ElseIf Login.isHost = False Then
Dim clickAction As String = formattedTag(0)
Select Case clickAction
Case "0"
sender.tag = "1"
sender.imagelocation = "1.png"
Case "3"
MsgBox("Game Over")
Case "2"
MsgBox("Already Clicked")
End Select
End If
End Sub
Private Sub formatSenderTag(ByVal sender As String)
'split into array
'element 0 as TILE TYPE
'element 1 as TILE LOCATION
formattedTag = sender.Split(New String() {"|"}, StringSplitOptions.None)
End Sub
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
End
End Sub
Private Sub btnEndTurn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEndTurn.Click
turn = False
gridtostring()
senddata()
End Sub
Private Sub gridtostring()
For i As Byte = 0 To 9
For j As Byte = 0 To 9
formatSenderTag(pictureboxes(i, j).Tag & "|")
stringToSend = stringToSend & formattedTag(0)
Next
Next
End Sub
Private Sub senddata()
'***SEND STUFF
'Assuming you have a textbox with the data you want to send
If (Not String.IsNullOrEmpty(stringToSend)) Then
Dim data() As Byte = Encoding.ASCII.GetBytes(stringToSend)
Login.sendingClient.Send(data, data.Length)
End If
End Sub
Public Sub checkdata(ByVal data As String)
If Not data = stringToSend Then
'Dim loopcount As Integer = 0
For i As Byte = 0 To 9
For j As Byte = 0 To 9
Dim loopcount As Integer = (i.ToString & j.ToString) + 1
'Dim pineapple As String = pictureboxes(i, j).Tag
'pictureboxes(i, j).Tag = GetChar(data, 3)
pictureboxes(i, j).Tag = GetChar(data, loopcount) & "|" & i & ", " & j '***Object reference not set to an instance of an object.***
formatSenderTag(pictureboxes(i, j).Tag)
pictureboxes(i, j).ImageLocation = formattedTag(0)
pictureboxes(i, j).ImageLocation = "0.png"
'Panel.Controls(pictureboxes(i, j).Tag) = GetChar(data, 3) '.Tag = GetChar(data, 3)
Next
Next
End If
End Sub
End Class
***AND BELOW IS THE OTHER FORM WHICH CALLS CHECKDATA()
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Net
Public Class Login
Dim Port As Integer = 8123
Private Const broadcastAddress As String = "255.255.255.255"
Public receivingClient As UdpClient
Public sendingClient As UdpClient
Dim sendAddress As String
Dim ServerMode As Boolean = False
Public isHost As Boolean = true
Dim returndata As String
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub ComboWANLAN_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboWANLAN.SelectedIndexChanged
If ComboWANLAN.Text = "Online (WAN)" Then
txtSendAddress.Enabled = True
ElseIf ComboWANLAN.Text = "Offline (LAN)" Then
txtSendAddress.Enabled = False
End If
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
'***START PORT LISTENING/SENDING AND NETWORKING STUFFS
Port = txtPort.Text
InitializeSender()
InitializeReceiver()
End Sub
Private Sub btnHost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHost.Click
'***START PORT LISTENING/SENDING AND NETWORKING STUFFS
Port = txtPort.Text
InitializeSender()
InitializeReceiver()
isHost = True
Me.Hide()
Form1.Show()
End Sub
Private Sub InitializeSender()
If ComboWANLAN.Text = "Offline (LAN)" Then
sendingClient = New UdpClient(broadcastAddress, Port)
'Use broadcastAddress for sending data locally (on LAN), otherwise you'll need the public (or global) IP address of the machine that you want to send your data to
ElseIf ComboWANLAN.Text = "Online (WAN)" Then
sendAddress = txtSendAddress.Text
sendingClient = New UdpClient(sendAddress, Port)
'Use broadcastAddress for sending data locally (on LAN), otherwise you'll need the public (or global) IP address of the machine that you want to send your data to
End If
sendingClient.EnableBroadcast = True
End Sub
Private Sub InitializeReceiver()
receivingClient = New UdpClient(Port)
ThreadPool.QueueUserWorkItem(AddressOf Receiver) 'Start listener on another thread
End Sub
Private Sub Receiver()
Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, port) 'Listen for incoming data from any IP on the specified port
Do While True 'Notice that i've setup an infinite loop to continually listen for incoming data
Dim data() As Byte
data = receivingClient.Receive(endPoint)
If Form1.turn = False Then
returndata = Encoding.ASCII.GetString(data) 'Recived data as string
Form1.checkdata(returndata)
End If
Loop
End Sub
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
End
End Sub
End Class
***STACK TRACE
at Minesweeper.Form1.checkdata(String data) in c:\users\harry\documents\visual studio 2010\Projects\Minesweeper\Minesweeper\Form1.vb:line 139
at Minesweeper.Login.Receiver() in C:\Users\Harry\Documents\Visual Studio 2010\Projects\Minesweeper\Minesweeper\Login.vb:line 71
at Minesweeper.Login._Lambda$__1(Object a0) in C:\Users\Harry\Documents\Visual Studio 2010\Projects\Minesweeper\Minesweeper\Login.vb:line 61
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
Note: Screwed up with the spacings in some places
This code does not give an error with the data you gave:
checkdata("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
Public Sub checkdata(ByVal data As String)
If Not data = stringToSend Then
'Dim loopcount As Integer = 0
For i As Byte = 0 To 9
For j As Byte = 0 To 9
Dim loopcount As Integer = (i.ToString & j.ToString) + 1
'Dim pineapple As String = pictureboxes(i, j).Tag
'pictureboxes(i, j).Tag = GetChar(data, 3)
pictureboxes(i, j).Tag = GetChar(data, loopcount) & "|" & i & ", " & j '***Object reference not set to an instance of an object.***
formatSenderTag(pictureboxes(i, j).Tag)
pictureboxes(i, j).ImageLocation = formattedTag(0)
pictureboxes(i, j).ImageLocation = "0.png"
'Panel.Controls(pictureboxes(i, j).Tag) = GetChar(data, 3) '.Tag = GetChar(data, 3)
Next
Next
End If
End Sub
Looking at your stack trace and code, you are missing the instantiation of Form1 in your login class. I think this can be the problem.
Try adding this to the beginning of your Login class:
Dim Form1 as New Form1
I'm making a Homework/Revision program for my coursework. I'm following this tutorial http://www.homeandlearn.co.uk/NET/nets12p9.html on how to make a database but theres something wrong with the my update button (btnUpdate)
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
txtFirstName.Text = ds.Tables("Users").Rows(inc).Item(1) = txtFirstName.Text
txtSurname.Text = ds.Tables("Users").Rows(inc).Item(2) = txtSurname.Text
txtUsername.Text = ds.Tables("Users").Rows(inc).Item(3) = txtUsername.Text
txtPassword.Text = ds.Tables("Users").Rows(inc).Item(4) = txtPassword.Text
txtClass.Text = ds.Tables("Users").Rows(inc).Item(5) = txtClass.Text
txtAdmin.Text = ds.Tables("Users").Rows(inc).Item(6) = txtAdmin.Text
txtHWP.Text = ds.Tables("Users").Rows(inc).Item(7) = txtHWP.Text
MessageBox.Show("Data updated")
End Sub
Public Class AdminEditUsers 'this is the whole code
Dim MaxRows As Integer
Dim inc As Integer
Dim con As New OleDb.OleDbConnection 'THE CONNECTION OBJECT
Dim dbProvider As String 'HOLDS THE PROVIDER
Dim dbSource As String 'HOLDS THE DATA SOURCE
Dim MyDocumentsFolder As String 'HOLDS THEDOCUMENTS FOLDER
Dim TheDatabase As String 'HOLDS THE DATABASE NAME
Dim FullDatabasePath As String 'HOLDS THE DATABASE PATH
Dim ds As New DataSet 'HOLDS A DataSet OBJECT
Dim da As OleDb.OleDbDataAdapter 'HOLDS A DataAdapter OBJECT
Dim sql As String 'HOLDS A SQL STRING
Private Sub AdminEditUsers_load(sender As Object, e As EventArgs) Handles Me.Load
'SET UP THE PROVIDER
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
'SET THE DATABASE AND WHERE THE DATABASE IS
TheDatabase = "\Visual Studio 2015/NEW_database.accdb"
MyDocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
FullDatabasePath = MyDocumentsFolder & TheDatabase
'SET THE DATA SOURCE
dbSource = "Data Source = " & FullDatabasePath
'SET THE CONNECTION STRING
con.ConnectionString = dbProvider & dbSource
'OPEN THE DATABASE
con.Open()
'STORE THE SQL STRING
sql = "Select * FROM tbl_user"
'PASS THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
da = New OleDb.OleDbDataAdapter(sql, con)
'Fill the dataset with records from the database table
da.Fill(ds, "Users")
'CLOSE THE DATABASE
con.Close()
'Counts how many rows are in the table
MaxRows = ds.Tables("Users").Rows.Count
inc = -1
End Sub
Private Sub NavigateRecords()
txtFirstName.Text = ds.Tables("Users").Rows(inc).Item(1)
txtSurname.Text = ds.Tables("Users").Rows(inc).Item(2)
txtUsername.Text = ds.Tables("Users").Rows(inc).Item(3)
txtPassword.Text = ds.Tables("Users").Rows(inc).Item(4)
txtClass.Text = ds.Tables("Users").Rows(inc).Item(5)
txtAdmin.Text = ds.Tables("Users").Rows(inc).Item(6)
txtHWP.Text = ds.Tables("Users").Rows(inc).Item(7)
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If inc <> MaxRows - 1 Then
inc = inc + 1
NavigateRecords()
Else
MessageBox.Show("No More Rows")
End If
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
If inc > 0 Then
inc = inc - 1
NavigateRecords()
ElseIf inc = -1 Then
MessageBox.Show("No Records Yet")
ElseIf inc = 0 Then
MessageBox.Show("First Record")
End If
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
If inc <> MaxRows - 1 Then
inc = MaxRows - 1
NavigateRecords()
End If
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
If inc <> 0 Then
inc = 0
NavigateRecords()
End If
End Sub
Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
End Sub
Private Sub btnCommit_Click(sender As Object, e As EventArgs) Handles btnCommit.Click
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
txtFirstName.Text = ds.Tables("Users").Rows(inc).Item(1) = txtFirstName.Text
txtSurname.Text = ds.Tables("Users").Rows(inc).Item(2) = txtSurname.Text
txtUsername.Text = ds.Tables("Users").Rows(inc).Item(3) = txtUsername.Text
txtPassword.Text = ds.Tables("Users").Rows(inc).Item(4) = txtPassword.Text
txtClass.Text = ds.Tables("Users").Rows(inc).Item(5) = txtClass.Text
txtAdmin.Text = ds.Tables("Users").Rows(inc).Item(6) = txtAdmin.Text
txtHWP.Text = ds.Tables("Users").Rows(inc).Item(7) = txtHWP.Text
MessageBox.Show("Data updated")
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
End Sub
End Class
Where are you actually updating the database?
Read one more paragraph in that tutorial:
Close down your programme, then run it again. Click the Next Record button to move to the first record. It will still be "John Smith". The data you updated has been lost! So here, again, is why:
"Changes are made to the DataSet, and NOT to the Database"
The tutorial shows you how to persist those changes to the database:
da.Update(ds, "AddressBook")
Edit: It would also appear that you're making a mistake on lines like this:
txtFirstName.Text = ds.Tables("Users").Rows(inc).Item(1) = txtFirstName.Text
This may behave differently in different languages. In C# for example, I think that the result of an assignment is the value being assigned, so something like this might work. But in VB there are semantic contextual differences when using the same operator. The assignment operator (=) is also the comparison operator, depending on the context.
So I suspect that this line of code is comparing the latter two items, and assigning the result of the comparison (True or False) to the first item.
Stick with what the tutorial is showing you, just assign the value to the data:
ds.Tables("Users").Rows(inc).Item(1) = txtFirstName.Text
There's no need to also try to assign the value back to the text box where you got the value, since logically that value is already there.
Using picture boxes overlaid onto an image as seen below.
Click for Form Layout
When the form loads if the student attends the class then the picturebox becomes a green tick. If the student does not attend the class the picturebox becomes blank.
Using SQL I can query the database to return all the classes that a specific student attends.
These classes are stored in an array StudentClass(n)
There are 50 classes on the timetable stored in an array AttendsClass(n). All with a default value of False.
If the student attends a class then the specific attendsclass in the array becomes True.
Once the form is loaded the user can click on a picturebox to select that class and the corresponding attendsclass in the array becomes true.
Finally the user saves the form, inserting the classes back into the database.
The problem I have is in the Sub Form4_Load at the end, I currently have to repeat the same block of code 50 times. That simply checks if the student attends the class then sets the picture box = to an image of a tick on the GUI (_new.jpg).
enter code here
If AttendsClass(1) = True Then
PictureBox1.Image = My.Resources._new
End If
If AttendsClass(2) = True Then
PictureBox2.Image = My.Resources._new
End If
If AttendsClass(3) = True Then
PictureBox3.Image = My.Resources._new
End If
If AttendsClass(4) = True Then
PictureBox4.Image = My.Resources._new
End If
Is it possible to put this into a loop to shorten my code. The code does work but its slow and lengthy so any improvements would be helpful.
Many Thanks Alex Currie
A Level Computing Student
Full Code:
enter code here
Imports System.Data.OleDb
Public Class Form4
Public con As New OleDbConnection
Public ds As New DataSet
Public da As OleDbDataAdapter
Public cb As OleDbCommandBuilder
Public constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" & Application.StartupPath & "\wma.accdb"
Public AttendsClass(50) As Boolean
Public StdID As Integer = Form2.DataGridView1.SelectedRows(0).Cells("StudentID").Value
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Forename As String = Form2.DataGridView1.SelectedRows(0).Cells("Forename").Value
Dim Surname As String = Form2.DataGridView1.SelectedRows(0).Cells("Surname").Value
Student.Text = "Student: " & Forename & " " & Surname
If Not con.State = ConnectionState.Open Then
con.ConnectionString = constring
con.Open()
End If
da = New OleDbDataAdapter("SELECT * FROM StudentClass WHERE StudentId = " & StdID, con)
da.Fill(ds, "Class")
Dim Maxrow As Integer = ds.Tables("Class").Rows.Count
Dim StudentClass(Maxrow) As Integer
For n = 1 To 50
AttendsClass(n) = False
Next
For n = 1 To Maxrow
StudentClass(n) = ds.Tables("Class").Rows(n - 1).Item(1)
For a = 0 To 50
If StudentClass(n) = a Then
AttendsClass(a) = True
End If
Next
Next
If AttendsClass(1) = True Then
PictureBox1.Image = My.Resources._new
End If
If AttendsClass(2) = True Then
PictureBox2.Image = My.Resources._new
End If
If AttendsClass(3) = True Then
PictureBox3.Image = My.Resources._new
End If
If AttendsClass(4) = True Then
PictureBox4.Image = My.Resources._new
End If
End Sub
Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click
If PictureBox1.Image Is Nothing Then
PictureBox1.Image = My.Resources._new
AttendsClass(1) = True
Else
PictureBox1.Image = Nothing
AttendsClass(1) = False
End If
End Sub
Private Sub PictureBox2_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox2.Click
If PictureBox2.Image Is Nothing Then
PictureBox2.Image = My.Resources._new
AttendsClass(2) = True
Else
PictureBox2.Image = Nothing
AttendsClass(2) = False
End If
End Sub
Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
If PictureBox4.Image Is Nothing Then
PictureBox4.Image = My.Resources._new
AttendsClass(4) = True
Else
PictureBox4.Image = Nothing
AttendsClass(4) = False
End If
End Sub
Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
If PictureBox3.Image Is Nothing Then
PictureBox3.Image = My.Resources._new
AttendsClass(3) = True
Else
PictureBox3.Image = Nothing
AttendsClass(3) = False
End If
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Not con.State = ConnectionState.Open Then
con.ConnectionString = constring
con.Open()
End If
Dim cmd As New OleDb.OleDbCommand
cmd.Connection = con
'Deletes Existing Records To Be rewritten'
cmd.CommandText = "DELETE * FROM StudentClass WHERE StudentID=" & StdID
cmd.ExecuteNonQuery()
For n = 1 To 50
If AttendsClass(n) = True Then
cmd.CommandText = "INSERT INTO StudentClass (StudentID, ClassID) VALUES (" & StdID & "," & n & ")"
cmd.ExecuteNonQuery()
End If
Next
Me.Dispose()
MsgBox("Updated Successfully", MsgBoxStyle.Information, "WMA")
con.Close()
End Sub
End Class
Working example:
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles PictureBox1.Click, ...., PictureBox50.Click
Dim pic As PictureBox = CType(Controls(CType(sender, PictureBox).Name), PictureBox)
'now you have clicked picturebox
'you can change image by pic.Image = ...
'if you want picturebox index, use line below:
Dim picidx As Integer = CInt(CType(sender, PictureBox).Name.Replace("PictureBox", ""))
End Sub
VB2010Ex.
When adding a data to an array I keep getting the error 'System.IndexOutOfRangeException' the Array is declared to bound of 200 and at the data I'm trying to add is at 6 + 1, 6 is the variable count, in the code.
Public Class FormEvents
Dim ArrayEvents(200) As String
Dim Count As Integer
Private Sub FormEvents_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Events As String = "C:\Users\Andrew prince\Desktop\Education\College\Computing\Controlled assesment\Program\Program files\Events.txt"
Dim ObjReader As New StreamReader(Events)
ArrayEvents = ObjReader.ReadLine().Split(",")
UpdateInfo()
ObjReader.Close()
TxtEventNo.Enabled = False
BtnAdd.Enabled = False
End Sub
Sub UpdateInfo()
TxtEventNo.Text = ArrayEvents(Count)
TxtEventType.Text = ArrayEvents(Count + 1)
TxtEventDistance.Text = ArrayEvents(Count + 2)
End Sub
Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
Count = Count + 3
checkInfo()
End Sub
Private Sub BtnPrev_Click(sender As Object, e As EventArgs) Handles BtnPrev.Click
Count = Count - 3
checkInfo()
End Sub
Sub Createvent()
Dim eventNo As String
eventNo = Count / 3
TxtEventNo.Text = eventNo
TxtEventDistance.Text = ""
TxtEventType.Text = ""
BtnNext.Enabled = False
BtnPrev.Enabled = False
BtnAdd.Enabled = True
End Sub
Sub checkInfo()
If Count <= 0 Then Count = 0
If ArrayEvents(Count) = "" Then Createvent() Else UpdateInfo()
End Sub
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
If TxtEventDistance.Text.Length > 0 And TxtEventType.Text.Length > 0 Then AddToArray()
End Sub
Sub AddToArray()
ArrayEvents(Count) = TxtEventNo.Text
ArrayEvents(Count + 1) = TxtEventType.Text 'error occurs here in the code
ArrayEvents(Count + 2) = TxtEventDistance.Text
Enable()
End Sub
Sub Enable()
BtnAdd.Enabled = False
BtnNext.Enabled = True
BtnPrev.Enabled = True
End Sub
End Class
ArrayEvents is probably no longer 200 in length after you set it in the load method to:
ArrayEvents = ObjReader.ReadLine().Split(",")