Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

Writing REALbasic Code

  • Print
  • + Share This
This chapter is from the book

Using the Code Editor to Write REALbasic Code

Although some behavior of some objects can be controlled by object binding, most behavior is implemented in code. The lines of REALbasic code that you write as you create your application spell out what its objects can do and, consequently, what the application can do. You write that code in the Code Editor.

As mentioned earlier in this book, REALbasic code is object-oriented and event-driven. The Code Editor is your key to object-oriented, event-driven programming in REALbasic. Through it, you explicitly associate chunks of code with the relevant objects and with the events that trigger their action. You open the Code Editor for a PushButton control to edit the code for that object.

The Code Editor has two parts: the browser, which is the left pane; and the editor, which is the right pane (Figure 4.4). Because the Code Editor it is your primary tool for programming in REALbasic, it pays to learn how to use all its features.

Figure 4.4Figure 4.4 The Code Editor has two parts: the browser and the editor.


To open the Code Editor for a control:

  1. Open the window that contains the control.

  2. Double-click the control.

    The Code Editor for the control's parent window opens, with the desired control selected and the primary handler for that control opened.

Every control lives in some window. When you open the Code Editor for a control, you have access to the code for all the controls in that window.

To open the Code Editor for a window:

  • Double-click anywhere in the window itself (not a control).

or

  • Press Option-Tab.

or

  • Click the desired window in the Project window, and press Option-Tab.

To see all the controls for a window:

  1. Open the Code Editor for the window.

  2. Click the disclosure triangle next to controls in the browser pane.

To see all the events to which a control can respond:

  1. Open the Code Editor for the window.

  2. Click the disclosure triangle next to Controls in the browser pane.

  3. Click the disclosure triangle next to the name of the control (Figure 4.5).

Figure 4.5Figure 4.5 The browser pane (left) shows all the events to which a control can respond.


Like a control, a window is an object and has a set of events to which it can respond.

To see the code associated with a control's event handler:

  1. Open the Code Editor for the window.

  2. Click the disclosure triangle next to Controls in the browser pane.

  3. Click the disclosure triangle next to the name of the control.

  4. Click the name of the event handler. The editor pane will display the code, if any, that is executed when this event occurs (Figure 4.6).

    Figure 06Figure 4.6 The editor pane (right) shows the code associated with the selected event.


The window itself can respond to events, just as controls can.

To see all the events to which a window can respond:

  1. Open the Code Editor for the window.

  2. Click the disclosure triangle next to Events in the browser pane.

  3. Click the disclosure triangle next to the name of the event.

To see the code associated with a window's event handler:

  1. Open the Code Editor for the window.

  2. Click the disclosure triangle next to Events in the browser pane.

  3. Click the disclosure triangle next to the name of the event.

    The editor pane will display the code, if any, that is executed when this event occurs.

What are these events to which your code needs to respond? One common and important category of event that your code should be able to handle is the user's selection of an item from a menu. Menu handlers are so common that they have their own section in the Code Editor's browser pane.

To see all the menu handlers for a window:

  1. Open the Code Editor for the window.

  2. Click the disclosure triangle next to Menu Handlers in the browser pane (Figure 4.7).

Figure 4.7Figure 4.7 Menu handlers are listed separately in the browser pane.


To see the code associated with a menu handler:

  1. Open the Code Editor for the window.

  2. Click the disclosure triangle next to Menu Handlers in the browser pane.

  3. Click the name of the menu handler. The editor pane will display the code, if any, that is executed when this menu item is selected (Figure 4.8).

Figure 4.8Figure 4.8 The code associated with a menu item.


You can create your own handlers, called methods, and your own custom properties for objects. You'll learn about methods and custom properties later in this chapter.

You may find it useful to customize the editor to suit your preferred way of working.

To change the Code Editor's fonts:

  1. Open the Preferences dialog via the Preferences menu.

In Mac OS X, the Preferences item is in the REALbasic menu; in Mac OS 9, it's in the Edit menu.

  1. Set the fonts for the Code Editor, code printing, and the default for controls (Figure 4.9).

Figure 4.9Figure 4.9 Setting the fonts for the Code Editor, code printing, and the default for controls.


To get more space for writing code:

  • Select a smaller code font in the Preferences dialog.

or

  • Drag the resize bar between the panes to the left (Figure 4.10).

    Figure 4.10Figure 4.10 To get more space for writing code, drag the resize bar between the panes to the left.


or

  • Press Shift-Tab. This last action will hide the browser. Pressing Shift-Tab a second time will make the browser visible again.

    NOTE

    This last method will work only if you have been navigating the items in the browser by using the keyboard arrow keys. If you have been using the mouse and clicking items, Shift-Tab only moves the focus between the browser and the Code Editor.

Tips

  • To get more room for editing in the Code Editor, you can drag the resize bar between the editing and browser panes to the left. If you drag it all the way, the browser pane disappears. To bring it back, Control-click anywhere in the Code Editor. Unlike the Shift-Tab method for hiding and showing the browser, this method always works.

  • Clicking the disclosure triangle next to an item in the Code Editor's browser pane is one way to eliminate some of the Code Editor's information overload. But you can hide all the handlers that don't have any code associated with them by clicking the Show and Hide Empty Methods button at the bottom of the window (Figure 4.11).

Figure 4.11Figure 4.11 To hide all the handlers that don't have any code associated with them, click the Show and Hide Empty Methods button at the bottom of the window.


When you select a method in the browser pane of the Code Editor, its associated code appears in the editor pane. Even for an "empty" method, some code is supplied automatically: the first and last lines (Figure 4.12). You can't enter, delete, or change these lines directly, but you can ignore them except for the parameters listed in the first line. Parameters are explained later in this chapter in the discussion on writing your own methods.

Figure 4.12Figure 4.12 The Code Editor always supplies the first and last lines of a handler or method. You get to write the rest.

To enter code in the code panel:

  1. Type anywhere between the first and last lines.

    REALbasic tries to guess what you are going to type. As you type, it offers its guess in light-gray type.

  2. If the guess REALbasic offers is what you want, press Tab to accept it.

    If REALbasic finds several plausible completions for what you are typing, it will display an ellipsis (...).

  3. If REALbasic displays an ellipsis, press Tab to see a contextual menu of choices; if you are offered a menu, click one of the menu items to accept it.

    This guesswork is called autocompletion.

Tips

  • You can use autocompletion in many places in REALbasic. Just watch what you type. If you see gray text appearing ahead of the characters you are entering, they are REALbasic's guess at what you intend to type. Press Tab to accept the guess or to see a contextual menu of guesses. If the guess is wrong, just go on typing.

  • Keep your eyes peeled for the Tips Window. It will give you good suggestions for writing code.

To edit your code:

  • Cut, copy, paste, and clear code via the Edit-menu items, just as you would do it with text.

    Command-key alternatives work as you would expect, too.

or

  • Use the Edit-menu items Undo and Redo or their Command-key alternatives to retract unwise editing actions or reinstate wise ones.

or

  • Highlight text to be moved, and drag it to where you want it.

This technique works within a handler, between handlers, from the Online Language Reference to the Code Editor, and between the Code Editor and any word processor or text editor that supports drag and drop. You can also drag text clippings from the Desktop to the Code Editor.

Tips

  • Hold down the Shift key when you choose the Cut or Copy menu item or their Command-key equivalents to perform a "cut and append" or "copy and append" operation. The cut or copied text is appended to the text that's already in the Clipboard, rather than replacing it. This method is handy when you want to gather up several snippets of code and move them all to one new location.

  • To copy and paste a single word quickly, click the location where you want to paste, hold down the Option key, and click the word you want to paste. You can also use this technique to copy property or method names from the browser pane to your code. The real benefit of this shortcut is not speed but accuracy. Every word that you can copy rather than typing is one word that you can't mistype.

To find/replace code in the Code Editor:

  1. Choose Edit > Find.

    The Find/Replace window appears.

  2. Enter the text to find.

  3. Enter the text to replace it with, if any.

  4. Select the scope of the search:

    • Source: search within the current handler/method.

    • Module: search within the current module. (Modules are explained in Chapter 5.)

    • Project: search anywhere in the current project.

  5. Click one of the three buttons to find, replace and find again, or replace all occurrences (Figure 4.13).

    Figure 4.13Figure 4.13 The Code Editor supports Find and Replace operations within code.

Tips

  • You can jump immediately to the last method you were editing by clicking the Back button (the small arrow at the bottom of the Code Editor window). The Back button and the Forward button (next to it) work similarly to the Back and Forward buttons in a Web browser; they let you step backward and forward through the methods you have been editing (Figure 4.14).

    Figure 4.14Figure 4.14 The Back button and the Forward button work similarly to the Back and Forward buttons in a Web browser; they let you step backward and forward through the methods you have been editing.

  • You will find yourself frequently jumping back and forth between the Code Editor for a window and the window itself in the IDE. A quick way to open the window from its Code Editor is to click the leftmost icon at the bottom of the Code Editor window (Figure 4.15).

    Figure 4.15Figure 4.15 A quick way to open the window from its Code Editor is to click the leftmost icon at the bottom of the Code Editor window.


To print your code:

  1. Choose File > Print, or press Command-P. The Print dialog appears.

  2. Specify how much code to print: the current method, the current module, or the whole project.

  3. Click Print.

  • + Share This
  • 🔖 Save To Your Account

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020