Palmphi


Tutorial 4: Tables (TTable)

TTable is a component to assist in the creation of tables on the Palm Pilot. Tables are a way to show structured information without having to care for the coordinates of each element on it. Palm tables are very powerful and have many features, with the class TTable you will be able to use much of them, for more advanced features you will have to access directly the API, remember that as with every component in Palmphi, TTable is just an alias for a pointer to a table (TablePtr).

1. Creating a table

You create a table just like you would do for any other component. Before changing any property a table looks like this :

                             

You can change some properties of the table with the object inspector, but you can change them all by double clicking on it.

This is the table editor. Here you can :
1: Change the amount of columns of the table
2: Change the amount of rows of the table
3: Change the type of the column. If the property AutoInit of the Table is set to true, the table is automatically initialized when the form is open. Following types are now allowed :
  • TCheckBox: Like a normal checkbox
  • TIntLabel: A label which displays only integer values
  • TStrLabel: A label which displays alphanumeric values
  • TDateLabel: A label to display dates
  • TEdit: An editable area in the table
4:  Here you can specify the initial value of this cell. If the property AutoInit of the Table is set to true, the cells automatically initialized with their initial values.

5. Here you can see how to access programmatically to this cell.

To change th width of a column, place the mouse over the first row between the arrows (or to the right of the last arrow) and drag to the desired width..

I will use following table for this example :

                     

To abandon the dialog saving the changes press OK on this dialog. Now the table looks like this :

                         

You can now press f9 without writing a single line code and take a look at the compiled program on the palm :

                 

As you see, each cell works as expected. The TEdit seems to have some problems with the selection, but I think that is the default behavior, you have to use one edit on each column to avoid this problem, until I figure out something better (ideas are welcomed).

Now we will be accessing this date from the program. First we will add a button for each cell to copy the contents to a TEdit. For me it looks now so:



We add also a TEdit under the TTable and I will call it MyEdit. Change the MaxChars properties to 25, so we can change it and make it underlined so we can see, where it is.

We write following code for each button (just double click on the button to get to the source editor) :

EVENT Button2_OnClick(EventPtr event)
{
   if(Table1.Checked(0,0))
      MyEdit.Text="Checked";
   else
      MyEdit.Text="not Checked";
}


EVENT Button3_OnClick(EventPtr event)
{
   Char str[20];
   sprintf(str,"%d",Table1.IntValue(0,1));    // I use this to convert an integer to a string.
   MyEdit.Text=str;
}

EVENT Button4_OnClick(EventPtr event)
{
   Char str[20];
   sprintf(str,"%d-%d-%d",Table1.ExtractYear(0,2),Table1.ExtractMonth(0,2),Table1.ExtractDay(0,2));
   MyEdit.Text=str;
}


EVENT Button5_OnClick(EventPtr event)
{
   MyEdit.Text=Table1.TextValue(0,3); 
}


You can now run again the program and see what happens when you press the buttons beside the table. It is not very exciting though, as we cannot change the values of the table and always the same value will be copied.

So now, add another button under the TEdit, call it "copyBtn".

EVENT CopyBtn_OnClick(EventPtr event)
{
   Char *str=MyEdit.Text;
   if(StrLen(str)>1) {
      Char CopyTo=str[0];
      if (CopyTo>='1' && CopyTo<='4') {
          switch (CopyTo) {
             case '4':
                 Table1.TextValue(0,3)=&str[1];
                 break;
             case '3':
                 Table1.SetDateValue(0,2,1,2,1903);
                 break;
             case '2':
                 Table1.IntValue(0,1)=StrToInt(&str[1]);
                 Table1.Erase();    // Erase the table from the form
                 Table1.Draw();
                 break;
             case '1':
                 Table1.Checked(0,0)=str[1]=='C';
                 Table1.Draw();
                 break;
          }
      }
   }
}

Now you can run the program and write in to the TEdit something like that :
1C  -> To check the checkbox
1N -> To delete the check mark
4Hello -> To write hello to the fourth row
21234 -> To write the value 1234 to the intlabel cell
...

For the date I write just a fixed value to the cell, well it is not that I am too lazy to decode the string into a date (no no), it is just to keep it simple.

Well I hope, now you know how to access the individual cells of a table. You can learn more taking a look directly to the file parser.c to the section where the table is defined, or seeing the help part on the table editor.

You can download this project, with the prc here.

If you have any questions you can just send an email.