180 likes | 186 Views
Learn how to create and use text controls, including label, edit, memo, listbox, and combobox controls, in a Windows programming environment. Explore their properties and methods for displaying, entering, and modifying text. Create a demo program to register stock and track the movement of parts in a machine factory.
E N D
0. • C++ programming in Windows environment Text controls in the program • Label control • Single line editor: Edit control • Multiple line editor: Memo control • ListBox control • ComboBox control • Demo program with text controls
1. • Label control The Label control is used for displaying oneline texts. It can not get the focus. Its content isdetermined by the Caption property. The letterscan be formatted by the Font property. If the Transparent property is true then the background having rectangle shape will be the same as the base surface. The Alignment property aligns it in this rectangle. • Single line editor: Edit control The one line editor can be used for displaying or entering, modifying texts. The text is stored in the Text property. If the ReadOnly property is true then the text is read-only, or can be modified from the program. The 0 value of MaxLength property means arbitrary length, a given value means a limit for the maximum length of entered text.
2. • Multiple line editor: Memo control The Memo control is capable for displaying, editing large, more dozen kByte length text. The text can be entered and be read continuously through the Text property, or can be used as set of lines through the Lines property. If the WantTabs and the WantReturns properties are true then the Tab key effective in the text and the Enter key wraps the text. The Modified property informs us about its changing. When ReadOnly = true the text can not be modified. In editing time the text content can be entered in the String List Editor after clicking the button in the row of Lines property.
3. • ListBox control The ListBox control stores the text as one line entries in the Namesstring vector. The Names is a data member of the Items property which is the instance of the TStrings class. The methods of TStrings class can be applied for handling the entries using an Items->Method(); form. Such methods are the Add(”Line”); for adding an item to the end of the list, the Insert(index, ”Line”); to insert the item to the position shown by the index, the Delete(index); to delete the item identified by the index, the IndexOf(”Line”); to get the index of "Line" or the fact that this item is missing from the list. The properties of TStrings class give information:the Count property gives the number of lines in the list, the ItemIndex property gives the index of the selected line,the Strings[index ] property gives access to that line of the list which is pointed by the index.
4. • ComboBox control When the MultiSelect property of ListBox is true then more lines can be selected at the same time, when the ExtendedSelect is true, then the Shift and Ctrl keys can be used to modify the selection too. The Selected[ ] array gives information about the selection, the elements, having the same indices as the selected lines, will be true. The true value of Sorted property indicates that the lines are sorted in ABC. The Combo Box control is a combination of an Edit control and a ListBox control. It can be used advantageously in such cases when the entering has to be performed with selecting an item from a list, or in such case when in addition to the previously mentioned the possibility to enter a new item is required.
5. The combo control may have different shapes depending on the value of Style property: Style = csSimple : the window of list always appears in a length given by the Height property. Style = csDropDown : the list window has to be dropped down by the button shown at the end of the edit line, or calling the Show( )method. Style = csDropDownList : the entering can only be made selecting from the list.
6. The features of ComboBox correspond with the introduced features of Edit control and ListBox control, having rational differences, e.g. multiple selection is not allowed. Remarks on text controls: The text controls have other properties, methods and event handlers beyond the introduced ones, see the Help in Builder. There exist more, partially different text controls too. Such are: StaticText: similar to Label, but for special purposes. MaskEdit: similar to Edit, but the entered text can be controlled, masked and the output can be formatted. RichEdit: similar to Memo, but letters of different character sets can be mixed and gives paragraph formatting possibilities too. These extra features have to be programmed by us.
7. • Demo program with text controls Let us make a program applying Label, Edit, Memo, ListBox and ComboBox text controls! The program serves for registering the stock in the Central repo-sitory and the repository of the Assembly shop of a machine factory. New parts can be added to the stock of the Central repository, one or more parts can be transported from there to the repository of the Assembly shop where one or more parts can be taken out for assemblage. The motion of parts is logged by the system to make possible the recon- struction, retrieving. The stock of repositories can be displayed in order of adding or in sorted by ABC form. The structure of program window be the next:
8. Solution: 1. Create a folder named Repository, then save the newly created empty C++ Builder application into this using Repository.prj projectname! 2. Place three GroupBoxeson the Form! The role of these is to integrate visually the controls that are placed to these and to present the name of these collections. 3. Place two ListBox components on panels to store and display the stocks! 4. Take an Edit component onto the left GroupBox panel! 5. Place three buttons for performing adding, transporting and using! 6. Place a ComboBox component for selection of display mode! 7. Place a Memo component for displaying the logged events! 8. Lastly place three Label components for displaying the texts Stock records, Display mode: and Name of new part:. The selected component can be positioned precisely by the arrow keys if the Ctrl button is held down. E.g. theDisplay mode: text can be set to the same line where the content of the Edit control is.
9. The window with the placed and sized components: 9. Give the value for Caption property of Form1, Label1, Label2, Label3, GroupBox1, GroupBox2, Groupbox3, Button1, Button2 and Button3 components.
10. 10. Rename the Name property of the next components: Label2 -> LabelMode Label3 -> LabelNew GroupBox1 -> GroupBoxCentral GroupBox2 -> GroupBoxAssemblyShop GroupBox3 -> GroupBoxLog ListBox1 -> ListBoxCentral ListBox2 -> ListBoxAssemblyShop Memo1 -> MemoLog ComboBox1 -> ComboBoxMode Edit1 -> EditNew Button1 -> ButtonAdd Button2 -> ButtonTransport Button3 -> ButtonUse. 11. Set the style of the border of Form (BorderStyle) to bsDialog ! 12. Change the shape of the characters of Label1 to bold, size 10 points inFont property and justify it to centre by mouse!
11. 13. Write over the Text property of ComboBoxMode to Normal! 14. Delete the value of the Text property of EditNewsingle line text editor! 15. Enable multi selection capability for ListBoxCentral and ListBoxAssemblyShop list-controls, set the value of MultiSelect andExtendedSelect properties to true! 16. Delete its name from MemoLog multiline editor, for this double click on the button appearing in the line of Lines property and clear the text from the appearing String List Editor! 17. Disable the editing capability for MemoLog setting its ReadOnly property to true because this control has displaying task only. 18. The content of MemoLog text control can be scrolled by keyboard but because of aiming mouse-oriented handling turn on the vertical scroll bar setting the ScrollBar property to ssVertical! 19. The events of repositories have to be logged. These events are identified with indices in the memo control, so define a memoCounter variable in the beginning of the unit1.cpp file after the TForm1 *Form1; line: int memoCounter = 0;
12. • 20. Create the skeleton of the event handler of clicking on the Add button by double clicking on the button! The result: • void __fastcall TForm1::ButtonAddClick(TObject *Sender) • { • } • 21. Write the code for adding the content of EditNew single line editor to the end of the ListBoxCentral list control in the block of the function, and log the event in the MemoLog multiline display! • void __fastcall TForm1::ButtonAddClick(TObject *Sender){ AnsiString s; if (EditNew->Text.Length() > 0) // if there is some text { ListBoxCentral->Items->Add(EditNew->Text); // adding to list //Logging: memoCounter++; s= memoCounter; // operator=( ) works! s += ". Adding new part to the stock of the Central repository: "; s += EditNew->Text; MemoLog->Lines->Add(s); // the memo is handled as set of lines now } • }
13. 22. Create the skeleton of the event handler function of the Transport button double clicking on the button, then insert the code lines for transporting the selected items and logging the motion: void __fastcall TForm1::ButtonTransportClick(TObject *Sender){ AnsiString s; for (int i=0; i < ListBoxCentral->Items->Count; i++) if ( ListBoxCentral->Selected[ i ] ) { ListBoxAssemblyShop->Items->Add(ListBoxCentral->Items->Strings[ i ] );//Logging: memoCounter++; s= memoCounter; s+= ". Part transfer from Central repository to Assembly shop: "; s+= ListBoxCentral->Items->Strings[ i ]; MemoLog->Lines->Add(s); }// Deleting the transferred item from Central repository. // Could not be deleted above because it could destroy the indices and // number of parts, or not the upper would be transferred first... for (int i = ListBoxCentral->Items->Count-1; i >= 0; i--) if (ListBoxCentral->Selected[ i ]) ListBoxCentral->Items->Delete( i ); }
14. • 23. Create the skeleton of the Use button double clicking on it then insert the code for taking out the selected items that means deleting them from the list and write the code for logging this event: • void __fastcall TForm1::ButtonUseClick(TObject *Sender){ AnsiString s; for ( int i = ListBoxAssemblyShop->Items->Count-1; i >= 0; i--) if ( ListBoxAssemblyShop->Selected[ i ] ) { //Logging: • memoCounter++; s= memoCounter; • s += ". Use of a part from the repository of Assembly shop: "; • s += ListBoxAssemblyShop->Items->Strings[ i ]; • MemoLog->Lines->Add(s); • //Deleting: • ListBoxAssemblyShop->Items->Delete( i ); • } • }
15. 24. The ComboBoxMode component is intended to set the display mode of Central repository and AssemblyShop lists from normal to sorted or back. For this fill up the Items property of the ComboBoxMode component with the Normal and Sorted texts! These possibilities will be shown in the drop down list of the combo box. 25. Finally enter the code into the OnChange( ) event handler of theComboBoxMode component that will set the value of Sorted properties for the sorted or normal displaying in the stock lists. This event handler will be called when the content of the editor of the combo box has been changed. The skeleton of the event handler can be created double clicking on the combo list. The completed function is shown in the next page.
16. • TheOnChange() event handler function of theComboBoxModecomponent: • void __fastcall TForm1::ComboBoxModeChange(TObject *Sender) • { • if ( ComboBoxMode->Text == "Normal" ) { ListBoxCentral->Sorted= false; • ListBoxAssemblyShop->Sorted= false; • } • else • { ListBoxCentral->Sorted= true; • ListBoxAssemblyShop->Sorted= true; • }} • The program can be made better of course, e.g. the adding of the new part may happen by pushing the Enter key.
17. • The window of the running Stock records program: