' Gambas class file
' Math Drill by William Teder. Feel free to use parts of the code, but please give me credit.
' Declare Variables
' Define number of times user has pressed the Give Up button
PRIVATE givenuptimes AS Integer
' Define how many questions the user has answered
PRIVATE questionsanswered AS Integer
' Define what level the user is on
PRIVATE level AS Integer
' Define the number of points the user has
PRIVATE points AS Integer
' Define whether ot not addition, subtraction, multiplication, and division are enabled. This value is changed whrn the textboxes are togglrd.
PRIVATE additionenabled AS Boolean
PRIVATE subtractionenabled AS Boolean
PRIVATE multiplicationenabled AS Boolean
PRIVATE divisionenabled AS Boolean
'Set an integer for counting the number of times the program resets, for use in email.
PRIVATE resetcount AS Integer
' Define variable to help in detrmining the problem type
PRIVATE problemtype AS Integer
' Define number of numbers to add when using an addition problem
PRIVATE currentproblem AS String
PRIVATE currentanswer AS String
PRIVATE currentproblempointvalue AS Integer
PRIVATE add1 AS Integer
PRIVATE add2 AS Integer
PUBLIC SUB Form_Open()
'Set inital values
givenuptimes = 0
questionsanswered = 0
level = 1
points = 0
additionenabled = TRUE
subtractionenabled = TRUE
multiplicationenabled = TRUE
divisionenabled = TRUE
' GET RID OF THE FOLLOWING LINE WHEN USED IN BUILDING OR THE PROGRAM WILL NOT WORK!
' currentanswer = 0
makeproblem()
END
PUBLIC SUB btnClearAnswer_Click()
'clear the contents of txtAnswer and give it the focus
txtAnswer.Text = ""
txtAnswer.SetFocus
END
PUBLIC SUB chkGiveUp_Click()
' Check to see if the Give Up button's visible property is set to true, and if it is, hide the button. If it is hidden, show it again.
IF btnGiveUp.Visible = FALSE THEN
btnGiveUp.visible = TRUE
lblhGiveUp.visible = TRUE
lblGivenUp.visible = TRUE
expSettings.Raise
RETURN
END IF
IF btnClearAnswer.Visible THEN
btnGiveUp.Visible = FALSE
lblGivenUp.Visible = FALSE
lblhGiveUp.Visible = FALSE
expSettings.Raise
RETURN
END IF
END
PUBLIC SUB btnGiveUp_Click()
'Increment the counter that shows the number pf times the user has given up by 1
givenuptimes = givenuptimes + 1
lblGivenUp.Text = givenuptimes
'Display the right answer
txtAnswer.Text = currentanswer
WAIT 2
txtAnswer.Text = ""
points = points - 10
lblPoints.Text = points
makeproblem()
END
PUBLIC SUB chkLevels_Click()
' Check to see if the Level label's visible property is set to true, and if it is, hide the label. If it is hidden, show it again.
IF lblLevel.Visible = FALSE THEN
lblhLevel.Visible = TRUE
lblLevel.visible = TRUE
'Move the answered section down one place, if it is not already
lblhAnswered.Top = 150
lblAnswered.Top = 143
'For some odd reason, when the new objects appear they move themselves forward. Re-lowering the Settings container fixes this.
IF lblGivenUp.Visible = FALSE OR lblAnswered.Visible = TRUE THEN
lblhGiveUp.Top = 200
lblGivenUp.Top = 193
END IF
expSettings.Raise
RETURN
END IF
IF lblLevel.Visible THEN
lblLevel.Visible = FALSE
lblhLevel.Visible = FALSE
'Move the answered section up one place
lblhAnswered.Top = 105
lblAnswered.Top = 98
'Check to see if the GiveUp section needs to be moved
IF lblGivenUp.Visible = TRUE AND lblhLevel.Visible = FALSE THEN
lblhGiveUp.Top = 150
lblGivenUp.Top = 143
END IF
'See above comment
expSettings.Raise
RETURN
END IF
END
PUBLIC SUB btnReset_Click()
' Notify user the program is working
lblProblem.Text = "Resetting, please wait..."
'First, reset variables
givenuptimes = 0
questionsanswered = 0
level = 1
points = 0
'Clear the answer textbox.
txtAnswer.Text = ""
'Next, clear interface.
lblAnswered.Text = "0"
lblLevel.Text = "0"
lblPoints.Text = "0"
lblGivenUp.Text = "0"
' Notify user that the reset has finished and that the program is generating a new problem
lblProblem.Text = "Reset complete, generating new problem..."
makeproblem()
END
' These four subs are the same code with different variables. Changes state of variable the checkbox is assigned to, makes it the opposite of it's current state.
PUBLIC SUB chkAddition_Click()
IF divisionenabled = FALSE AND subtractionenabled = FALSE AND multiplicationenabled = FALSE THEN
PRINT Message("You must select at least one option.")
additionenabled = TRUE
chkAddition.Value = TRUE
RETURN
END IF
' Set the boolean additionenabled to be true or false based on it's current state
IF additionenabled = TRUE THEN
additionenabled = FALSE
RETURN
END IF
IF additionenabled = FALSE THEN
additionenabled = TRUE
RETURN
END IF
END
PUBLIC SUB chkSubtraction_Click()
IF additionenabled = FALSE AND divisionenabled = FALSE AND multiplicationenabled = FALSE THEN
PRINT Message("You must select at least one option.")
subtractionenabled = TRUE
chkSubtraction.Value = TRUE
RETURN
END IF
' Set the boolean subtractionenabled to be true or false based on it's current state
IF subtractionenabled = TRUE THEN
subtractionenabled = FALSE
RETURN
END IF
IF subtractionenabled = FALSE THEN
subtractionenabled = TRUE
RETURN
END IF
END
PUBLIC SUB chkMultiplication_Click()
IF additionenabled = FALSE AND subtractionenabled = FALSE AND divisionenabled = FALSE THEN
PRINT Message("You must select at least one option.")
multiplicationenabled = TRUE
chkMultiplication.Value = TRUE
RETURN
END IF
' Set the boolean multiplicationenabled to be true or false based on it's current state
IF multiplicationenabled = TRUE THEN
multiplicationenabled = FALSE
RETURN
END IF
IF multiplicationenabled = FALSE THEN
multiplicationenabled = TRUE
RETURN
END IF
END
PUBLIC SUB chkDivision_Click()
IF additionenabled = FALSE AND subtractionenabled = FALSE AND multiplicationenabled = FALSE THEN
PRINT Message("You must select at least one option.")
divisionenabled = TRUE
chkDivision.Value = TRUE
RETURN
END IF
' Set the boolean divisionenabled to be true or false based on it's current state
IF divisionenabled = TRUE THEN
divisionenabled = FALSE
RETURN
END IF
IF divisionenabled = FALSE THEN
divisionenabled = TRUE
RETURN
END IF
END
' Subroutine to make a problem
SUB makeproblem()
' This is the code that determines a new problem for the user based on a number of factors, including how many problems of that type they have done,
' whether or not the type is enabled or disabled, how quickly they can answer the question, thier points and level, and how many times they have
' given up on that type of problem. The rest is random. DO NOT MESS WITH THIS CODE UNLESS YOU KNOW WHAT YOU ARE DOING! It is very easy to mess up the program,
' as well as generate stack overflow errors.
' Engine version : 0.0.0.1
' Engine by William Teder
' ------------------------------------
' Generates a random number for the Problem Type, 1 - 4
problemtype = Rnd(1, 5)
IF problemtype = 1 THEN
' Problem Type: Addition
lblProblem.Text = "Problem Type: Addition"
' Determine the number of numbers to add together
IF level <= 5 THEN
' Determine how high the number should be
IF points <= 200 THEN
add1 = Rnd(1, 10)
add2 = Rnd(1, 10)
currentanswer = add1 + add2
currentproblem = add2 & " + " & add1
lblProblem.Text = currentproblem
RETURN
END IF
IF points > 200 AND < 400 THEN
add1 = Rnd(1, 20)
add2 = Rnd(1, 20)
add1 + add2 = currentanswer
currentproblem = add2 & " + " & add1
lblProblem.Text = currentproblem
RETURN
END IF
IF points > 400 AND < 500 THEN
add1 = Rnd(1, 30)
add2 = Rnd(1, 30)
add1 + add2 = currentanswer
currentproblem = add2 & " + " & add1
lblProblem.Text = currentproblem
RETURN
END IF
END IF
IF level <= 10 AND >= 6 THEN
' Code for three numbers
END IF
IF level <= 15 AND >= 11 THEN
'Code for 4 numbers
END IF
IF level <= 20 AND >= 16 THEN
' Code for 5 numbers
END IF
IF problemtype = 2 THEN
' Problem Type: Subtraction
lblProblem.Text = "Problem Type: Subtraction"
END IF
IF problemtype = 3 THEN
' Problem Type: Multiplication
lblProblem.Text = "Problem Type: Multiplication"
END IF
IF problemtype = 4 THEN
' Problem Type: Division
lblProblem.Text = "Problem Type: Division"
END IF
END
PUBLIC SUB gotright()
' Increment questions answered counter
questionsanswered = questionsanswered + 1
' Increment Points
points = points + currentproblempointvalue
' For every 100 points, increment the level counter by 1.
IF level = 1 AND points = 200 THEN level = 2
IF level = 2 AND points = 300 THEN level = 3
IF level = 3 AND points = 400 THEN level = 4
IF level = 4 AND points = 500 THEN level = 5
IF level = 5 AND points = 600 THEN level = 6
IF level = 6 AND points = 700 THEN level = 7
IF level = 7 AND points = 800 THEN level = 8
IF level = 8 AND points = 900 THEN level = 9
IF level = 9 AND points = 1000 THEN level = 10
IF level = 10 AND points = 1100 THEN level = 11
IF level = 11 AND points = 1200 THEN level = 12
IF level = 12 AND points = 1300 THEN level = 13
IF level = 13 AND points = 1400 THEN level = 14
IF level = 14 AND points = 1500 THEN level = 15
IF level = 15 AND points = 1600 THEN level = 16
IF level = 16 AND points = 1700 THEN level = 17
IF level = 17 AND points = 1800 THEN level = 18
IF level = 18 AND points = 1900 THEN level = 19
IF level = 19 AND points = 2000 THEN level = 20
' Change font color of the textbox to green to let the user know he/she got the problem right
txtAnswer.Foreground = &H579524&
' Create delay to let the user know they got the answer right
WAIT 1
' Change back to regular color
txtAnswer.Foreground = &HFF004&
END
PUBLIC SUB txtAnswer_KeyRelease()
IF txtAnswer.Text = currentanswer THEN
txtAnswer.Foreground = &H579524&
questionsanswered = questionsanswered + 1
lblAnswered.Text = questionsanswered
WAIT 0.25
txtAnswer.Text = ""
' Change back to regular color
txtAnswer.Foreground = &HFF0004&
lblPoints.text = points
makeproblem()
END IF
END
PUBLIC SUB Button1_Click()
makeproblem()
END
At line 251, there is an unexpected >. Why does it fail to compile? Thanks.
IF points > 400 AND < 500 THEN
That doesn't look quite right to me, unless your BASIC is really advanced (and it appears GAMBAS isn't quite that advanced). It should be:
IF points > 400 AND points < 500 THEN
You have the right idea with this line, for example:
IF lblGivenUp.Visible = TRUE AND lblhLevel.Visible = FALSE THEN
Same for all the variations you have of this, you need to include the variable on the right hand side of the and as well):
IF points > 200 AND < 400 THEN
IF points > 400 AND < 500 THEN
IF level <= 10 AND >= 6 THEN
IF level <= 15 AND >= 11 THEN
IF level <= 20 AND >= 16 THEN
Related
May I know, how to make the option box and checkbox checked as in the list box? Let say, if the data is yes so that the option box will automatically be checked and another one if the choose Whatsapp and email it will automatically be checked at the WhatsApp and email.
The column from Method is starting from column C9 and for Participation at column D9.
FYI,
Emp 2 - Yes Emp 3 - No Emp 8 - Whatsapp Emp 9 - Phone Call Emp 10 -
Facebook Emp 11 -Email Emp 12 - SMS
And here is the coding that I already try
Private Sub lstEmployee_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'dim the variables
Dim i As Integer
On Error Resume Next
'find the selected list item
i = Me.lstEmployee.ListIndex
'add the values to the text boxes
Dim methodsOfCommunication() As String
Me.Emp1.Value = Me.lstEmployee.Column(0, i)
Select Case Me.lstEmployee.Column(2, i)
Case "Yes"
Emp2.Value = True
Emp3.Value = False
Case "No"
Emp2.Value = False
Emp3.Value = True
End Select
' Reset Methods of Communication checkboxes.
Emp8.Value = False
Emp9.Value = False
Emp10.Value = False
Emp11.Value = False
Emp12.Value = False
' Set Methods of Communication checkboxes.
methodsOfCommunication = Split(Me.lstEmployee.Column(1, i), ", ")
For i = LBound(methodsOfCommunication, 1) To UBound(methodsOfCommunication, 1)
Select Case methodsOfCommunication(i)
Case "Whatsapp"
Emp8.Value = True
Case "Phone Call"
Emp9.Value = True
Case "Facebook"
Emp10.Value = True
Case "Email"
Emp11.Value = True
Case "SMS"
Emp12.Value = True
End Select
Next
Me.Emp4.Value = Me.lstEmployee.Column(3, i)
Me.Emp5.Value = Me.lstEmployee.Column(4, i)
Me.Emp6.Value = Me.lstEmployee.Column(5, i)
Me.Emp7.Value = Me.lstEmployee.Column(6, i)
Me.Emp13.Value = Me.lstEmployee.Column(7, i)
Me.Emp14.Value = Me.lstEmployee.Column(8, i)
Me.Emp15.Value = Me.lstEmployee.Column(9, i)
On Error GoTo 0
End Sub
Dim methodsOfCommunication() As String
Dim i As Integer
Me.Emp1.Value = Me.lstEmployee.Column(0, i)
Select Case Me.lstEmployee.Column(2, i)
Case "Yes"
Me.Emp2.Value = True
Me.Emp3.Value = False
Case "No"
Me.Emp2.Value = False
Me.Emp3.Value = True
End Select
' Reset Methods of Communication checkboxes.
Me.Emp8.Value = False
Me.Emp9.Value = False
Me.Emp10.Value = False
Me.Emp11.Value = False
Me.Emp12.Value = False
' Set Methods of Communication checkboxes.
methodsOfCommunication = Split(Me.lstEmployee.Column(1, i), ", ")
For i = LBound(methodsOfCommunication, 1) To UBound(methodsOfCommunication, 1)
Select Case methodsOfCommunication(i)
Case "Whatsapp"
Me.Emp8.Value = True
Case "Phone Call"
Me.Emp9.Value = True
Case "Facebook"
Me.Emp10.Value = True
Case "Email"
Me.Emp11.Value = True
Case "SMS"
Me.Emp12.Value = True
End Select
Next
I do not know how to explain the problem, hope you understand
I use the code to force the user to fill out the form in order
The problem I have is 4 combobox
The 1 opens the 3 or 4 by the value of 1
If I change the value of 1, I have to write the value again in 2 to open the 3 or 4
My problem is that he does not constantly check what the value is in 1, only when there is a change in 2
How can I force the 2 to constantly check the value in 1?
That's the code I have
Private Sub ComboBox2_Change()
Dim lLoc As Long
lLoc = Me.ComboBox2.ListIndex
If lLoc = -1 Then
Me.ComboBox2.SetFocus
ComboBox3.Enabled = False
ComboBox4.Enabled = False
Else
ComboBox3.Enabled = True
End If
If ComboBox1.Text = "abc" Then
ComboBox4.Enabled = True
Else
ComboBox4.Enabled = False
End If
End Sub
I have a Combobox with values: "none", "1", "2", "3" and "4". And I want to have visible the number of pages shown by the Combobox. How can I achieve that? i.e. combobox = 3 - Pages 1, 2 and 3 become visible.
An alternative:
Private Sub ComboBox1_Change() ' Could also use a number spinner to control user input
Dim pgCount as Short
' Some data validation
pgCount = IIf(isnumeric(ComboBox1.Text),CShort(ComboBox1.Text),0)
pgcount = iif(pgCount >= 0 and pgCount < 5, pgCount,0)
Me.MultiPage2(0).Visible = pgCount > 0
Me.MultiPage2(1).Visible = pgCount > 1
Me.MultiPage2(2).Visible = pgCount > 2
Me.MultiPage2(3).Visible = pgCount > 3
End Sub
Data validation for user input is always important - how can you handle bad input? A better question is: how can you prevent bad input in the first place.
A little explanation:
The data validation firstly checks if the user has put in something representing a number
If the input resembles a number then convert it to a number. In this case converting it to an Short removes any decimal places the user may have entered.
If the input does not resemble a number, use 0 instead (a soft fail)
If the converted number is between 0 ("None") and 4, then OK, otherwise set it to 0 (another soft fail).
[End of data conversion]
The next four lines use simple Boolean logic to determine if the tab should be shown. I used your answer code to determine the logic.
Finally got it
Private Sub ComboBox1_Change()
Select Case ComboBox1.Text
Case "none"
Me.MultiPage2(0).Visible = False
Me.MultiPage2(1).Visible = False
Me.MultiPage2(2).Visible = False
Me.MultiPage2(3).Visible = False
Case "1"
Me.MultiPage2(0).Visible = True
Me.MultiPage2(1).Visible = False
Me.MultiPage2(2).Visible = False
Me.MultiPage2(3).Visible = False
Case "2"
Me.MultiPage2(0).Visible = True
Me.MultiPage2(1).Visible = True
Me.MultiPage2(2).Visible = False
Me.MultiPage2(3).Visible = False
Case "3"
Me.MultiPage2(0).Visible = True
Me.MultiPage2(1).Visible = True
Me.MultiPage2(2).Visible = True
Me.MultiPage2(3).Visible = False
Case "4"
Me.MultiPage2(0).Visible = True
Me.MultiPage2(1).Visible = True
Me.MultiPage2(2).Visible = True
Me.MultiPage2(3).Visible = True
End Select
End Sub
I am pretty new to VBA and am running into a very frustrating error. My code attempts to lock and black out cells based on the values of other cells. However, if any cell in the worksheet is deleted, the code breaks and I get a type mismatch error. The code does not break if the value of a cell is changed, only if it is deleted.
My code is provided below, does anybody have ideas? Thanks in advance!!
Private Sub Worksheet_Change(ByVal Target As Range)
If Target <> Range("$C$28") And Target <> Range("$C$31") Then Exit Sub
ActiveSheet.Unprotect
If Range("C31") = "Cellular" And Range("C28") = "No" Then
Range("C34").Locked = False
Range("C34").Interior.ColorIndex = 19
Range("C35").Locked = True
Range("C35").Interior.ColorIndex = 1
Range("C35").Value = "Verizon"
Range("C37").Locked = False
Range("C37").Interior.ColorIndex = 19
Range("C38").Locked = False
Range("C38").Interior.ColorIndex = 19
ElseIf Range("C31") = "Cellular" And Range("C28") = "Yes" Then
Range("C34").Locked = False
Range("C34").Interior.ColorIndex = 19
Range("C35").Locked = True
Range("C35").Interior.ColorIndex = 1
Range("C35").Value = "Verizon"
Range("C37").Locked = True
Range("C37").Interior.ColorIndex = 1
Range("C37").Value = "Verizon"
Range("C38").Locked = True
Range("C38").Interior.ColorIndex = 1
Range("C38").Value = "Verizon"
ElseIf Range("C31") <> "Cellular" And Range("C28") = "No" Then
Range("C34").Locked = True
Range("C34").Interior.ColorIndex = 1
Range("C34").Value = "Verizon"
Range("C35").Locked = False
Range("C35").Interior.ColorIndex = 19
Range("C37").Locked = False
Range("C37").Interior.ColorIndex = 19
Range("C38").Locked = False
Range("C38").Interior.ColorIndex = 19
Else
Range("C34").Locked = True
Range("C34").Interior.ColorIndex = 1
Range("C34").Value = "Verizon"
Range("C35").Locked = False
Range("C35").Interior.ColorIndex = 19
Range("C37").Locked = True
Range("C37").Interior.ColorIndex = 1
Range("C37").Value = "Verizon"
Range("C38").Locked = True
Range("C38").Interior.ColorIndex = 1
Range("C38").Value = "Verizon"
End If
ActiveSheet.Protect
End Sub
When you delete an entire row, you should confirm that the resulting Target object will represent an entire row range. So your comparison statement will fail, because you're relying on the implicit/default property of a range object (its .Value property). A multi-cell range will raise an error when attempting to reference this property, in this manner.
So you need some logic to avoid this. Perhaps something as simple as examining the first cell in the Target range:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells(1,1) <> Range("$C$28") And Target <> Range("$C$31") Then Exit Sub
I am making an app in visual basic with .NET Framework 4. I have to generate a list of prime numbers as per the user's input. So far, for my output if you put in 5 for the first five prime numbers you get 3 5 7 7 9 11 11. I am not sure if my number increment is in the wrong place. Thanks for any help you can give me. Also, I'm not sure how to include 2 as a prime number in my code.
Imports System.Math
Public Class Form1
Dim number, divisor, max, count As Integer
Dim IsPrime As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
number = TextBox1.Text
For divisor = 2 To Sqrt(number)
If number Mod divisor = 0 Then
IsPrime = False
TextBox2.Text = ("Number is not prime")
Exit For
Else
TextBox2.Text = ("Number is prime")
End If
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Wrap As String
Wrap = Chr(13) & Chr(10)
max = TextBox3.Text
Dim count = 0
number = 2
While count <= max
IsPrime = True
For divisor = 2 To Sqrt(number)
If number Mod divisor = 0 Then
IsPrime = False
Exit For
Else
IsPrime = True
TextBox4.Text += number & Wrap
count += 1
End If
Next
number += 1
End While
End Sub
End Class
You should not use the else branch in the for loop, in this case, each time the Mod unequal to 0, you will touch the else block, take number 11 for example:
11 mod 2 <> 0, you went into the else block,
11 mod 3 <> 0, you went into the else block again!
you can divide the number by all numbers between 2 to sqrt(number), and then use isprime to check whether it is a prime like this.(I am using VBScript here)
isprime = true
for i = 2 to Int(sqr(number))
if number mod i = 0 then
isprime = false
exit for
end if
next
if isprime = true then
count = count + 1
' do something here...
end if
number = number + 1
and don't forget to truncate the square root of number.
Just to share with you this script :
Option Explicit
Dim Title,Copyright,j,fso
Dim WshShell,Affich,LogFile
Title = "Calcul Nombres Premiers"
Copyright = " (c) by Hackoo 2015"
For j = 2 to 1000
If Premier(j) = True Then
Affich = Affich & j & vbTab
End If
Next
MsgBox Affich,vbInformation,Title + Copyright
LogFile = "c:\NombresPremiers.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(LogFile) Then
fso.DeleteFile LogFile
end If
Affich = replace(Affich,vbTab,vbCrlf)
Call WriteLog(Affich,LogFile)
Set WshShell=CreateObject("wscript.shell")
WshShell.Run LogFile
'**********************************
Function Premier(Nombre)
Dim i,d
' Trois nombres ne seront pas pris en compte par le compteur,
' on s'organise pour qu'ils soient vus avant.
Select Case Nombre
Case 0
Premier = False
Exit Function
Case 1
Premier = False
Exit Function
Case 2
Premier = True
Exit Function
End Select
For i = 2 To Int(Sqr(Nombre)) + 1
d = Nombre Mod i
If d = 0 Then
Premier = False
Exit Function
End If
Next
Premier = True
End Function
'**********************************
Sub WriteLog(strText,LogFile)
Dim fs,ts
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
ts.WriteLine strText
ts.Close
End Sub
'***********************************