Wilson's
Notes on CBuilder
Some useful Controls
TPageControl -- for "tabbed notebook" stuff
MessageBoxes -- the simplest use of them
TStringGrid -- a grid to put rows & columns
of text in
DB stuff
TDBLookupComboBox -- Select an item from
one table to go in another
TDBLookupListBox -- Select an item
from one table to go in another
TDBComboBox -- Select an item from a list
-- static or dynamic -- to go in a table
TDataSet -- Accessing tables & queries
TDataSource -- tying datasets to controls
Disclaimer
TPageControl (hereinafter, TPC) -- a Tabbed
Notebook kind of thing
-
Use a TPC found on the Win95 toolbar -- Those other things have some other
purpose
-
To create the individual tabs, right-click on the TPC and choose New Page
-
To change page (to a different tab) programaticly, set the TPC->ActivePage
property to the desired TabSheet
-
To prevent the page from being turned, use the TPC's OnChanging event.
Set AllowChange = false -- doing whatever logic you want. The frustrating
part about all this is that the OnChanging event doesn't know what page
you're trying to turn to. That happens in the OnChange event -- when
the ActivePage property tells what page has just been turned to.
If you don't want to let the user get there, turn back to a different page.
-
You might think that the Sender object in OnChange or OnChanging would
provide a clue about the page being turned to. It doesn't.
It refers to the TPC itself.
-
TPC on a page of another TPC? No problem!
MessageBox
-
int MessageBox(
HWND hWnd, // handle of owner window
LPCTSTR lpText, // address of text in message box
LPCTSTR lpCaption, // address of title of message
box
UINT uType // style of message box
);
-
MessageBox(NULL,"The text","The Caption", 0); works very nicely for
really simple message boxes -- as for debugging purposes.
TStringGrid
-
This is good for lots of rows & columns for a "spreadsheet" look.
-
The Cells property deals with the individual text in the cells -- more
on this later
-
Use stringgrid->Cells[Col][Row]="The text to include";
-
Adjust ColWidths[Col] or RowHeights[Row] programmatically
-
Lots of stuff is under options.
-
Set RowSizing and ColSizing = true to resize at runtime.
-
Set Editing = true to edit the cells at runtime.
TDBLookupComboBox
In this case I will provide an example in italics.
-
Provide the user with a convenient drop-down list of lookup items for filling
in fields that require data from another dataset. A field in table
Part is WarrantyNum, a foreign key from table Warranty.
-
Set the DataSource property to the data source that will receive the user’s
selection. Table Part.
-
Set the DataField to the field to receive the user’s selection.
Part.WarrantyNum.
-
Set the ListSource property to the data source of the table holding the
lookup items. Table Warranty.
-
Set KeyField to the field you want copied into DataField. WarrantyNum
-
Set ListField to display a field other than KeyField in the combo box.
WarrantyTerms, b/c the WarrantyNum makes no sense to the user.
TDBLookupListBox
This is the same as the TDBLookupComboBox,
except it is in ListBox format, not ComboBox format -- that is it doesn't
do a drop - down list.
TDBComboBox
-
This control allows the user to change the value of the field of the current
record in a dataset either by selecting an item from a list or by typing
in the edit box part of the control.
-
Set the DataSource and DataField properties to specify the table &
field to edit.
-
Use the Items property -- a TStrings property -- to change the list of
selectable items.
-
To determine if a particular item in the list of strings that makes up
the Items property for a combo box is selected, use the Selected property.
-
The ItemIndex property determines which item is selected, if any.
TDataSet
-
void __fastcall AppendRecord(const System::TVarRec *Values, const int Values_Size);
-
Call AppendRecord to create a new, empty record at the end of the dataset,
populate it with the field values in Values, and post the values
to the database.
-
The newly appended record becomes the current record.
-
Customer->AppendRecord(ARRAYOFCONST((CustNoEdit->Text,CoNameEdit->Text,AddrEdit->Text,
Null, Null, Null, Null, Null, Null,DiscountEdit->Text))); //This
statement appends a record to the Customer table. Note that Nulls are entered
for some of the values, but are not required for missing values at the
end of the array argument, i.e. after the Discount field.
-
void __fastcall Append(void);
-
Call Append to:
-
1 Open a new, empty record at the end of the dataset.
-
2 Set the current record to the new record.
-
__property System::Variant FieldValues[System::AnsiString FieldName];
-
Use FieldValues to read and write values for fields in a dataset.
-
FieldName is the name of a field to read from or write to, and must be
enclosed in double quotes.
-
FieldValues accepts or returns a Variant type, so it can be used with fields
of any type. return qLogin->FieldValues["EnterService"];
-
bool __fastcall Locate(const System::AnsiString KeyFields, const System::
Variant &KeyValues, TLocateOptions Options);
-
Call Locate to search a dataset for a specific record and position the
cursor on it.
-
KeyFields is a string containing a semicolon-delimited list of field names
on which to search.
-
KeyValues is a variant array containing the values to match in the key
fields. To specify multiple search values, pass KeyValues as a variant
array as an argument, or construct a variant array on the fly using the
VarArrayOf routine.
-
Options is a set that optionally specifies additional search latitude when
searching on string fields. If Options contains the loCaseInsensitive setting,
then Locate ignores case when matching fields. If Options contains the
loPartialKey setting, then Locate finds the first record that fulfills
at least some initial part of the KeyValues criteria for record matching.
If Options is an empty set, or if the KeyFields are not string fields,
Options is ignored.
-
Locate returns true if it finds a matching record, and makes that record
the current one. Otherwise Locate returns false.
-
Locate uses the fastest possible method to locate matching records. If
the search fields in KeyFields are indexed and the index is compatible
with the specified search options, Locate uses the index. Otherwise Locate
creates a filter for the search.
-
The following code illustrates a search on multiple columns using multiple
search values and partial-key matching:
{
Variant tmp(OPENARRAY(int, (0,1)), vtInteger);
tmp << (int)("Sight Diver");
tmp << (int)("P");
VarArrayOf(&tmp,1); }
-
A more practical example:
Variant V(AComboBox->Items->Strings[ItemIndex]);
TLocateOptions Op;
Op << 0;
Op << 0;
MyDataSet->Locate(FillField,V,O);
TDataSource
-
Use TDataSource to provide a conduit between a dataset and data-aware controls
on a form that enable display, navigation, and editing of the data underlying
the dataset.
-
All datasets must be associated with a data source component if their data
is to be displayed and manipulated in data-aware controls.
-
Similarly, each data-aware control needs to be associated with a data source
component in order for the control to receive and manipulate data.
Data source components also link datasets in master-detail relationships.
-
The OnDataChange event is especially useful in applications that must synchronize
data display in controls that are not data aware.
Disclaimer
All the information herein contained is provided for your convenience.
It is accurate as far as I know and have tested on my version of Borland
CBuilder -- 1.0 Professional. If some of it is not accurate, please
tell me. I'll post
the correction as soon as I get the chance. Until then, hopefully
the good information will help more than the misinformation will frustrate.
Happy coding.