VB.NET Bubble Sorting not sorting - vb.net

the problem given here is an error I have obtained when trying to sort my set values via bubble sorting method. Below is the code and the error i would obtain would be 'System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' after the first pass. Below is the entire code
Public Class Form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ourArray(20) As Integer
Dim i As Integer
ourArray(0) = 20
ourArray(1) = 12
ourArray(2) = 1
ourArray(3) = 15
ourArray(4) = 5
ourArray(5) = 9
ourArray(6) = 10
ourArray(7) = 4
ourArray(8) = 3
ourArray(9) = 8
ourArray(10) = 2
ourArray(11) = 6
ourArray(12) = 13
ourArray(13) = 14
ourArray(14) = 7
ourArray(15) = 17
ourArray(16) = 11
ourArray(17) = 16
ourArray(18) = 18
ourArray(19) = 19
Do While ourArray(i) > ourArray(i + 1)
Dim iTemp As Integer
For iPass = 0 To 1
For i = 0 To 19
If ourArray(i) > ourArray(i + 1) Then
iTemp = ourArray(i)
ourArray(i) = ourArray(i + 1)
ourArray(i + 1) = iTemp
End If
Next
Next iPass
Dim assist As String
For i = 0 To 19
assist = assist & ourArray(i) & vbNewLine
Next
MsgBox(assist)
Loop
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
End Class
The goal is to press the given button when the program starts, and have the program sort the given random values in the correct order with each pass/button press. Example: start program, press sort button, values 4,5,3,2,1 appear in a message box. press button again, one of the values will be in the correct spot. 5,4,3,2,1 -> 1,4,3,2,5 -> 1,2,4,3,5 -> 1,2,3,5,4 -> 1,2,3,4,5

Related

Lottery Program - Visual Basic

Have to create a lottery program, getting the random numbers and such easily enough. However I'm stumped. Essentially I have 2 buttons. One to display a checked list box with numbers 1-100, along with the 5 lotto numbers. I have a 2nd button that checks 2 things, to make sure that more than 5 numbers are not checked matching numbers. I'm lost on how to check for a match between the selected numbers between the RNG numbers.
Public Class Form1
Private Sub displayBtn_Click(sender As Object, e As EventArgs) Handles displayBtn.Click
Dim lottoNumbers(5) As Integer
Dim counter As Integer = 0
Dim number As Integer
Dim randomGenerator As New Random(Now.Millisecond)
'This will randomly select 5 unique numbers'
Do While counter < 5
number = randomGenerator.Next(0, 98)
If Array.IndexOf(lottoNumbers, number) = -1 Then
lottoNumbers(counter) = number
counter += 1
End If
Loop
'Display the lottery numbers in the label.'
Label1.Text = String.Empty
Array.Sort(lottoNumbers)
For Each num As Integer In lottoNumbers
Label2.Text = "Lottery Numbers"
Label1.Text &= CStr(num) & " "
Next num
For x = 0 To 98
CheckedListBox1.Items.Add(x + 1)
Next
End Sub
Private Sub checkBtn_Click(sender As Object, e As EventArgs) Handles checkBtn.Click
Dim count As Integer = 0
Dim x As Integer
'Checks to see if user checked more than 5'
For x = 0 To CheckedListBox1.Items.Count - 1
If (CheckedListBox1.CheckedItems.Count > 5) Then
MessageBox.Show("You cannot select more than 5 numbers.")
Return
Else
If (CheckedListBox1.GetItemChecked(x) = True) Then
count = count + 1
ListBox1.Items.Add(CheckedListBox1.Items(x))
End If
End If
Next
End Sub

I couldn't change color of diagonal line in 16*16 label matrix. What's my issue here?

16*16 matrix is coming to my screen when I start the program. But when I click diagonal button, diagonal line isn't red. That does not change.
my code :
Public Class Form1
Dim etk As New Label 'i define the matrix as etk
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i = 0 To 15
For j = 0 To 15
Dim etk As New Label
Me.Panel.Controls.Add(etk)
etk.Name = i
etk.Tag = j
etk.Size = New Size(26, 26)
etk.BackColor = Color.Black
etk.Location = New Point(30 * i + 10, 30 * j + 10)
Next
Next
End Sub
Private Sub diagonal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Timer1.Enabled = True
For i = 0 To 15
For j = 0 To 15
etk.Name = i
etk.Tag = j
If i = j Then
etk.BackColor = Color.Red
End If
Next
Next
End Sub
End Class
thanks for your interests..
First, the names of your dynamically loaded labels are not unique. You need to concatenate the x and the y to make it unique:
For i = 0 To 15
For j = 0 To 15
Dim etk As New Label
Me.Panel.Controls.Add(etk)
etk.Name = i & "," & j
etk.Tag = j
etk.Size = New Size(26, 26)
etk.BackColor = Color.Black
etk.Location = New Point(30 * i + 10, 30 * j + 10)
Next
Next
Then, when you are looping through in the click event, you need to find the existing label by name. Simply setting the name property on an existing label doesn't do anything to find a control. All that does is alter the one that's already there. To find a control by name, you can look it up using the name as the key value of the Controls property:
For i = 0 To 15
For j = 0 To 15
If i = j Then
Dim etk As Control = Me.Panel.Controls(i & "," & j)
etk.BackColor = Color.Red
End If
Next
Next
Or, more simply:
For i = 0 To 15
Dim etk As Control = Me.Panel.Controls(i & "," & i)
etk.BackColor = Color.Red
Next

Sorted List in Array Visual Basic [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I have a program that creates a list of 25 random numbers from 0 to 1,000. I have to buttons the first button will load a list box with the random numbers and the second button will sort the list of numbers from the smallest to largest which is where I implemented bubble sort code. Now the other list box that is supposed to hold the sorted numbers doesn't work properly it only shows one number instead of all of them.
Here is my code:
Option Strict On
Public Class Form1
Dim rn As Random = New Random
Dim Clicked As Long = 0
Dim numbers, sort As Long
Private Sub GenerateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBtn.Click
Clicked += 1
For x = 0 To 25
numbers = rn.Next(0, 1000)
RandomBox.Items.Add(numbers)
If Clicked >= 2 Then
RandomBox.Items.Clear()
Clicked = 1
End If
Next
End Sub
Private Sub SortBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortBtn.Click
Dim Sorted() As Long = {numbers}
Dim Swapped As Boolean
Dim endOfArray As Integer = Sorted.Length - 1
Dim Tmp As Byte
While (Swapped)
Swapped = False
For I = 0 To endOfArray - 1
If Sorted(I) > Sorted(I + 1) Then
Tmp = CByte(Sorted(I))
Sorted(I) = Sorted(I + 1)
Sorted(I + 1) = Tmp
Swapped = True
End If
endOfArray = endOfArray - 1
Next
End While
SortBox.Items.Clear()
For I = 0 To Sorted.Count - 1
SortBox.Items.Add(Sorted(I))
Next
End Sub
End Class
Change your:
Dim Sorted() As Long = {numbers}
to
Sorted(x) = numbers
edit: Since you changed your code. You need to put back in the line that loads the Sorted Array.
For x = 0 To 25
numbers = rn.Next(0, 1000)
RandomBox.Items.Add(numbers)
Sorted(x) = numbers
If Clicked >= 2 Then
RandomBox.Items.Clear()
Clicked = 1
End If
Next
and remove the:
Dim Sorted() As Long = {numbers}
from the second part and put this declaration back in the beginning like you had:
Dim Sorted(26) as Long
The way you have will only show the latest random number. It is not any array but a single entity. Therefore only the latest will be add into the array. You need to load each number into the array as you create each one. Thus the (x) which loads it into position x.
You didn't use any arrays at all in your project...you're using the ListBox as your storage medium and that's a really bad practice.
I recommend you set it up like this instead:
Public Class Form1
Private rn As New Random
Private numbers(24) As Integer ' 0 to 24 = 25 length
Private Sub GenerateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBtn.Click
For x As Integer = 0 To numbers.Length - 1
numbers(x) = rn.Next(0, 1000)
Next
' reset the listbox datasource to view the random numbers
RandomBox.DataSource = Nothing
RandomBox.DataSource = numbers
' empty out the sorted listbox
SortBox.DataSource = Nothing
End Sub
Private Sub SortBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortBtn.Click
' make a COPY of the original array that we will sort:
Dim sorted(numbers.Length - 1) As Integer
Array.Copy(numbers, sorted, numbers.Length)
Dim Swapped As Boolean = True
Dim endOfArray As Integer = Sorted.Length - 1
Dim Tmp As Integer
While (Swapped)
Swapped = False
For I As Integer = 0 To endOfArray - 1
If sorted(I) > sorted(I + 1) Then
Tmp = sorted(I)
sorted(I) = sorted(I + 1)
sorted(I + 1) = Tmp
Swapped = True
End If
Next
endOfArray = endOfArray - 1
End While
' reset the listbox datasource to view the sorted numbers
SortBox.DataSource = Nothing
SortBox.DataSource = sorted
End Sub
End Class
Also, note that you were decrementing endOfArray inside your for loop. You should only decrement it after each pass; so outside the for loop, but inside the while loop.
Additionally, you were using a Tmp variable of type Byte, but generating numbers between 0 and 999 (inclusive). The Byte type can only hold values between 0 and 255 (inclusive).
Your Bubble Sort implementation was very close to correct!

Visual Basic - Continually add input to a total from a continually changing input

I have a label next to a textbox that is supposed to display the total as it is typed into the textbox.
TEXTBOX INPUT: 14 -> 0 -> 20 -> 0 -> 30 LABEL DISPLAY: 14 -> 14 ->
34 -> 34 -> 64
I have overcome the problems I expected, adding each number as inputted and adding each number as deleting input (using backspace). But now if the input value is over 1000 it deletes a 9 per place value every backspace until the number is back under 1000.
INPUT: 1000 DISPLAY: 91 ||| INPUT: 10000 DISPLAY: 9892
I have no idea why. If someone could figure out why numbers over 1000 act differently I would greatly appreciate the help.
Here's my code.
Private Sub HandleKeyPress(sender As Object, e As KeyPressEventArgs)
Me.KeyPressedUnicode = Asc(e.KeyChar)
End Sub
Private Sub HandleTextChanged(sender As Object, e As EventArgs)
Dim textboxText As Double = Val(Me.TB.Text)
Dim labelText As Double = Val(Me.LBL.Text)
Dim previousTextboxText As Double = 0
If (Me.TB.TextLength > 1) Then
previousTextboxText = Val(textboxText .ToString.Substring(0, Me.TB.TextLength - 1))
labelText = labelText - previousTextboxText
If KeyPressedUnicode = 8 Then
textboxText = (textboxText.ToString.Substring(0, 1))
previousTextboxText = 0
End If
ElseIf (Me.TB.TextLength <= 1 And KeyPressedUnicode = 8) Then
textboxText = 0
previousTextboxText = 0
End If
Me.LBL.Text = labelText + textboxText
End Sub
Its possible I forgot a few pieces of code, let me know if more information is needed.
You've over-complicated the code. Part of the problem is that you're trying to make the code work when hitting the backspace key which you have to do multiple times with a large number. If you can change the code to only do the addition when you hit ENTER then the code is super simple.
Try this:
Private Sub HandleKeyPress(sender As Object, e As KeyPressEventArgs)
If Asc(e.KeyChar) = 13 Then
Me.LBL.Text = Val(Me.LBL.Text) + Val(Me.TB.Text)
End If
End Sub
If you really need to add the values based on the backspace key being hit them try this:
Private Ready As Boolean = False
Private Sub HandleKeyPress(sender As Object, e As KeyPressEventArgs)
If Asc(e.KeyChar) = 8 Then
If Me.Ready Then
Me.LBL.Text = Val(Me.LBL.Text) + Val(Me.TB.Text)
Me.Ready = False
End If
Else
Me.Ready = True
End If
End Sub

For loop through function

I tried to draw 64 button(8 rows and 8 columns) to make a game, but for some reason my code does not work. I hope someone can help me with this.
Public Class Form1
Dim AMOUNTOF_COLUMNS As Integer = 8
Dim AMOUNTOF_ROWS As Integer = 8
Public Function setNew(row As Integer, column As Integer) As Button
Dim newButton As New Button()
newButton.Width = 40
Me.Controls.Add(newButton)
newButton.Left = 0
newButton.Top = 0
newButton.Height = newButton.Width
newButton.Text = ""
newButton.Tag = (column + (row * AMOUNTOF_COLUMNS))
AddHandler newButton.MouseDown, AddressOf Klik
Return newButton
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For value1 As Integer = 0 To AMOUNTOF_ROWS Step 1
For value2 As Integer = 0 To AMOUNTOF_COLUMNS Step 1
Me.setNew(value1, value2)
Next
Next
End Sub
If I execute this code, it only draws me one button.
You're drawing 64 buttons but they are all on top of each other. You will need to offset them correctly. Particularly these lines should be modified so that every button gets the correct position.
newButton.Left = 0
newButton.Top = 0
EDIT:
newButton.Tag = "Column x - row y"
Public Sub Klik(sender as Object, e As EventArgs)
Dim b as Button = sender
Dim value as String = b.Tag
If value = "Column 1 - row 1" Then
' Do action for col 1 row 1
Else If value = "Column 2 row 1" Then
' Do action for col 2 row 1
Else If ...
Else If ...
End If
End Sub
You need to modify you method to draw your buttons next to each other, and set their Left and Top properties accordingly:
Public Function setNew(row As Integer, column As Integer) As Button
Dim newButton As New Button()
newButton.Width = 40
newButton.Height = newButton.Width
Me.Controls.Add(newButton)
newButton.Left = column * newButton.Width ' you may need some offset here too
newButton.Top = row * newButton.Height ' you may need some offset here too
newButton.Text = ""
newButton.Tag = (column + (row * AMOUNTOF_COLUMNS))
AddHandler newButton.MouseDown, AddressOf Klik
Return newButton
End Function
Also, you need to draw one less column and row as you're currently creating 9 rows and columns
For value1 As Integer = 0 To AMOUNTOF_ROWS - 1 Step 1
For value2 As Integer = 0 To AMOUNTOF_COLUMNS -1 Step 1
Me.setNew(value1, value2)
Next
Next