PostHeaderIcon Calendar and Combo Box Components in C++ Builder

Calendar in Mobile Platforms

C++ Builder uses the TDateEdit component to wrap a calendar component or datepicker for the mobile target platform.

To use the TDateEdit component, perform the following simple steps:

1. Select the TDateEdit component in the Tool Palette, and drop the component onto the Form Designer. To find the component in the Tool Palette, enter the first few characters (such as “dat”) in the search box;

2. After you drop the component, you can see the TDateEdit component on the Form Designer:;

3. Optionally, in the Object Inspector, you can set the following properties of TDateEdit:

  • ShowCheckBox: when true, displays a checkbox on the TDateEdit control. This checkbox allows you to enable/disable the TDateEdit control at run time.
  • ShowClearButton: when true, displays a button on the TDateEdit control. Click this button to clear values in this control at run time.

4. Run your application on either a simulator or your connected mobile device. After you tap TDateEdit, the calendar control appears, and you can select a date.

User Changes the Date

After the user changes the date, the OnChange event is fired. You can implement an event handler for the OnChange event to react to the user’s action.

To implement the OnChange event handler:

Select the TDateEdit component.
In the Object Inspector, open the Events page, and double-click the empty space next to OnChange.
Write code as follows:

//---------------------------------------------------------------------------
void __fastcall Tmy_form::my_dateeditChange(TObject *Sender)
{
    ShowMessage(FormatDateTime("dddddd", my_dateedit->Date));
}
//---------------------------------------------------------------------------

Combo Box for Pick Items List

To define a picker and the associated list items:
1. Select the TComboBox component in the Tool Palette, and drop it on the Form Designer.

2. After you drop the component, you can see the TComboBox component on the Form Designer.
3. Right-click the TComboBox component and select Items Editor…:
4. To define items, click Add Item several times.
5. In the Structure View, select ListBoxItem1 (the first item in the list).
6. In the Object Inspector, edit the Text property for ListBoxItem1.
In this example , enter “Zhejiang” as the first item in the list, Edit other items as well.
7. Select the TComboBox component and in the Object Inspector, set the TComboBox.Align property to Top.
8. Add a second TComboBox (ComboBox2) to the form. Select the TComboBox component in the Tool Palette, and drop it again on the Form Designer.
9. Select ComboBox2 and in the Object Inspector, set the TComboBox. Align property to Bottom.

Building a List of Items Using Code and Displaying a Specific Item

To build a list of items using code, you should implement the onFormCreate event handler in the following way:

//---------------------------------------------------------------------------
    my_autocomb->Items->Add("Tiger");
    my_autocomb->Items->Add("Cat");
//---------------------------------------------------------------------------

The currently selected item is specified by the ItemIndex property. ItemIndex is an integer value that is specified using a zero-based index (that is, the first item is zero).

To display the list with the third item selected (“Shanxi”), specify ItemIndex for my_combox as follows:

//---------------------------------------------------------------------------
    my_combox->ItemIndex=2;
//---------------------------------------------------------------------------

If you do not know the index value, you can find the value by using the IndexOf method. To display the my_autocomb with the item whose text is ‘Penguin’ selected, add the following line to the previous code:

//---------------------------------------------------------------------------
    my_autocomb->ItemIndex = my_autocomb->Items->IndexOf("Penguin");
//---------------------------------------------------------------------------

After the user selects an item, the OnChange event is fired. To respond to the user’s action, you can implement an event handler for the OnChange event.

Note: Before proceeding with this scenario, perform the following steps:

Select the TMemo component in the Tool Palette, and drop it on the Form Designer.
In the Object Inspector, set the TMemo.Align property to Client.

To implement an OnChange event handler:

Select the my_combox component.
In the Object Inspector, open the Events page, and double-click the empty space next to OnChange.
The Code Editor opens. Write code as follows:

//---------------------------------------------------------------------------
void __fastcall Tmy_form::my_comboxChange(TObject *Sender)
{
    my_memo->Lines->Insert(0, my_combox->Name+": Item "
    +my_combox->Selected->Text
    +" at Index "+ IntToStr(my_combox->ItemIndex));
}
//---------------------------------------------------------------------------

Now put them together:

void __fastcall Tmy_form::FormCreate(TObject *Sender)
{
    my_combox->ItemIndex=2;
    my_autocomb->ItemIndex=0;
    my_autocomb->Items->Add("Tiger");
    my_autocomb->Items->Add("Cat");
    my_autocomb->Items->Add("Penguin");
    my_autocomb->Items->Add("Bee");
    // Other animals can be listed here
    my_autocomb->Items->Add("Elephant");
    my_autocomb->Items->Add("Lion");
}
//---------------------------------------------------------------------------

void __fastcall Tmy_form::my_comboxChange(TObject *Sender)
{
    my_memo->Lines->Insert(0, my_combox->Name+": Item "
    +my_combox->Selected->Text
    +" at Index "+ IntToStr(my_combox->ItemIndex));
}
//---------------------------------------------------------------------------

14,278 views

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Copyright © 2010 - C++ Technology. All Rights Reserved.

Powered by Jerry | Free Space Provided by connove.com