Is there a way when I want to do this:
textbox1.text = x + i
textbox2.text = x + i
textbox3.text = x + i
To do for example like this?
for l = 0 to 2
textbox(l) = x + i
next
Assuming these controls are inside a userform, do something like:
Private Sub FillTextboxes()
Dim i As Long
Dim x As Long
x = 10
For i = 0 To 2
Me.Controls("textbox" & i + 1).Text = x + i
Next i
End Sub
Related
I am currently making a highscore table - reading the times from a .csv file and sorting them from lowest to highest. The list only becomes partially sorted after the code runs.
All the data inputs correctly but when it goes to sort it sorts the data out incorrectly.
Private Sub BeginnerProcess(ByRef player() As pe_player, ByVal x As Integer)
Dim i As Integer
Dim j As Integer
Dim temp As Object
For i = x To 0 Step -1
For j = 0 To i - 1
If player(j).playerTime > player(j + 1).playerTime Then
temp = player(j)
player(j) = player(j + 1)
player(j + 1) = temp
End If
Next
Next
Dim k As Integer
For k = 1 To x
player(k).position = k
Next
End Sub
Here's the output
Leaderboard
Adapting the classic bubble-sort to your case, I think i should be something like the code below:
For i = 0 To x - 1
For j = i + 1 To x
If player(i).playerTime > player(j).playerTime Then
temp = player(i)
player(i) = player(j)
player(j) = temp
End If
Next
Next
I am creating a program and I need help on how I should go at this I need it to read encoded letters and return the correct letter but any loop that I try I just get System.ArgumentOutOfRangeException and it says that I need to make sure that what I am calling is just as long as what is there.
Private Sub btnDecode_Click(sender As Object, e As EventArgs) Handles btnDecode.Click
Do While ((y / 4) = txtDecoded.Text.Length)
If txtEncoded.Text.Substring(y, 1) = "1" Then
y = y + 1
If txtEncoded.Text.Substring(y, 1) = "0" Then
y = y + 1
If txtEncoded.Text.Substring(y, 1) = "1" Then
y = y + 2
strholder2(z) = "S"
ElseIf txtEncoded.Text.Substring(y, 1) = "0" Then
y = y + 2
strholder2(z) = "s"
End If
Else
If txtEncoded.Text.Substring(y, 1) = "5" Then
y = y + 1
If txtEncoded.Text.Substring(y, 1) = "9" Then
y = y + 2
strholder2(z) = "O"
Else
If txtEncoded.Text.Substring(y, 1) = "8" Then
y = y + 2
strholder2(z) = "o"
End If
End If
End If
End If
End If
Loop
Dim h As Integer
For h = 0 To (z)
strencode2 = strencode2 & strholder2(z)
Next
txtDecoded.Text = strencode2
Label1.Text = y.ToString
End Sub
Now that I took Francis Lord's loop into account I figured out how to get it working so thank you Francis Lord and everyone that gave me feedback..
You'll need to put a condition in your While loop to stop it from incrementing the y past the length of your string. Something like
Do While(y < txtEncode.Text.Lenght)
would do, but I'm not too sure how your code is supposed to work so you might have to adjust the condition to match your logic.
j = LBound(arrayTime)
Do Until j = UBound(arrayTime)
j = j + 1
b = b + 1
cnc = b + r
MsgBox cnc
If cnc > 7 Then
b = 0
r = 0
cnc = b + r
End If
numMins = Sheet5.Cells(cnc + 3, 2) - arrayTime(j)
If numMins < 0 Then
g = g + 1
ReArrangeArray arrayTime, j
'ReDim Preserve arrayTime(numrows - 1 + g)
'arrayTime(numrows - 1 + g) = arrayTime(j)
'MsgBox (arrayTime(numrows - 1 + g))
Else
Sheet5.Cells(cnc + 3, 2) = numMins
End If
Loop
If the if statement is true I want to be able to put the array value at the end of the array and remove that value from its current spot. As the code is, it just adds it to the end and increases the size of the array from 12 to 13. How can I get the array to remain size 12 and still place the value at the end of the array and then remove it from its original position? I do not want to touch the array values in front. Just want to take that value and move it to the end.
For instance
array(1,2,3,4,5)
If statement
j on third loop.
array(j)=3
end array should be
array(1,2,4,5,3)
You could use a helper Sub like this one:
Sub ReArrangeArray(inputArray as Variant, indexToSwap as long)
Dim I As Long
Dim tempVal As Variant
If indexToSwap >= LBound(inputArray) And indexToSwap < UBound(inputArray) Then
tempVal = inputArray(indexToSwap)
For I = indexToSwap To UBound(inputArray) - 1
inputArray(i) = inputArray(i + 1)
Next I
InputArray(UBound(inputArray)) = tempVal
End If
End Sub
To be called by your main Sub as follows:
ReArrangeArray arrayTime, j
I'm doing a school project in Visual Basic (using visual studio 2015) and i'm kinda stuck.
My goal is to create a lottery, where player chooses 6 numbers from checkboxes, then he generates six random numbers (1 - 49) and finally, those two sets should be compared and needed result is the number of correctly guessed numbers.
I have both results (guessed numbers, generated numbers) saved in two different labels.
The checkboxes itself are genereted like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lev = 20
tt = 0
For j = 1 To 50
tt = tt + 1
n = n + 1
box(j) = New CheckBox
box(j).Name = "box(" & Str(j) & ")"
If n = 11 Then lev = lev + 110 : n = 1 : tt = 1
box(j).Left = lev
box(j).Parent = Me
box(j).Top = tt * 20
box(j).Tag = j
box(j).Text = j
box(j).Visible = True
Next
box(50).Enabled = False
End Sub
First label (guessed numbers) is filled this way (i'm not posting whole code)
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + " " + box(j).Text
Next
and the second one (generated numbers) like this:
Do
rn = rg.Next(1, 50)
If Not r.Contains(rn) Then
r.Add(rn)
End If
Loop Until r.Count = 6
Label1.Text = r(0).ToString + " " + r(1).ToString + " " + r(2).ToString + " " + r(3).ToString + " " + r(4).ToString + " " + r(5).ToString
any idea how to compare numbers stored in those labels and get the result (number of correctly guessed numbers).
thanks in advance
You can compare numbers in the labels by splitting the Text properties of the labels into arrays of strings and converting them to integer arrays. First though there is a tiny problem with your code that adds the guessed numbers to the label.
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + " " + box(j).Text
Next
The " " should be moved to the end of the line because at the moment, the label will always start with a space and that messes with the function below. So you should have -
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + box(j).Text + " "
Next
Ok. The function below splits the two text labels into their own array and loops through the guesses and checks if any number is contained in the generated numbers. It then returns the number of matches.
Private Function ComparePicks() As Integer
Dim numbersMatched As Integer
Dim picks(5) As Integer
Dim generatedNumbers(5) As Integer
For i As Integer = 0 To 5
picks(i) = CInt(Split(Label9.Text, " "c)(i))
Next
For i As Integer = 0 To 5
generatedNumbers(i) = CInt(Split(Label1.Text, " "c)(i))
Next
For i As Integer = 0 To 5
If generatedNumbers.Contains(picks(i)) Then
numbersMatched += 1
End If
Next
Return numbersMatched
End Function
I've already created a program that will display x number of rows and repeat that :
i.e.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
Now I want to make Pascal's Triangle
Maybe something like this:
Dim arr As Integer(,) = New Integer(7, 7) {}
For i As Integer = 0 To 7
For k As Integer = 7 To i + 1 Step -1
'print spaces
Console.Write(" ")
Next
For j As Integer = 0 To i - 1
If j = 0 OrElse i = j Then
arr(i, j) = 1
Else
arr(i, j) = arr(i - 1, j) + arr(i - 1, j - 1)
End If
Console.Write(arr(i, j) & " ")
Next
Console.WriteLine()
Next
Console-output:
Another approach, only keeps previous and current iterations in memory:
Dim oldList As New List(Of Integer)({0, 1})
For line = 1 To 7
Dim newList As New List(Of Integer)
For i = 1 To oldList.Count - 1
newList.Add(oldList(i - 1) + oldList(i))
Next
Debug.Print(String.Join(" ", newList))
oldList.Clear()
oldList.Add(0)
oldList.AddRange(newList)
oldList.Add(0)
Next
to do that using a windows form, you would need a textbox,multi-line textbox and a button on the design interface
here is the code you need to generate it
Imports System.Numerics 'this allows you to use big integer
Public Class pascal_triangle
Private Function factorial(ByVal k As Integer) As BigInteger
'big integer allows your proram compute for inputs of more than 22
If k = 0 Or k = 1 Then
Return 1
Else
Return k * factorial(k - 1)
End If
End Function
Private Sub BtnGen_Click(sender As Object, e As EventArgs) Handles BtnGen.Click
Dim nCr As Double
Dim i, j, k As Integer
Dim output As String
output = ""
j = Val(TxtColumn.Text)
For k = 0 To j
For i = 0 To k
Dim fact, fact1, fact2 As BigInteger
fact = factorial(k)
fact1 = factorial(k - i)
fact2 = factorial(i)
nCr = fact / (fact1 * fact2)
TxtOutput.Text += Str(nCr) & output
Next
TxtOutput.Text += vbCrLf
Next
End Sub
Private Sub pascal_triangle_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class