Difference between revisions of "TStringGrid"

From Free Pascal wiki
Jump to navigationJump to search
Line 17: Line 17:
  
 
[[File:TStringGrid04.png]]
 
[[File:TStringGrid04.png]]
 +
 +
To add information to the StringGrid1 component, it is necessary to either add data from a TStream, LoadCVSFile, link the grid to a database or other similar actions.  If linking to a database there are other components that should be considered like the TDBGrid. Other components such as the OpenDialog may also assist using methods like the LoadCVSFile. In many cases, it is necessary to either directly link data to given cells or ranges. In our example, we will use the InsertRowWithValues method. It is now necessary to add to the ButtonAddFiles an Event by clicking the Events tab of the Object Inspector and selecting the 'OnClick' event.
 +
 +
[[File:TStringGrid05.png]]

Revision as of 20:46, 27 April 2016

TStringGrid is one of the Lazarus components available for use by developers. The objective of this page is to show a simple example illustrating some of the easily used properties and methods available for use. It can be found typically along the upper edge of the main Lazarus window below the file menus and IDE Coolbar. You may also have it opened as a components window opened by selecting from the menu View-->Components, or, if using anchor docking opened by you as part of the Lazarus IDE. The following image shows the TStringGrid component as it may look:

Media:TStringGrid01.png

To create this example create a new project in Lazarus. Select TStringGrid to add to the form, by clicking the TStringGrid component from the menu area or component window, then click on the form. In this case 2 buttons were also selected and dropped onto the form as shown. In this example it is also necessary to select an OpenDialog component and drop it onto the form.

Media:TStringGrid02.png

In this example, Button1's name was changed to ButtonAddFiles and Button2's name was changed to ButtonExit. The StringGrid1 was stretched out and the buttons were aligned as shown. Note that there is a row and a column that are of a different color. That state illustrates the concept that this row and column could be for title labels for their respective column or row. Of course this default state can be changed simply by changing the 'FixedCols' or 'FixedRows' in the Object Inspector.

Media:TStringGrid03.png

You can see in this case that the title lines have been changed and the StringGrid1 component has been anchored. This was achieved by taking two steps. The first one involves looking at the Object Inspector and selecting various properties as needed. One step that should be taken when starting out is to carefully note the default properties. When you have made numerous changes and need to go back, it is much easier if you know what their state was when you started or at various points along the way. The state of these properties in the last image illustrates only a single fixed row with column titles. This state illustrates FixedCols[0], FixedRows[1], HeaderHotZones[gzFixedCols], HeaderPushZones[gzFixedCols], Options[goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goSmoothScroll], TitleFont[Color[clPurple], Style[fsBold], and RowCount = 1. After viewing your work by clicking the Run button on Lazarus you may find it desirable to change these properties. In this case, the additional properties of ColClickSorts and AlternateColor was selected.

At the bottom of the Object Inspector you can find useful information about the properties shown as seen in this example:

TStringGrid04.png

To add information to the StringGrid1 component, it is necessary to either add data from a TStream, LoadCVSFile, link the grid to a database or other similar actions. If linking to a database there are other components that should be considered like the TDBGrid. Other components such as the OpenDialog may also assist using methods like the LoadCVSFile. In many cases, it is necessary to either directly link data to given cells or ranges. In our example, we will use the InsertRowWithValues method. It is now necessary to add to the ButtonAddFiles an Event by clicking the Events tab of the Object Inspector and selecting the 'OnClick' event.

TStringGrid05.png