How to modify a value in the array in visual basic - arrays

The following is the code which I am using for creating a two dimensional array
Public Class frmGrades
Private Score As Dictionary(Of String, String) = New Dictionary(Of String, String)
Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click
Static intNumber As Integer
ReDim Preserve Score(1, intNumber)
Score(0, intNumber) = txtStudent.Text
Score(1, intNumber) = txtGrade.Text
hsbStudent.Maximum = intNumber
hsbStudent.Value = intNumber
intNumber = intNumber + 1
txtGrade.Clear()
txtStudent.Clear()
txtStudent.Focus()
End Sub
Private Sub hsbStudent_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hsbStudent.Scroll
txtStudent.Text = Score(0, hsbStudent.Value)
txtGrade.Text = Score(1, hsbStudent.Value)
End Sub
Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
txtStudent.Text = Score(0, hsbStudent.Minimum)
txtGrade.Text = Score(1, hsbStudent.Minimum)
End Sub
Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
txtStudent.Text = Score(0, hsbStudent.Maximum)
txtGrade.Text = Score(1, hsbStudent.Maximum)
End Sub
**Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click
If Score.ContainsKey(txtStudent.Text) Then
Score.Item(txtStudent.Text) = txtGrade.Text
Else
Score.Add(txtStudent.Text, txtGrade.Text)
End If
End Sub
End Class**
Now I would like to edit the grade text box which should also change the grade in the array. Any idea on how could this be done.

UPDATE:
OK, I see the code has a little more now and the problem is more clear as well as the need for a different approach. I rewrote your code. See if this works for you:
Public Class frmGrades
Public Class StudentGrade
Public Name As String
Public Grade As String
End Class
Private Score As List(Of StudentGrade) = New List(Of StudentGrade)
Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click
AddStudent()
End Sub
Private Sub hsbStudent_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hsbStudent.Scroll
Update(hsbStudent.Value - 1) 'lists are zero-based
End Sub
Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
Update(0)
End Sub
Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
Update(hsbStudent.Maximum - 1)
End Sub
Private Sub AddStudent()
Dim nm As New StudentGrade
nm.Name = txtStudent.Text
nm.Grade = txtGrade.Text
Score.Add(nm)
hsbStudent.Minimum = 1
hsbStudent.Maximum = Score.Count
hsbStudent.Value = Score.Count
txtGrade.Clear()
txtStudent.Clear()
txtStudent.Focus()
End Sub
Private Sub Update(i As Integer)
txtStudent.Text = Score(i).Name
txtGrade.Text = Score(i).Grade
End Sub
Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click
Dim index As Integer = -1, cnt As Integer = 0
For Each nm As StudentGrade In Score
If nm.Name.ToLower = txtStudent.Text.ToLower Then
index = cnt
Exit For
End If
cnt += 1
Next
If index = -1 Then
AddStudent()
Else
Score(index).Name = txtStudent.Text
Score(index).Grade = txtGrade.Text
hsbStudent.Value = index + 1
End If
End Sub
End Class
With this code you can expand the class StudentGrade to contain whatever you need to store. The list, contrary to dictionary, allow you to use index values as well.

Related

variable does not increment

Public Class Form2
Dim i As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
Private Sub btnEnterPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterPatient.Click
ReDim Preserve Names(0 To i)
Names(i) = txtPatientName.Text
ReDim Preserve Heights(0 To i)
Heights(i) = txtPatientHeight.Text
ReDim Preserve Weights(0 To i)
Weights(i) = txtPatientWeight.Text
i = i + 1
End Sub
End Class
This is my code for when I click a button to enter data into three arrays but when I click the button the i does not increment. Also if I don't enter anything into the Height or Weight textbox and press the button I get the error: Conversion from string "" to type 'Integer' is not valid.
What is the problem here?
Thanks

helping with the right code for a 15 shuffle game

I am trying to build a 15 number shuffle game in VB.
Later on, I will have to submit the 15 pieces to a full image.
At the mintime, I tries to build it with numbers. it was all working fine until I got this messege:
Conversion from string "" to type 'Double' is not valid.
I got it in line:
If butt2.Content = "" Then
here is my code:
<pre lang="vb">Class MainWindow
Private Sub btn1_Click(sender As Object, e As RoutedEventArgs) Handles btn1.Click
'2,4
checkBtn(btn1, btn4)
checkBtn(btn1, btn2)
chechSolved()
End Sub
Private Sub btn2_Click(sender As Object, e As RoutedEventArgs) Handles btn2.Click
'1,3,5
checkBtn(btn2, btn1)
checkBtn(btn2, btn3)
checkBtn(btn2, btn5)
chechSolved()
End Sub
Private Sub btn3_Click(sender As Object, e As RoutedEventArgs) Handles btn3.Click
'2,6
checkBtn(btn3, btn2)
checkBtn(btn3, btn6)
chechSolved()
End Sub
Private Sub btn4_Click(sender As Object, e As RoutedEventArgs) Handles btn4.Click
'1,5,7
checkBtn(btn4, btn1)
checkBtn(btn4, btn5)
checkBtn(btn4, btn7)
chechSolved()
End Sub
Private Sub btn5_Click(sender As Object, e As RoutedEventArgs) Handles btn5.Click
'2,4,6,8
checkBtn(btn5, btn2)
checkBtn(btn5, btn4)
checkBtn(btn5, btn6)
checkBtn(btn5, btn8)
chechSolved()
End Sub
Private Sub btn6_Click(sender As Object, e As RoutedEventArgs) Handles btn6.Click
'3,5,9
checkBtn(btn6, btn3)
checkBtn(btn6, btn5)
checkBtn(btn6, btn9)
chechSolved()
End Sub
Private Sub btn7_Click(sender As Object, e As RoutedEventArgs) Handles btn7.Click
'4,8
checkBtn(btn7, btn4)
checkBtn(btn7, btn8)
chechSolved()
End Sub
Private Sub btn8_Click(sender As Object, e As RoutedEventArgs) Handles btn8.Click
'5,7,9
checkBtn(btn8, btn5)
checkBtn(btn8, btn7)
checkBtn(btn8, btn9)
chechSolved()
End Sub
Private Sub btn9_Click(sender As Object, e As RoutedEventArgs) Handles btn9.Click
'6,8
checkBtn(btn9, btn6)
checkBtn(btn9, btn8)
chechSolved()
End Sub
Sub checkBtn(ByVal butt1 As Button, ByVal butt2 As Button)
If butt2.Content = "" Then
butt2.Content = butt1.Content
butt1.Content = ""
End If
End Sub
Sub chechSolved()
If btn1.Content = "1" And btn2.Content = "2" And btn3.Content = "3" And btn4.Content = "4" And btn5.Content = "5" And btn6.Content = "6" And btn7.Content = "7" And btn8.Content = "8" And btn9.Content = "" Then
MsgBox("הצלחת")
End If
End Sub
Sub shuffle()
Dim a(8), i, j, RN As Integer
Dim flag As Boolean
flag = False
i = 1
a(j) = 1
Do While i <= 8
Randomize()
RN = CInt(Int((8 * Rnd()) + 1))
For j = 1 To i
If (a(j) = RN) Then
flag = True
Exit For
End If
Next
If flag = True Then
flag = False
Else
a(i) = RN
i = i + 1
End If
Loop
btn1.Content = a(1)
btn2.Content = a(2)
btn3.Content = a(3)
btn4.Content = a(4)
btn5.Content = a(5)
btn6.Content = a(6)
btn7.Content = a(7)
btn8.Content = a(8)
btn9.Content = ""
End Sub
Private Sub btnSH_Click(sender As Object, e As RoutedEventArgs) Handles btnSH.Click
shuffle()
End Sub
End Class</pre>
it would help a lot if you look at it.
The compiler might be having a problem comparing the string to the btn.Content because you set the Content to an Integer type (btn1.Content = a(1))

Access level on VB.NET

I have created a system and im new to vb.net. It is working fine. The problem is, I need to restrict the main page from 1.) Users and 2.) Admin.
For example, After logging in, Admin will proceed to the main page
And if users logs in, they will be directed to a different page
I'm still new to vb.net (like 6 weeks ago) and i am using microsoft access as my database. A lot of code I find online is very complex and technical. I just need simple vb.net code
Any help will be appreciated, thank you!!
i have posted my code for my login form, so you guys can further understand my coding:
Public Class Form1
Dim loginerror As String
Public Function login()
Dim DBconn As New ADODB.Connection
Dim user As New ADODB.Recordset
Dim Username As String
Dim userDB As String
Dim passDB As String
Dim UserFound As Boolean
DBconn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = '" & Application.StartupPath & "\LoginDB.mdb'")
user.Open("UserTable", DBconn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
UserFound = False
login = False
Username = "Username = '" & txtuser.Text & "'" '
Do
user.Find(Username)
If user.BOF = False And user.EOF = False Then
userDB = user.Fields("Username").Value.ToString
passDB = user.Fields("Password").Value.ToString
If userDB <> txtuser.Text Then
user.MoveNext()
Else
UserFound = True
If passDB = txtpass.Text Then
user.Close()
DBconn.Close()
Return True
Else
loginerror = "Invalid Password"
user.Close()
DBconn.Close()
Return False
End If
End If
Else
loginerror = "Invalid Username"
user.Close()
DBconn.Close()
Return False
End If
Loop Until UserFound = True
user.Close()
DBconn.Close()
Return False
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If login() = True Then
Welcome.Show()
Me.Close()
Else
MessageBox.Show(loginerror, "Login Message")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AcceptButton = Button1
Me.Show()
Application.DoEvents()
txtuser.Focus()
End Sub
Private Sub txtpass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.TextChanged
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub txtuser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuser.TextChanged
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
End Sub
End Class
Just in case here is my coding for my main form:
Public Class Mainform
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
Case Windows.Forms.DialogResult.No
e.Cancel = True
End Select
End Sub
Private Sub Mainform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WorkTableAdapter.Fill(Me.LoginDBDataSet.Work)
Me.AssetTableAdapter.Fill(Me.LoginDBDataSet.Asset)
Me.HistoryTableAdapter.Fill(Me.LoginDBDataSet.History)
Me.InventoryTableAdapter.Fill(Me.LoginDBDataSet.Inventory)
Me.Equipment1TableAdapter.Fill(Me.LoginDBDataSet.Equipment1)
End Sub
Private Sub mainform_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Escape Then Me.Close()
End Sub
Private Sub Equipment1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Equipment1BindingNavigatorSaveItem.Click
Me.Validate()
Me.Equipment1BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.LoginDBDataSet)
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Mainform_Load(Me, New System.EventArgs)
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
Form1.Show()
Me.Dispose()
Me.Close()
End Sub
Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click
End Sub
Private Sub IDLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub TabPage5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage5.Click
End Sub
Private Sub Equipment_NameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub HistoryDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
End Sub
Private Sub InventoryDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
End Sub
Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
End Sub
Private Sub Machine_IDTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub _WO_CompletedLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub _WO_CompletedCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub _WO_CompletedCheckBox_CheckedChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Machine_IDTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Machine_IDTextBox1.TextChanged
End Sub
Private Sub _WO_CompletedCheckBox_CheckedChanged_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _WO_CompletedCheckBox.CheckedChanged
End Sub
End Class
you need to determine if the user is administrator or not (usually your user table should have a role attached to the user e.g. administration, employee, etc.)
something like this when you try to log in
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If login() = True and UserRole='Administrator' Then
Welcome.Show()
Me.Close()
elseif login() = True then
WelcomeUser.Show()
Me.Close()
Else
MessageBox.Show(loginerror, "Login Message")
End If
End Sub

Using listbox to access 2D array VB

So I am trying to use a listbox to access variables in my 2D array. I am unsure what is the best way to do this. Right now I am using the selectedindex of the listbox to access it but I am only seeing the second dimension being show in my message box. Any help would be appreciated.
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
lstInventory.Items.Add("Hand Grenade")
lstInventory.Items.Add("9mm Ammo Box")
lstInventory.Items.Add(".40 Ammo Box")
lstInventory.SelectedIndex = 0
End Sub
Dim dblInventoryItem(,) As Double = {{10.99, 5},
{5.99, 10},
{8.99, 8}}
Private Sub btnCheck_Click(sender As System.Object, e As System.EventArgs) Handles btnCheck.Click
Dim intRow As Integer = lstInventory.SelectedIndex
MessageBox.Show(dblInventoryItem(intRow, 1).ToString)
End Sub
End Class
You don't want to use a multi-dimensional array here.
The OOP way to do this would be to define inventory item as its own class or structure, and use instances to both populate your list box and store the inventory item price and quantity.
Something like:
Public Class Form1
Structure InventoryItem
Public Sub New(ByVal itmName As String, ByVal itmPrice As Double, ByVal itmQty As Integer)
Name = itmName : Price = itmName : Quantity = itmQty
End Sub
Dim Name As String
Dim Price As Double
Dim Quantity As Integer
End Structure
Dim invItems As New List(Of InventoryItem)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
invItems.Add(New InventoryItem("Hand Grenade", 10.99, 5))
'' ... Add your additional items here
For Each i As InventoryItem In invItems
lstInventory.Items.Add(i.Name)
Next
End Sub
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
Dim invItem As InventoryItem = invItems(lstInventoryItems.SelectedIndex)
MessageBox.Show(invItem.Name & "," & invItem.Price & "," & invItem.Quantity)
End Sub
End Class

VB.NET CreateInstance of structure

I wrote the following code:
Public Class Form1
Private Structure udtThing
Dim SomeText As String
Dim SomeElements() As String
Public Shared Function CreateInstance() As udtThing
Dim result As New udtThing
result.SomeText = String.Empty
ReDim result.SomeElements(2)
result.SomeElements(0) = String.Empty
result.SomeElements(1) = String.Empty
result.SomeElements(2) = String.Empty
Return result
End Function
End Structure
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim nThings() As udtThing
nThings = Array.CreateInstance(GetType(udtThing), 10)
End Sub
End Class
I partly works, nThings becomes an array of 11 udtThings.
But .SomeElements is not redimmed to 3 strings of String.Empty, but it is "Nothing" instead.
Does anybody see where I went wrong?
Thank you very much!
By design, a Redim is required. Array.CreateInstance() isn't going to perform that operation, it can't guess what size is required. You'll have to help:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim nThings(10) As udtThing
For ix As Integer = 0 To UBound(nThings)
nThings(ix) = udtThing.CreateInstance()
Next
End Sub

Resources