I have THREE buttons on a form and would like to control the order in which they can be CLICKED based on an ever changing random number made up of 3 digits
I can control the click order as long as the random number does not change
As the code is now the random number is not being generated
I just change the random number to test
I understand what is needed is a valid test when clicking the buttons looked at using an Array not much luck
How to dynamically associate the position of the number in the String to control the buttons click order?
Private Sub btnAdd_Click()
ck = 0
GetPos
tbOne.Text = S1
End Sub
Private Sub GetPos()
Dim Y As Integer
S1 = "132" 'Random Number
For Y = 1 To Len(S1)
pos = Mid(S1, Y, 1) 'Position of Value in S1 the Random Number
lbOne.AddItem pos & vbNewLine
Next
End Sub
Private Sub btnOne_Click()
ck = ck + 1
If Mid(S1, 1, 1) = "1" And ck = 3 Then
btnOne.BackColor = vbYellow
Else
ck = 4
If btnOne.BackColor = vbYellow Then
Exit Sub
End If
btnOne.BackColor = vbRed
If btnOne.BackColor <> vbYellow Then
btnOne.Enabled = False
End If
End If
End Sub
Private Sub btnTwo_Click()
ck = ck + 1
If Mid(S1, 2, 1) = "3" And ck = 1 Then
btnTwo.BackColor = vbYellow
Else
ck = 4 ' Prevents other buttons from being Yellow
If btnTwo.BackColor = vbYellow Then
Exit Sub
End If
btnTwo.BackColor = vbRed
If btnTwo.BackColor <> vbYellow Then
btnTwo.Enabled = False
End If
End If
End Sub
Private Sub btnThree_Click()
ck = ck + 1
If Mid(S1, 3, 1) = "2" And ck = 2 Then
btnThree.BackColor = vbYellow
Else
ck = 4
If btnThree.BackColor = vbYellow Then
Exit Sub
End If
btnThree.BackColor = vbRed
If btnThree.BackColor <> vbYellow Then
btnThree.Enabled = False
End If
End If
End Sub
This seems to do what you need:
Private Sub btnOne_Click()
ck = ck + 1
If ck = Mid(S1, 1, 1) Then
btnOne.BackColor = vbYellow
Else
btnOne.BackColor = vbRed
btnOne.Enabled = False
End If
End Sub
Private Sub btnTwo_Click()
ck = ck + 1
If ck = Mid(S1, 2, 1) Then
btnTwo.BackColor = vbYellow
Else
btnTwo.BackColor = vbRed
btnTwo.Enabled = False
End If
End Sub
Private Sub btnThree_Click()
ck = ck + 1
If ck = Mid(S1, 3, 1) Then
btnThree.BackColor = vbYellow
Else
btnThree.BackColor = vbRed
btnThree.Enabled = False
End If
End Sub
This code finds all the information you need
I am still trying to implement the values in the respective button clicks events
Private Sub btnAdd_Click()
S1 = "132"
S2 = "321"
FindPlace
tbOne.Text = S1
End Sub
Private Sub FindPlace()
Dim Y As Integer
Dim i As Integer
tbOne.Text = S1
lbOne.Clear
For Y = 1 To Len(S2)
pos = Mid(S1, 4 - Y, 1)
S3 = Mid(S2, Y, 1)
For i = 1 To Len(S1)
If Mid(S1, i, 1) = S3 Then
lbOne.AddItem "Press " & i & " the value " & pos & " is in Position " & Mid(S1, i, 1)
End If
Next
Next
End Sub
Related
I am creating a program that prints the element of array one at a time (from top left to bottom right) using the timer control, however, it's not printing the elements one at a time, it prints all of them at the same time. I need help, below is a sample screenshot and my code so far.
Public Class SymbolDrawFRM
Private symbol(10, 10) As String
Sub Drawing()
Dim s As String = ""
For i = 1 To rowNUD.Value
For j = 1 To columnNUD.Value
s = s & symbol(i, j) & # & " "
Next
s = s & vbCrLf
Next
outputTBX.Text = s
End Sub
Private Sub startStopBTN_Click(sender As Object, e As EventArgs) Handles startStopBTN.Click
Timer.Start()
End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
Drawing()
End Sub
End Class
Ignoring the Array, and only using the NumericUpDown controls:
Private Sub startStop_Click(sender As Object, e As EventArgs) Handles startStop.Click
Timer1.Interval = 500
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Static x As Integer
Static y As Integer
If x = 0 And y = 0 Then
outputTBX.Clear()
End If
outputTBX.AppendText("# ")
x = x + 1
If x = columnNUD.Value Then
x = 0
outputTBX.AppendText(vbCrLf)
y = y + 1
If y = rowNUD.Value Then
x = 0
y = 0
Timer1.Stop()
MessageBox.Show("Done!")
End If
End If
End Sub
im using console. ive written code its for my exam and im not very good at it but ive tried and no matter what i do i cant get the program to stop crashing. it keeps crashing at code 0 and im really frustrated please help me ive attached the code below
Module Module1
Sub Main()
Dim discount As Integer = 0
Dim freetickets As Integer = 0
Dim estimatedcost As Integer = 0
Dim totalstudents As Integer = 0
Dim coachcost As Integer = 550
Dim entryticket As Integer = 30
Dim name(45) As String
Dim paidstatus(45) As Boolean
Dim studentspaid As Integer = 0
Dim totalcost As Integer = 0
Dim collectedcost As Integer = 0
Dim finalcost As Integer = 0
Console.WriteLine("Enter Student Name")
name(45) = Console.ReadLine()
Console.WriteLine("has the student paid? (true/false)")
paidstatus(45) = Console.ReadLine()
If paidstatus(45) = True Then
studentspaid = studentspaid + 1
totalstudents = totalstudents + 1
ElseIf paidstatus(45) = False Then
totalstudents = totalstudents + 1
End If
totalcost = (totalstudents * 30) + (550 / totalstudents)
If totalstudents = 45 Then
If studentspaid = 10 Then
freetickets = freetickets + 1
End If
If studentspaid = 20 Then
freetickets = freetickets + 1
End If
If studentspaid = 30 Then
freetickets = freetickets + 1
End If
If studentspaid = 40 Then
freetickets = freetickets + 1
End If
collectedcost = (studentspaid * 30) + (550 / studentspaid)
discount = (freetickets * 30) - (550 / studentspaid)
finalcost = totalcost - collectedcost - discount
If finalcost > 0 Then
Console.WriteLine("loss of")
Console.WriteLine(-finalcost)
End If
If finalcost = 0 Then
Console.WriteLine("broken even")
End If
If finalcost < 0 Then
Console.WriteLine("profit of")
Console.WriteLine(finalcost)
End If
End If
End Sub
End Module
I've been searching Google for awhile and on this site but I can't figure out what &HF7 means? Can someone please explain? Sorry if its a dumb question. I'm very new to this stuff...
Here is the code I'm studying.
Set WshShell = CreateObject("WScript.Shell")
Key = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
DigitalID = WshShell.RegRead(key & "DigitalProductId")
ProductName = "Product Name: " & WshShell.RegRead(Key & "ProductName") & vbNewLine
ProductID = "Product ID: " & WshShell.RegRead(Key & "ProductID") & vbNewLine
ProductKey = "Installed Key: " & ConvertToKey(DigitalID)
ProductID = ProductName & ProductID & ProductKey
If vbYes = MsgBox(ProductId & vblf & vblf & "Save to a file?", vbYesNo + vbQuestion, "Windows Key Information") then
Save ProductID
End if
Function ConvertToKey(Key)
Const KeyOffset = 52
isWin8 = (Key(66) \ 6) And 1
Key(66) = (Key(66) And &HF7) Or ((isWin8 And 2) * 4)
i = 24
Chars = "BCDFGHJKMPQRTVWXY2346789"
Do
Cur = 0
X = 14
Do
Cur = Cur * 256
Cur = Key(X + KeyOffset) + Cur
Key(X + KeyOffset) = (Cur \ 24)
Cur = Cur Mod 24
X = X -1
Loop While X >= 0
i = i -1
KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
Last = Cur
Loop While i >= 0
If (isWin8 = 1) Then
keypart1 = Mid(KeyOutput, 2, Last)
insert = "N"
KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0)
If Last = 0 Then KeyOutput = insert & KeyOutput
End If
a = Mid(KeyOutput, 1, 5)
b = Mid(KeyOutput, 6, 5)
c = Mid(KeyOutput, 11, 5)
d = Mid(KeyOutput, 16, 5)
e = Mid(KeyOutput, 21, 5)
ConvertToKey = a & "-" & b & "-" & c & "-" & d & "-" & e
End Function
Function Save(Data)
Const ForWRITING = 2
Const asASCII = 0
Dim fso, f, fName, ts
fName = "Windows Key.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile fName
Set f = fso.GetFile(fName)
Set f = f.OpenAsTextStream(ForWRITING, asASCII)
f.Writeline Data
f.Close
End Function
&HF7 is used as a mask here.
The byte in position 66 of the array Key is compared with the byte &HF7
Written in binary form &HF7 becomes 11110111. If you "And" the value of Key(66) with 11110111 then you'll get a new byte made up of all the bits in Key(66) except the bit in the 4th position from the right.
For example if Key(66) is 10101010 then 10101010 And 11110111 will be 10100010.
It's the number 247.
In VBScript, the &H prefix indicates a hexadecimal number, similar to the 0x prefix in C/C++. The number is F7, which is equivalent of the decimal value 247.
It's a public constant: http://www.vbforums.com/showthread.php?277384-VB-Key-COnsts
Typically used to detect modifier keys being held (ctrl, alt etc)
http://microsoft.public.word.vba.general.narkive.com/28vVYW5c/detect-modifier-keys-from-vba
That specific one is Public Const VK_CRSEL which I assume to be Ctrl select.
I want to make a line rotate. I studied the pi and radians and I made my own algorithm (if I can call it like that). I don't like to use already-made code from the Internet. I want to discover them alone, but using logic. Here is the code:
Dim pi As Double
Dim a, b, c, d, e, x, y As Double
Dim speed, radius As Integer
Private Sub Form_Load()
pi = 3.14159265358979
speed = 1
radius = 600
End Sub
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled = True Then
Command1.Caption = "Stop"
Else
Command1.Caption = "Start"
End If
End Sub
Private Sub Timer1_Timer()
ForeColor = vbWhite
timer1.interval=speed
Refresh
a = a + 2
b = Sin((a * pi) / 180)
c = Cos((a * pi) / 180)
y = radius * b
x = radius * c
Call Label1.Move(6240 + x, 4200 + y)
If Left(b, 1) = "-" Then
Label1.Caption = "---"
Else
Label1.Caption = "+++"
End If
If Left(c, 1) = "-" Then
Label1.Caption = Label1.Caption & " " & "---"
Else
Label1.Caption = Label1.Caption & " " & "+++"
End If
Line (3000 + x, 4200 + y)-(6240 + x, 4200 + y)
Line (3000, 4200)-(3000 + x, 4200 + y)
Line (6240, 4200)-(6240 + x, 4200 + y)
For d = 3000 To 6240
Line (d, 4200)-(3000 + x, 4200 + y)
Next
For e = 3000 + x To 6240 + x
Line (e, 4200 + y)-(6240, 4200)
Next
End Sub
I want to rotate the line on x-axis, not z (it appears to be z). I recalculated everything, but I don't see where is the problem. What would be an explained formula?
I believe you are after the following effect:
Option Explicit
Dim D As Long, S As Long, Y As Long
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled = True Then
Command1.Caption = "Stop"
Else
Command1.Caption = "Start"
End If
End Sub
Private Sub Form_Load()
D = 1 'Start going down; change to 0 to start going up instead
Y = 100 'Mid point
End Sub
Private Sub Timer1_Timer()
If S Then
If S = 8 Then
S = 0
Else
S = S + 1
lblRate = "0"
Exit Sub
End If
End If
Refresh
If D Then
If Y < 200 Then
Select Case Y
Case Is < 20
'Begin to accelerate
Y = Y + 1
lblRate = "+1"
Case Is < 40
'Continue to accelerate
Y = Y + 2
lblRate = "+2"
Case Is < 160
'Set acceleration to peak
Y = Y + 3
lblRate = "+3"
Case Is < 180
'Begin to decelerate
Y = Y + 2
lblRate = "+2"
Case Else
'Continue to decelerate
Y = Y + 1
lblRate = "+1"
End Select
Else
'Stop and reverse direction
D = 0
S = 1
lblRate = "0"
End If
Else
If Y > 0 Then
Select Case Y
Case Is < 20
'Begin to accelerate
Y = Y - 1
lblRate = "-1"
Case Is < 40
'Continue to accelerate
Y = Y - 2
lblRate = "-2"
Case Is < 160
'Set acceleration to peak
Y = Y - 3
lblRate = "-3"
Case Is < 180
'Begin to decelerate
Y = Y - 2
lblRate = "-2"
Case Else
'Continue to decelerate
Y = Y - 1
lblRate = "-1"
End Select
Else
'Stop and reverse direction
D = 1
S = 1
End If
End If
Line (120, 100)-(120, Y)
End Sub
While not technically following a properly calculated curvature, it is more of a simplified version of a line rotating around the X-axis.
Also, make sure to use the Pixel scale mode, rather than Twips, for better drawing performance.
Quick question that I've been struggling with. I have 2 arrays of different lengths that contain strings.
I want to output a new array which removes BOTH the elements if a duplicate is detected. At the moment it only removes duplicates but leaves the original which is incorrect for what I am trying to accomplish.
E.g.
input = array ("cat","dog","mouse","cat")
expected output = array ("dog","mouse")
actual output = array ("cat","dog","mouse")
Code is below:
Sub removeDuplicates(CombinedArray)
Dim myCol As Collection
Dim idx As Long
Set myCol = New Collection
On Error Resume Next
For idx = LBound(CombinedArray) To UBound(CombinedArray)
myCol.Add 0, CStr(CombinedArray(idx))
If Err Then
CombinedArray(idx) = Empty
dups = dups + 1
Err.Clear
ElseIf dups Then
CombinedArray(idx - dups) = CombinedArray(idx)
CombinedArray(idx) = Empty
End If
Next
For idx = LBound(CombinedArray) To UBound(CombinedArray)
Debug.Print CombinedArray(idx)
Next
removeBlanks (CombinedArray)
End Sub
Thanks for all help and support in advance.
What about using Scripting.Dictionary? Like this:
Function RemoveDuplicates(ia() As Variant)
Dim c As Object
Set c = CreateObject("Scripting.Dictionary")
Dim v As Variant
For Each v In ia
If c.Exists(v) Then
c(v) = c(v) + 1
Else
c.Add v, 1
End If
Next
Dim out() As Variant
Dim nOut As Integer
nOut = 0
For Each v In ia
If c(v) = 1 Then
ReDim Preserve out(nOut) 'you will have to increment nOut first, if you have 1-based arrays
out(nOut) = v
nOut = nOut + 1
End If
Next
RemoveDuplicates = out
End Function
Here is a quick example. Let me know if you get any errors.
Sub Sample()
Dim inputAr(5) As String, outputAr() As String, temp As String
Dim n As Long, i As Long
inputAr(0) = "cat": inputAr(1) = "Hen": inputAr(2) = "mouse"
inputAr(3) = "cat": inputAr(4) = "dog": inputAr(5) = "Hen"
BubbleSort inputAr
For i = 1 To UBound(inputAr)
If inputAr(i) = inputAr(i - 1) Or inputAr(i) = temp Then
inputAr(i - 1) = "": temp = inputAr(i): inputAr(i) = ""
End If
Next i
n = 0
For i = 1 To UBound(inputAr)
If inputAr(i) <> "" Then
n = n + 1
ReDim Preserve outputAr(n)
outputAr(n) = inputAr(i)
End If
Next i
For i = 1 To UBound(outputAr)
Debug.Print outputAr(i)
Next i
End Sub
Sub BubbleSort(arr)
Dim value As Variant
Dim i As Long, a As Long, b As Long, c As Long
a = LBound(arr): b = UBound(arr)
Do
c = b - 1
b = 0
For i = a To c
value = arr(i)
If (value > arr(i + 1)) Xor False Then
arr(i) = arr(i + 1)
arr(i + 1) = value
b = i
End If
Next
Loop While b
End Sub
EDIT
Another way without sorting
Sub Sample()
Dim inputAr(5) As String, outputAr() As String
Dim n As Long, i As Long, j As Long
Dim RemOrg As Boolean
inputAr(0) = "cat": inputAr(1) = "Hen": inputAr(2) = "mouse"
inputAr(3) = "cat": inputAr(4) = "dog": inputAr(5) = "Hen"
For i = 0 To UBound(inputAr)
For j = 1 To UBound(inputAr)
If inputAr(i) = inputAr(j) Then
If i <> j Then
inputAr(j) = "": RemOrg = True
End If
End If
Next
If RemOrg = True Then
inputAr(i) = ""
RemOrg = False
End If
Next i
n = 0
For i = 0 To UBound(inputAr)
If inputAr(i) <> "" Then
n = n + 1
ReDim Preserve outputAr(n)
outputAr(n) = inputAr(i)
End If
Next i
For i = 1 To UBound(outputAr)
Debug.Print outputAr(i)
Next i
End Sub