Realbasic programs 1
Night club entry program
The program below checks the age of a person and that persons gender. If that person is male or female and is 18 years of age or older, then the person can enter the nightclub. If the person is not a male or female?????? "very strange" :-) or is younger than 18, then it's no entry for you!!!
*Before you start create a new project and place two editfields onto your window + three static textboxes + two pushbuttons. pushbutton1 = button1 (enter data) pushbutton2 = button2(exit program) statictext1 = (you must be 18 to enter) statictext2 = (gender) statictext3 = (age) editfield1 = text1 editfield2 = text2. Check out the picture below to see how I've arranged the controls and various editfield.
Code
dim gender as string "declare gender as a string"
dim age as integer "declare age as integer, numerical,"
gender = text1.text
age = val(text2.text) "use "val" here because it's an integer"
if text1.text = "" or text2.text = "" then "if text 1 = nothing or text2 = nothing, then msgbox, enter info"
msgbox "you must enter some info"
end if "end block statement"
if gender = "female" or gender = "male" then "if entry = female or male then"
if age >= 18 then "if the age is 18 or equal then"
statictext3.text = "you're old enough" "textbox display "you're not old enough"
else "else"
msgbox "no entry too young" "messagebox.... no entry too young!"
text1.text = "" "text1= clear text"
text2.text = "" "text2 = clear the text from text box 2"
text1.setfocus "set the focus back to text box 2"
end if "end first if block"
end if "end second if block"
**** In this program we've been introduced to the if / then statement and relational operators, >"greater than" <"less than" >="greater than or equal to" <=" less than or equal to, <> "not equal to. These are just a few more common operators we'll come across later on****
*Click the pictures to enlarge*
Arrays and counters
The above program shows how we can create an array and loop through the elements of that array using a counter.
* First off, open a new project and put one pushbutton control on it. Call it button1 and change it's caption property to array/counter. Then double click on the button and insert the following code into it.
Code
dim strnamesarray() as string
strnamesarray = array("pete","dave","paul","ann")
dim intcounter as integer
for intcounter = 0 to ubound(strnamesarray)
msgbox ("now processing names " + strnamesarray(intcounter))
next intcounter
*Now go ahead and click the button, and you'll loop through the name list*
*You could also write the array like this.................................
dim strnames(4) as string
strnames(0) = "pete"
strnames(1) = "dave"
strnames(2) = "paul"
strnames(3) = "ann"
*Notice that there are four names, yet I've started off at 0 and continued to 3??????????
When you create an array, you always start at 0 and the next number is 1 2 etc!
Mark
Simple adding program
This is a very simple adding program, the source code is only one line for the calculation part of the program.
First off put three editfield boxes onto youtr form. Then three static text boxes and three pushbuttons. Label them as in the picture.
Next the editfield names as follows text1, text2, result, now the pushbuttons buttonenter, buttonclear, buttonquit.
Now double click on your form the window1. And place this code into it.................
window1.title = "adding program"
This will give your program a name. Next, insert this code into your pushbuttonenter button.........................................................................................................................
result.text = str(cdbl(text1.text) + cdbl(text2.text))
This line adds the code from text1 and text2 and the answer appears in the result.text field.
Finally to clear the numbers from the editfields, double click the clear button and add this code..................................................
text1.text = ""
text2.text = ""
result.text = ""
Mark.
Making decisions using if/then or elseif
We're going to take a look at how computer programs make a decision with our next program. First off we'll be using the if he statement. And later on we'll use the elseif expression, but for now just do the following. Open a new window and place a static text box on your form. Next put two pushbuttons on and name them buttonchange and buttonquit. In the caption just name them like the example. Next add three radio buttons and name them rbyellow, rbgrey, rbwhite, and their caption names.... yellow, grey, and white.
Double click on the close program button and insert this line of code............................
window1.close (or you could simply type "quit")
Next double click on the change button and add this code.............................................
if rbyellow.value = true then
window1.hasbackcolor = true
window1.backcolor = &cFFFF80
end if
if rbgrey.value = true then
window1.hasbackcolor = true
window1.backcolor = &cC0C0C0
end if
if rbwhite.value = true then
window1.hasbackcolor = true
window1.backcolor = &cFFFFFF
end if
Now run the program click on a radio button and push the change button and see your program in action!
Ok you can see all the blocks of code, let's make the program a little more efficient shall we? Replace the above code with the code below and then try your program out.Here we've just introduced the elseif statement and the program executes the same as the above.
if rbyellow.value = true then
window1.hasbackcolor = true
window1.backcolor = &cFFFF80
elseif rbgrey.value = true then
window1.hasbackcolor = true
window1.backcolor = &cC0C0C0
elseif rbwhite.value = true then
window1.hasbackcolor = true
window1.backcolor = &cFFFFFF
end if
Mark.
If then button captions and uppercase lowercase
In this program we're going to have some fun by experimenting with captions and disabling buttons etc. First open a new project and add the controls as in the screenshot. Call the editfield text1, and the enter button buttonenter, and put an exit button on there and call it buttonquit. Don't forget to add another window to the project, it's name will be window2 by default.Now add the code below to your enter button.
dim strpassword as string
strpassword = "openme"
if text1.text <> strpassword then
msgbox(uppercase("you have entered an incorrect password"))
buttonenter.caption = "DISABLED"
buttonenter.enabled = false
text1.text = ""
text1.setfocus
elseif text1.text = strpassword then
msgbox(lowercase("you have entered the correct password"))
window2.show
text1.text = ""
text1.setfocus
end if
Notice we're using upper and lower case to display the text in the message boxes. Go ahead try out your application. First of all enter the correct password............the message box will display a message in lower case and when you click it window2 will be opened. Ok now try entering an incorrect password and see what happens. You'll receive a message in uppercase, and when you click that.The enter button will be disabled and it's caption will be changed to DISABLED.
Don't forget to enter the code into the exit button, just type.......................
quit
Mark.
Making decisions using select case
There is another way programs make decisions and this is the...... select case way! At times, you might want to test a single condition against a number of possible values. While
you can certainly perform this type of test using an If...Then…ElseIf block, REALbasic also pro-
vides you with the Select Case block, which is better suited to performing this type of test.
While an If…Then…ElseIf block evaluates each ElseIf statement, a Select Case block stops exe-
cuting once a matching Case statement is found, making it more efficient. The following
outlines the syntax for the Select Case block.Open up the password program and replace the code in the button enter with the new code below.
dim strpassword as string
strpassword = "mark"
strpassword = text1.text
select case strpassword
case "mark"
msgbox(uppercase("you selected the correct password"))
text1.text = ""
text1.setfocus
window2.show
case else
text1.text = ""
text1.setfocus
msgbox(lowercase("wrong dude!"))
buttonenter.caption = "DISABLED"
buttonenter.enabled = false
end select
Now crank your program up and give it a spin!
A.T.M program
Enter your atm number and get your bank balance
In this application you'll be able to enter your atm password and get your balance. Remember because we're dealing with numeric values rather than strings we have to use integer in our program.
Ok on window1 add a statictextbox with the writing on and center it by using the textallign property. Then add an editfield and call it text1. Then add three pushbuttons and call them buttonenter,buttonoutput,buttonclear.Next add a new window in the project explorer, resize the window until it looks like the example and add the text as in the example. Add a button and call it buttonok. Now we can start coding the first window.
Click on text1 and put a tick in the password box, this will mask your entry like ****** that.
Double click on the enter atm number and add the following code.............................
dim intatmnumber as integer
intatmnumber = 12345
intatmnumber = val(text1.text)
select case intatmnumber
case 12345
msgbox(uppercase("Welcome Mr Mark click ok to see your bank balance"))
window2.show
case else
msgbox(uppercase("invalid number"))
buttonenter.caption = "DISABLED"
buttonenter.enabled = false
buttonenter.enabled = false
buttonclear.caption = "DISABLED"
buttonclear.enabled = false
text1.enabled = false
end select
In the clear buttons code window add this code..........................................
text1.text = ""
And in the exit program buttoms code window type the following code.................
Window1.close
Now open window2 and in the ok button add this line of code...............................
me.hide
Right all that's left is to test drive your application!
Mark.