Index was outside the bounds of the array - VB - vb.net

Public Class Form1
Dim a As Integer
Dim b As Integer
Dim c As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
a += 1
Else : a += 0
End If
If CheckBox2.Checked = True Then
b += 1
Else : b += 0
End If
If CheckBox3.Checked = True Then
c += 1
Else : c += 0
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim max As Integer = 0
Dim d() As Integer = {a, b, c}
Dim f() As String = {"ch1", "ch2", "ch3"}
For i As Integer = 0 To 2
If max < d(i) Then
max = d(i)
Else : max = max
End If
Next
Label1.Text = f(max)
End Sub
End Class

So, in summary you're using checkboxes to increment numbers every time Button1 is clicked. These values are stored in variables a, b and c, which when Button2 is clicked, are put into an integer array. From the looks of your code you're then trying to find which one of these is the largest value and display a value from another array based off that index. The problem, of course, is that you're taking the value of d and using it as the index of f. The value of max then becomes an arbitrarily large number, easily exceeding the bounds of f which in your example only has three values in it. You might want to try it more like this:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim max As Integer = 0
Dim index as Integer = 0
Dim d() As Integer = {a, b, c}
Dim f() As String = {"ch1", "ch2", "ch3"}
For i As Integer = 0 To 2
If max < d(i) Then
max = d(i)
index = i
Else : max = max
End If
Next
Label1.Text = f(index)
End Sub
This still uses max as the comparer, but takes note of the index of the For loop when it reaches a new highest number, using that to pull out the value of f, ensuring that the value of index is never higher than i.

The values of a,b and c are being incremented ever time you click the first button depending on their checked state. Then when you loop thru and get the max of the variables you are trying to use that as an indec into the f() array. The f() array can only go from 0 to 2 but max can be any value. You cannot use max to select from the array. What are you trying to do ?

Related

Check if integer is valid bitwise enum value

is there a way to check if an integer is a valid value for an enum even if the value is bitwise?
Example:
Public Enum eFlags As Integer
None = 0
First = 1
Second = 2
Third = 4
End Enum
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim x As eFlags = eFlags.Second Or eFlags.Third
Dim y As Integer = 5
Dim res = x.GetType.IsEnum AndAlso [Enum].IsDefined(x.GetType, y)
End Sub
This returns false. I want to set the value of a property using reflection where the property is an enum and the value is integer...
Maybe this will help
<Flags> Public Enum eFlags As Integer
None = 0
First = 1
Second = 2
Third = 4
'note: flags added above will have to be Or'ed in All
All = First Or Second Or Third '<<<<<<<<<
End Enum
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim x As Integer = 14
For y As Integer = 6 To 13
Dim res As eFlags = CType(x And y, eFlags)
If eFlags.All.HasFlag(res) Then
Stop
Else
Stop
End If
Next
End Sub

Adding Multiple PictureBoxes To Array

So I have a total of 55 PictureBoxes that I am trying to add to an array. The naming of them looks like the Following:
Row1_Brick1, Row1_Brick2, up to Row1_Brick10
There is a total of 10 rows and there is 1 less brick in each row.
This is what I have thought of so far to make this work:
Dim bricks(0 To 54) As PictureBox 'Total of 55 Bricks Spread Out
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Start of Loading 55 Bricks into the bricks() Array
For i = 0 To 54
For a = 1 To 10
For am = 10 To 1 Step -1
bricks(i) = ("Row" & a & "_Brick" & am)
Next
Next
Next
End Sub
Any ideas on how to do this would be great.
I recommend a jagged array, which would look like this (note that this is 0-indexed rather than 1-indexed, as with your control names):
Dim bricks(10)() As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Set up child arrays
For i As Integer = 0 To 9
bricks(i) = New PictureBox(9-i)
'Set up each element in the array
For j As Integer = 0 To 9 - i
bricks(i)(j) = Me.Controls("Row" & i + 1 & "_Brick" & j + 1)
Next j
Next
End Sub
But if you really only want a single array, it is at least easier to set up (you might be able to get it down to a single line):
Dim bricks() As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bricks = Me.Controls.OfType(Of PictureBox)().ToArray()
End Sub
If you need to, you can put in a Where() call to limit to pictureboxes where the name matches your patter, though it would be better to put these controls into a common Panel or GroupBox you can use as the parent rather than the form. You can also use an Orderby() call to make sure the PictureBoxes are returned in the proper sequence:
Dim bricks() As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bricks = BrickPanel.Controls.
OfType(Of PictureBox)().
OrderBy(Function(pb) pb.Name). 'Naive OrderBy... 10 is before 2. I leave it to the OP to fix that part
ToArray()
End Sub
If you are unconformtalbe with the Linq functions, the trick is to increment your result array index as part of your inner loop, rather than having a separate loop by itself:
Dim bricks(54) As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim i As Integer = 0
For r As Integer = 1 To 10
For c As Integer = 1 to 11 - r
bricks(i) = Me.Controls("Row" & r & "_Brick" & c)
i+=1
Next c
Next r
End Sub
This is completely untested, because I was too lazy to create a new project and set up a form the same way you have, but it should be close.
Dim arr(54) As PictureBox
Dim index As Integer = 0
For row As Integer = 1 To 10
For col As Integer = 1 To 10 - row + 1 'Max column is based on the inverted value of the current row
arr(index) = Ctype(f.Controls("Row" & row & "_Brick" & col), PictureBox)
index += 1
Next
Next
This method of getting the control by the name explicitly will avoid any errors due to the order that the controls were added to the form.

VB.NET Help sectioning with distinct numbers

What I need is this: (A, B, C, D, etc.. are chars(name of the section))
A = 0-40
B = 41-80
C = 81-120
D = 121-160
and so on
I have a combobox containing numbers from 0 to 1040, when I select numbers such as 80, the section turns into C, which should be B because it goes inside 41-80 as seen above. As well as I choose 120, it turns into D, which should be C. etc etc. Only numbers from 1-40 works perfectly. Here is my code:
Dim counter As String
Dim mysection As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter = cmbYearAdd.SelectedItem
Dim a As Char = "A"
Dim distinct1% = 1, distinct2% = 40
For x As Integer = counter To 1040
If x >= distinct1 And x <= distinct2 Then
mysection = a
Exit For
Else
a = Chr(Asc(a) + 1)
distinct1 += 40
distinct2 += 40
End If
Next
lblSectionAdd.Text = "Section: " & mysection
End Sub
I have a label, combobox, and button.
This is because when the limits are raised the value of x is incremented when the for loop starts over.
But please don't try to fix it - this method is so awkward you shouldn't put any effort in it anymore. Simply keep the counter value fixed and divide by your section size to directly compute which section the value belongs to:
Dim counter As String
Dim mysection As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter = cmbYearAdd.SelectedItem
Dim a As Char = "A"
Dim sectionsize as Integer = 40
Dim sect as Integer
sect = Int((counter-1) / sectionsize)
mysection = Chr(Asc("A") + sect)
lblSectionAdd.Text = "Section: " & mysection
End Sub
The division result needs to be truncated to Integer, I'm using Int() for this.

Vb Write a program to print multiples of 2 and 3

For my class, i need to write a program to find multiples of 2 and 3. The code i have would get me multiples of any number inputted into the program. My problem is that nothing is showing up in the message box that i've created and i don't know why. here's the code.
Public Class form1
Private Sub Button1_Click(ByVal Sender)
Dim Number1 As Integer
Dim Number2 As Integer
Dim Multiplier As Integer
Dim Answer As Integer
Dim i As Integer
Number1 = Val(TextBox1.Text)
Number2 = Val(TextBox2.Text)
Multiplier = 1
Do While Multiplier <= 10
For i = Number1 To Number2
Answer = i * Multiplier
ListBox1.Items.Add(i & "*" & Multiplier & "=" & Answer)
Next i
Multiplier = Multiplier + 1
Loop
End Sub
End Class
Any help at all would be appreciated.
I have not tested it but I think, this is what you looking for - all numbers that can be divided by 3 and 2 using multipliers from 1 to 10 over the range of numbers in your text boxes. In your code, I don't see where you weeding out your numbers that can be divided by 2 and 3
Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs) Handles Button1.Click
Dim num1 As Integer = Integer.Parse(TextBox1.Text)
Dim num2 As Integer = Integer.Parse(TextBox2.Text)
' may be need to check num2 > num1
Dim sum As Integer
For mult as Integer = 1 to 10
For i as integer = num1 To num2
total = i * mult
If sum Mod 2 = 0 OrElse sum Mod 3 = 0 Then
ListBox1.Items.Add(i.ToString() & " * " & mult & " = " & sum.ToString())
End If
Next i
Next
End Sub
This is my best guess as to what you are wanting. You said you were wanting multiples of 2 and 3 for any numbers given to the program, that that's what this does. If you wanted multiples of anything else, just add onto the part inside the {} in the coefficients declaration. Instead of using text boxes for input, I suggest using a NumericUpDowninstead; the GUI will be more intuitive to the end user.
Imports System.Text
Public Class Form1
Private Property maxNumb As Integer
Private Property minNumb As Integer
Private coefficients() As Integer = {2, 3}
Private Sub Button1_Click(sender As Button, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder
For i = Me.minNumb To maxNumb Step 1
For Each coef As Integer In coefficients
sb.Append(i & " * ").Append(coef).Append(" = ").Append(i * coef)
Me.ListBox1.Items.Add(sb.ToString)
sb.Clear()
Next
Next
Me.ListBox1.Refresh()
End Sub
Private Sub NumericUpDown_ValueChanged(sender As NumericUpDown, e As EventArgs) Handles min_NumericUpDown1.ValueChanged, max_NumericUpDown2.ValueChanged
If sender.Name.Contains("max") Then
Me.maxNumb = sender.Value
Else
Me.minNumb = sender.Value
End If
End Sub
End Class

How i can retrive the data from datagridview row click to corresponding data to another form

i am trying to retrieve data from one form to another form , on gridview click event. I have small code I got it from google search but its giving me error. in this code I was trying to retrieve it to second form datagrid. please check my code. where I am wrong.
Private Sub ReceiveGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For j As Integer = 0 To frmGoodsReceive.dgvPODetails.RowCount - 1
If frmGoodsReceive.dgvPODetails.Rows(j).Cells(0).Value = True Then
Dim count As Integer = 0
For i As Integer = 0 To frmGoodsReceive.dgvPODetails.ColumnCount - 1
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value
Next
count += count
End If
Next
End Sub
I am getting this error, while I run.
Conversion from string "PO003" to type 'Boolean' is not valid.
please help me
what I have the code that totally wrong code I guess. because what I am trying to do is , I have one gridview, and some data on that, when I click the row on datagrid , I want to open the corresponding information to second form gridview. this is the right way to do that?
Try Adding...
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value.ToString
Ohhh okay i get it remove the if statement to transfer all your data into the form:
Private Sub ReceiveGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For j As Integer = 0 To frmGoodsReceive.dgvPODetails.RowCount - 1
Dim count As Integer = 0
For i As Integer = 0 To frmGoodsReceive.dgvPODetails.ColumnCount - 1
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value
Next
count += count
Next
End Sub
Or replace the if condition if you want to get all data without NULL value or Nothing
Private Sub ReceiveGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For j As Integer = 0 To frmGoodsReceive.dgvPODetails.RowCount - 1
If frmGoodsReceive.dgvPODetails.Rows(j).Cells(0).Value.ToString <> "" Then
Dim count As Integer = 0
For i As Integer = 0 To frmGoodsReceive.dgvPODetails.ColumnCount - 1
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value
Next
count += count
End If
Next
End Sub
Check if both DataGridView Has The Same Column if not the error may Occur ex. if your passing 10 datas vs 20 datas on other datagridview, the first datagridview will be insufficient to give required data needed by the second datagridview.
Try to analyze this one...
Solution1: DataGridView1 > DataGridview 2
Use this Code
Private Sub ReceiveGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For j As Integer = 0 To frmGoodsReceive.dgvPODetails.RowCount - 1
Dim count As Integer = 0
For i As Integer = 0 To frmGoodsReceive.dgvPODetails.ColumnCount - 1
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value
Next
count += count
Next
End Sub
Solution2:DataGridview1 < DataGridview2
Use this Code
Private Sub ReceiveGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For j As Integer = 0 To Me.dgvReceiveGoods.RowCount -1
If frmGoodsReceive.dgvPODetails.Rows(j).Cells(0).Value.ToString <> "" Then
Dim count As Integer = 0
For i As Integer = 0 To frmGoodsReceive.dgvPODetails.ColumnCount - 1
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value
Next
count += count
End If
Next
End Sub
Solution3:DataGridView1 = DataGridView 2
you can use Both...