GettingStarted

Difference between revisions of "Web Tutorial"

From Xojo Documentation

(Created page with "= Learn the Basics: Xojo Web Tutorial = This Web Tutorial is for people who are new to programming and new to Xojo. It is an introduction to the Xojo development environment...")
 
(Next Steps)
Line 320: Line 320:
  
 
==Next Steps==
 
==Next Steps==
Congratulations, you have successfully completed the Web Tutorial and now have a fully functional app. To continue on your journey of learning Xojo, you should next move on to the [UserGuide|User_Guide], which covers Xojo in its entirety. You will also want to refer to the [Language Reference|language_reference], which covers the specifics of language elements, classes and other details of Xojo.
+
Congratulations, you have successfully completed the Web Tutorial and now have a fully functional app. To continue on your journey of learning Xojo, you should next move on to the [[UserGuide:Welcome|User Guide]], which covers Xojo in its entirety. You will also want to refer to the [[:Category:Desktop|Language Reference]], which covers the specifics of language elements, classes and other details of Xojo.

Revision as of 19:55, 9 December 2020

Learn the Basics: Xojo Web Tutorial

This Web Tutorial is for people who are new to programming and new to Xojo. It is an introduction to the Xojo development environment and will lead you through the development of a real web app. It should take you about an hour to complete this tutorial.

If you are already familiar with Xojo and would like a shorter introduction to making web apps with Xojo, jump to the Web QuickStart.


Getting Started

If you haven’t done so already, now is the time to launch Xojo. After it finishes loading, the Project Chooser window appears.

Xojo Project Chooser Web.png

Xojo lets you build several different types of apps (Desktop, Web, Console and iOS). For this Tutorial, you are building a web app, so click on Web.

You will now see three fields that need values: Application Name, Company Name and Application Identifier.

  • Application Name is the name of your app. This will be the filename of the actual app file that gets created.
  • Company Name is the name of your company. You may choose to leave this blank.
  • Application Identifier is a unique identifier for this app. It will automatically populate using what you enter for the Application and Company Names, but you can also change it to whatever you want.
  1. Enter TaskManager as the Application Name. You can leave Company Name as it is or change it.
  2. Click OK. The main Xojo window (called the Workspace) opens. This is where you will begin designing your app.


Workspace

Xojo opens the Workspace with the default web page for your app selected in the Navigator and displayed in the Layout Editor.

Web Layout Editor.png

Navigator: The area on the top left shows you all the items in your project. By default you can see WebPage1 (which is selected), the App object and the Session object. Use the Navigator to navigate within your project.

Layout Editor: The center area is the Layout Editor. You use the Layout Editor to design the user interface for the web pages in your app. It shows the web page and previews how it looks when the app runs in a web browser. In this image, the web page is blank because you haven't yet added any user interface controls from the Library.

Library: The area on the right is the Library and shows the controls and interface elements that you can add to a web page or to the project. You design the web page by dragging controls from the Library to the web page. You can also add a control to the web page by double-clicking it. You can change how the controls display in the Library by clicking the small gear icon and choosing a different setting.

If the Library is not visible, click the Library button on the toolbar to show it.

Inspector: Not shown in the above image is the Inspector, which allows you to see and change the properties for the selected control. This area of the Workspace window is shared with the Library. You can show the Inspector by clicking the Inspector button on the toolbar. The Inspector shows information about the selected item in the Navigator or Editor. The contents of the Inspector changes as you click on different items.

About the App

In this tutorial you will create an app to track tasks. Finished Web Tutorial App.png

Task Manager

For the Task manager app, you enter tasks in the text field and then click the Add button to add them to the list. You can click on individual tasks in the list to delete them or mark them as complete.

Task Manager uses three controls:

  • TextField: A TextField control is used to enter text. In this project, the task to add is entered into a TextField at the bottom of the window.
  • Button: A Button is used to trigger an action. This project uses several buttons to perform different actions.
  • ListBox: A ListBox is used to display a list of data. In this project, it is what displays the tasks entered in the TextField.

The next sections walk you through creating the user interface and adding the necessary code to make the app work.

Designing the User Interface

Task List

You should have WebPage1 open in the Layout Editor. You are now going to add a Listbox to the web page. The Listbox is used for storing the tasks.

  1. . In the Library, click on the Listbox and drag it to the top-left corner of the Layout Editor. As you get close to the edges of the web page, you will see alignment indicators that help you position the control.
  2. . Drop the Listbox when you are happy with its position on the web page.
  3. . Click on the Listbox so that the resizing handles appear.
  4. . Grab the handle in the bottom-right corner and drag it to enlarge the Listbox to fill the top 2/3 of the web page.

Buttons

Now you will add the three buttons needed by Task Manager to the web page.

The Delete button removes tasks from the Listbox, the Add button adds tasks to the Listbox and the Complete button marks tasks in the Listbox as completed. Let's add those three buttons to WebPage1:

  1. . In the Library, click on the Button control and drag it to the web page below the lower-right corner of the Listbox. Use the alignment indicators to help you position the button so that it lines up with the right edge of the Listbox.
  2. . In the Library, click on the Button control and drag it to the web page near the bottom-left corner. Again, take advantage of the alignment indicators to help you position the button.
  3. . In the Library, click on the Button control and drag it to the web page near the bottom-right corner.


Text Field

The Text Field is where the user types the Task to add to the list.

  1. . In the Library, click on TextField and drag it to the web page so that it is between the delete and add buttons.
  2. . Resize the Task field. Select the rightmost drag handle and drag the TextField so that it is the same width as the Listbox.

Use the alignment indicators as guides to help you line everything up correctly. After adding all the controls, your web page should now look like this: Web Tutorial Layout.png

Properties

What is a Property?

Controls have various values associated with them such as their caption, height, width and more. Because they help describe the control, they are called Properties. Changing property values allows you to change the look and behavior of the object. For this project, let's change various properties of the webpage and the controls you added. Some of the things you need to do are:

  1. . Rename all controls (and the web page) so that they describe their behavior and are easy to refer to in code.
  2. . Add a Caption to each Button.
  3. . Make the controls resize properly when the window is resized by setting the locking properties.

Inspector

The Inspector is used to change web page and control properties. It shares the same area on the right of the Workspace as the Library. In order to show the Inspector, click the Inspector button on the toolbar or press ⌘-I (Ctrl+I on Windows and Linux).

Web Page Properties

If you haven’t already, display the Inspector by clicking the Inspector button on the toolbar. You need to change the Name and Title properties of the web page:

  1. . First, in the Layout Editor, click on the title bar of the web page to select it.

The Inspector pane now shows the properties for the web page.

  1. . In the Name field (located in the ID group), change the name from “WebPage1” to “TaskManagerPage”. Press Return to see the name change in the Navigator.
  2. . In the Title field (located in the Frame group), change the name from “Untitled” to “Task Manager”. Press Return to see the name change in the title bar of the web page.

ListBox Properties

The Listbox is where the tasks that your user enters are displayed and stored. You need to change the following properties: Name, ColumnCount, Initial Value, ColumnWidths and Locking.

  1. . First, in the Layout Editor, click on the Listbox to select it. The Inspector pane now shows the properties for Listbox.
  2. . In the Name field (located in the ID group), change the name from “Listbox1” to “TaskList”. Press Return to see the name change in the Navigator.
  3. . The Listbox has two columns, one to show the completed status and another to show the name of the task. In the ColumnCount field, change the value from “1” to “2” and press Return.
  4. . You want to change the column headers to describe the data in the list. In the Inspector, click the pencil button for the Initial Value property. This will bring up the Initial Value editor.
  5. . In the Initial Value editor type "Completed", press the Tab key and then type "Task." Click the OK button when you're finished editing.
  6. . Since Completed is only going to contain a simple checkmark when the task is marked as completed, it can be narrower. In the ColumnWidths field, change the value from “*” to “100,*”. Press Return to see the column widths change on the web page. Using “100,*” tells the Listbox that the first column should always be 100 pixels wide and that the rest of the columns share the available width.
  7. . Lastly you need to make changes to the locking so that the Listbox gets larger or smaller as the web page size changes. In the Locking group, look at the image that shows the web page with small locked icons for the top and left and small unlocked icons for bottom and right. Click the locks so that top, left, bottom and right are all locked.

Button Properties

The three buttons are used to perform actions. You need to change the following properties for each button: Name, Caption and Locking.

Delete Button

The Delete button is used to remove tasks from the TaskList.

  1. . In the Layout Editor, click on the Delete button to select it (this is the button directly below the Listbox).

The Inspector now shows the properties for WebButton.

  1. . In the Name field (located in the ID group), change the name from “Button1” to “DeleteButton”. Press Return to see the name change in the Navigator.
  2. . In the Caption field (located in the Appearance group), change the name from “Untitled” to “Delete”. Press Return to see the name change on the button in the web page.
  3. . Now you need to make changes to the locking so that the Delete button stays on the right side of the web page when the web page resizes. In the Locking group look at the image that shows the web page with small locked icons for the top and left and small unlocked icons for bottom and right.
  4. . Click the locks so that right and bottom are locked and left and top are unlocked.

Button

The Add button is used to add the task entered in the TextField to the Task List.

  1. . In the Layout Editor, click on the Add button to select it (this is the button on the far left of the web page below the TextField). The Inspector now shows the properties for PushButton.
  2. . In the Name field (located in the ID group), change the name from “Button2” to “AddButton”. Press Return to see the name change in the Navigator.
  3. . In the Caption field (located in the Appearance group), change the name from “Untitled” to “Add”. Press Return to see the name change on the button in the web page.
  4. . Now you need to check the locking so that the Add button stays on the bottom of the web page when the web page resizes. In the Locking group look at the image that shows the web page with small locked icons for the top and left and small unlocked icons for bottom and right.
  5. . Click the locks so that left and bottom are locked and top and right are unlocked.

Complete Button

The Complete button is used to mark a task as completed.

  1. . Click on the Complete button to select it (this is the button directly below the TextField on the right). The Inspector now shows the properties for WebButton.
  2. . In the Name field (located in the ID group), change the name from “Button3” to “CompleteButton”. Press Return to see the name change in the Navigator.
  3. . In the Caption field (located in the Appearance group), change the name from “Untitled” to “Complete”. Press Return to see the name change on the button in the web page.
  4. . Now you need to make changes to the locking so that the Complete button stays on the right side of the web page when the web page resizes. In the Locking group look at the image that shows the web page with small locked icons for the top and left and small unlocked icons for bottom and right.
  5. . Click the locks so that right and bottom are locked and left and top are unlocked. In the Project List, the newly renamed controls show under the Controls for TaskManagerPage.

Text Field Properties

The TextField is where your user will type the task to add to the list. You need to change the following properties: Name and Locking.

  1. . In the Layout Editor, click on the TextField to select it. The Inspector now shows the properties for the TextField.
  2. . In the Name field (located in the ID group), change the name from “TextField1” to “TaskField”. Press Return to see the name change in the Navigator.
  3. . Now you need to make changes to the locking so that the TextField gets larger or smaller when the web page resizes. In the Locking group look at the image that shows the web page with small locked icons for the top and left and small unlocked icons for bottom and right.
  4. . Click the locks so that left, bottom and right are locked and top is unlocked.

Final Layout

After adjusting all the properties for the web page and its controls, your layout should look like this:

Fiile:Web Tutorial Final Layout.png

Running the App

Your user interface layout is now complete, so it’s time to try it out. But before you go further, you should first save your work.

Saving the Project

  1. . Choose File > Save.
  2. . Name the project “TutorialWeb” and click Save.

Now you can test your app:

  1. . Click the Run button in the toolbar to run the project.
  2. . When Task Manager is displayed, you can interact with the buttons by clicking on them, you can type in the TextField and you can resize the web page to see the buttons reposition themselves.
  3. . Close the browser tab or window to return to the Layout Editor.

Of course, Task Manager doesn’t do anything yet! For that you need to add some code, which is what you'll do next.

Adding Code

Add Button

The Add button adds tasks to the list. The code you add to the button needs to take what was typed in TaskField and add it as a new row to the list.

Follow these steps to add the code:

1. In the Layout Editor, double-click the AddButton control. The Add Event Handler window appears. When a user presses a WebButton, the Pressed event handler is called. 2. The Pressed Event Handler is already selected so just click the OK button.

This displays the Code Editor. Also notice the Navigator updates to show the Pressed event underneath the AddButton control. Now you need to get the task that was typed into the TaskField. You might think you could get the task just by referring to the name of the field, TaskField. That is close, but not quite what you want. What you instead need is a property of TaskField. When you need to refer to a property of an object, you use the name of the object, followed by a dot, followed by the name of the property. In other words, you use this syntax: ObjectName.PropertyName. This is something called “dot” notation and is commonly used in object-oriented programming. In this case the object is TaskField and the property you want is Value (use the Language Reference to find out about all the properties available to TextFields). The syntax looks like this:

TaskField.Value

To actually add a row to a Listbox, you use the AddRow method. You already know how to get the text in the TaskField from step 2. As you have seen before, objects can have properties. And as you now see with TaskList, objects can also have methods. AddRow is one of many methods available to Listboxes and it adds values to the two columns in TaskList. The first column contains the completed status, so it is initially set to blank. The second column contains the name of the task.

3. Add this code to the Pressed event: TaskList.AddRow("", TaskField.Value) 4. Save the project by choosing File > Save. 5. Run the app to test it out. 6. Type tasks in the TaskField and click the Add button to see them appear in the task list. 7. Close the browser tab or window. 8. The app is still running in Xojo, so click the Stop button in the toolbar to return to the Code Editor.

Complete Button

When the user clicks the Complete button, the selected task in the Listbox should be marked as completed. This is indicated by showing a checkmark (✓) in the Completed column.

Let's add code to the Complete button in the same way we added code for the Add button:

1. On the web page, double-click the CompleteButton control, labelled “Complete”. 2. The Pressed event is already selected so click the OK button. Notice the Navigator updates to show the Pressed event underneath the CompleteButton control. To change a row, you first need to know what row is selected. In a Listbox, the currently selected row is contained in the ListIndex property. To set the value in a particular cell of a Listbox, you use the Cell property, specifying the row and column. 3. In the Pressed event handler, add this code to put the checkmark character in column 0 (the completed column) of the currently selected row: TaskList.CellValueAt(TaskList.SelectedRowIndex, 0) = "✓"

4. Run the app and add a few sample tasks. 5. Now click on a task and click the Complete button. A checkmark appears in the Completed column. 6. Close the browser tab or window. 7. The app is still running in Xojo, so click the Stop button in the toolbar to return to the Code Editor.

Delete Button

The Delete button is used to remove tasks from the list. The code you add to the button needs to determine the selected row in the list and remove it from the list.

Let's add code to the Delete button in the same way we added code for the Complete button:

1. On the web page, double-click the DeleteButton control. The Add Event Handler window appears. 2. The Pressed event handler is already selected so click the OK button. Since the selected row will be deleted, you again want to use the SelectedRowIndex property. 3. Use the Listbox method RemoveRowAt to remove a row from the Listbox. You pass RemoveRowAt the row number to remove as a parameter. # Add this code to the Pressed event handler:

TaskList.RemoveRow(TaskList.SelectedIndex)

4. Save the project by choosing File > Save. 5. Run the app and add a few sample tasks. 6. Click on a task in the Task List then click the Delete button. The task is removed from the list.

Debugging=

Finding Bugs is part of creating apps. A bug is when your code or app does something unexpected, often leading to a crash. Although your app works just fine, there are a couple of lingering bugs that need addressing. Have you figured out what the problem is?

Here’s a hint: What happens if you you click on the Complete or Delete buttons but have not selected a task? Try it.

1. Run the app and click on the Complete button without selecting a task. Xojo has switched to its Debugger with a line of code highlighted because an error has occurred. The error occurred because you attempted to remove (or complete) a row that does not exist. When no row is selected in the Listbox, the SelectedRowIndex property returns -1. Since this is not a valid row in the Listbox, the CellValueAt method caused an error. Specifically, the value passed was out of bounds and thus the error is called an OutOfBoundsException. It's called an Exception because it's something that is the unusual case. 2. Click the Resume button in the Debugger toolbar, to see the actual error message. 3. Close the browser tab or window. 4. Click the Stop button in the toolbar to return to the Code Editor. Nobody wants buggy code. Luckily it is easy to prevent this bug from occurring. Essentially, you want to make sure a row is selected before you attempt to Delete or Complete a task. The code to do this uses what you have already learned.

1. Edit the code for Pressed event handler of the DeleteButton as follows:

If TaskList.SelectedRowCount > 0 Then
TaskList.RemoveRowAt(TaskList.SelectedRowIndex)
End If

5. Edit the code for the Complete button as well:

If TaskList.SelectedRowCount > 0 Then
TaskList.CellValueAt(TaskList.SelectedRowIndex, 0) = "✓"
End If

In both cases, the code verifies that a row is selected by checking the SelectedRowCount property to ensure that a row is selected before the actual method is called. 6. Save the project by choosing File > Save. 7. Run the project again and click the Complete button without selecting a row in the task list. No more bug!

Finishing the App

Testing

Just because your app seems to work, doesn’t mean you are finished with it. A good developer always thoroughly tests their apps to look for possible problems.

You already found and fixed one problem (pressing the Completed or Delete button when no row was selected). Do you think there are other problems to fix? Run the app and play around with it a bit. Make a note of things you want to change. In the next section, you will add some improvements to Task Manager.

Improvements

Did you notice that there are times when the buttons in Task Manager probably should be disabled? For example, the Complete button should not mark a task as completed if one is not selected. Right now you can click it, but nothing happens. Also, you are not going to want to add a task to the list if nothing has been entered in the Taskfield.

There are several ways to accomplish this, but one way is to disable the buttons when they should not be used.

Follow these steps to add this improvement:

1. On the web page, select the Complete button. 2. In the Inspector, turn off the Enabled property (in the Appearance group). 3. Select Add Button. 4. In the Inspector, turn off the Enabled property (in the Appearance group). 5. Select the DeleteButton. 6. In the Inspector, turn off the Enabled property (in the Appearance group). Now you will add code to enable the Add button when there is text in the TaskField. 7. On the web page, double-click the TaskField control. The Add Event Handler window appears. Here you are seeing yet another list of event handlers. Every control type has its own specific list of event handlers. In this case, we want to disable AddButton when there is no value in the Taskfield and enable it when there is a value. The ValueChanged event is called whenever the value in the Taskfield is changed, either by the user typing or by your code changing the Value property. 8. ValueChanged is already selected so press the OK button. You now need code that checks the Value property of the TaskField (Me.Value) to see if anything is there. If there is a value there, then the AddButton is enabled by setting its Enabled property to True. If there is no value, then it is disabled by setting its Enabled property to False. 9. Enter the following code:

If Me.Value <> "" Then
AddButton.Enabled = True
Else
AddButton.Enabled = False
End If

You already added code to prevent the Delete and Complete buttons from doing anything if no row is selected in the Task List. Now you can also make those buttons enable when a row is selected and disable when no rows are selected. This is accomplished with the SelectedRowCount property of the Listbox. 10. Double-click the TaskList control. The SelectionChanged event is called whenever the selection in the TaskList control changes. 11. SelectionChanged is already selected so press the OK button. 12. Add this code:

If Me.SelectedRowCount > 0 Then
DeleteButton.Enabled = True
CompleteButton.Enabled = True
Else
DeleteButton.Enabled = False
CompleteButton.Enabled = False
End If

14. Save the project by choosing File > Save. 15. Run the app to test it out. Notice that the Add button is initially disabled. But try typing some text in the TaskField. The Add button immediately become enabled. And if you remove the value from the TaskField, the buttons again become disabled. Similarly, when you click on a row in the Task List, the Delete and Complete buttons become enabled.

Deploying the App

There are several ways to deploy a Xojo web app. The easiest way is to use Xojo Cloud so that you can just click "Deploy" on the main toolbar to send your app to web server where it can be used.

You can also deploy apps yourself as a standalone application that is a self-contained app and web server which can be run on any computer or server. Users can access the app by using the computer's URL and port number.

To learn more about these options, refer to [Web App Deployment|Web_App_Deployment_Overview] in the User Guide.

Next Steps

Congratulations, you have successfully completed the Web Tutorial and now have a fully functional app. To continue on your journey of learning Xojo, you should next move on to the User Guide, which covers Xojo in its entirety. You will also want to refer to the Language Reference, which covers the specifics of language elements, classes and other details of Xojo.