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
Related
I want to assign 2D array to DataGridView.
There are 2 buttons, the first is an array button which will add my inputs to the array every time I press it.
The second button is a submit button that will assign all of the values of the array to the DataGridView.
But I just can't work it out, every time I press the array button, the value is replaced by the new value I inputted.
Here's the code:
Public Class Form2
Dim array(1, 4) As String
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
For i = 0 To array.GetUpperBound(0)
DataGridView1.Rows.Add(array(i, 0), array(i, 1), array(i, 2), array(i, 3))
Next
End Sub
Private Sub btnArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArray.Click
ReDim array(1, 2)
Dim id, name As String
id = txtID.Text
name = txtName.Text
For i = 0 To array.GetUpperBound(0)
array(i, 0) = id
array(i, 1) = name
Next
End Sub
End Class
This code works for me!
Public Class Form2
Dim arrayCopy(10, 1) As String
Dim b As Integer
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
DataGridView1.Rows.Clear()
For i = 0 To b - 1
DataGridView1.Rows.Add(arrayCopy(i, 0), arrayCopy(i, 1))
Next
End Sub
Private Sub btnArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArray.Click
Dim id, name As String
id = txtId.Text
name = txtName.Text
arrayCopy(b, 0) = id
arrayCopy(b, 1) = name
b += 1
End Sub
End Class
I am basically trying to create an array to export checked items onto a word document. But I am getting an error saying
"Object reference not set to an instance of object."
and
"Referenced 'SelectedMutualFunds' has a value of 'Nothing'
Below is my code:
Public Class ExportFunds
Public SelectedMutualFunds() As String
Private Sub ExportFundOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportFundOkButton.Click
Dim i As Integer
Dim array_Counter As Integer
array_Counter = 0
For i = 0 To ExportFundCheckedListBox.Items.Count() - 1
If ExportFundCheckedListBox.GetItemCheckState(i) = CheckState.Checked Then
SelectedMutualFunds(array_Counter) = ExportFundCheckedListBox.Items(i).ToString
array_Counter += 1
End If
Next
Me.Close()
End Sub
Can someone please help me solve this issue?
You need to provide length to your string array
Public SelectedMutualFunds() As String
to the following within ExportFundOkButton_Click before you use, preferably just before the for loop.
Redim SelectedMutualFunds(ExportFundCheckedListBox.Items.Count() - 1)
You can get this down to a one-liner and fix the NullReference exception at the same time:
Private Sub ExportFundOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportFundOkButton.Click
SelectedMutualFunds = ExportFundCheckedListBox.Items.Where(Function(i) i.CheckState = CheckState.Checked).Select(Function(i) i.ToString()).ToArray()
Me.Close()
End Sub
Or, slightly longer but easier to read:
Private Sub ExportFundOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportFundOkButton.Click
SelectedMutualFunds = ExportFundCheckedListBox.Items.
Where(Function(i) i.CheckState = CheckState.Checked).
Select(Function(i) i.ToString()).
ToArray()
Me.Close()
End Sub
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
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
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.