If at any time a cell is selected, you can get the rectangular dimension of that cell using the CellRect() method. Its syntax is: TRect __fastcall CellRect(int ACol, int ARow); The arguments, ACol and ARow, represent the column index and the row index of the cell that has focus. This method returns the TRect rectangle of the cell. You can call this method when the user clicks a cell in the string grid. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { StringGrid1->ColWidths[0] = 72; StringGrid1->ColWidths[1] = 54; StringGrid1->ColWidths[2] = 35; StringGrid1->ColWidths[3] = 75; StringGrid1->RowHeights[0]= 15; StringGrid1->RowHeights[1]= 22; StringGrid1->RowHeights[2]= 10; StringGrid1->RowHeights[3]= 35; } //--------------------------------------------------------------------------- This would produce:
As mentioned already, the string grid a Windows control that has its origin in the TWinControl class. As a result, when a grid is clicked, it fires the OnClick event. When the user clicks a cell, you can call the CellRect() method to get the rectangular box of that cell and do whatever you want with it. Here is an example: void __fastcall TForm1::StringGrid1Click(TObject *Sender) { TRect Recto = StringGrid1->CellRect(StringGrid1->Col, StringGrid1->Row); int Area = Recto.Width() * Recto.Height(); Label1->Caption = Area; } //---------------------------------------------------------------------------
If the mouse is positioned or passing somewhere on or
over the string grid and you want to know void __fastcall MouseToCell(int X, int Y, int &ACol, int &ARow); This method is usually used on a mouse event such as OnMouseDown(), OnMouseMove(), and OnMouseUp() as these events provide the mouse coordinates. The MouseToCell() method retrieves the horizontal and vertical coordinates of the mouse, translates that position to the column and row indexes of the cell under the mouse and return those values. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { int x, y; StringGrid1->MouseToCell(X, Y, x, y); Label1->Caption = L"Col: " + UnicodeString(x) + L" Row: " + UnicodeString(y); } //---------------------------------------------------------------------------
In the same way, sometimes when the user clicks a cell, you may want to find out what cell was clicked. To get this information, you can call the MouseCoord() method of the TCustomGrid class. The TCustomGrdi::MouseCoord() method returns an object of type TGridCoord, which is a structure: struct TGridCoord { int X; int Y; { in int X int Y; } }; The syntax of the TCustomGrdi::MouseCoord() method is: TGridCoord __fastcall MouseCoord(int X, int Y); This method also is usually used in a mouse event. It takes as arguments the mouse position. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { TGridCoord GC = StringGrid1->MouseCoord(X, Y); Label1->Caption = "Col: " + AnsiString(GC.X) + L" Row: " + AnsiString(GC.Y); } //--------------------------------------------------------------------------- Just before the user selects the content of a cell, the control fires the OnSelectCell() event. Its syntax is: void __fastcall OnSelectCell(TObject *Sender, int ACol, int ARow, bool &CanSelect) The ACol and ARow arguments represent the cell that is about to be selected. The CanSelect argument allows you to specify whether the user is allowed to select the content of the cell. This event allows you to decide whether the cell can be selected or not. The following event is used to let the user know that a certain cell cannot be accessed: //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1SelectCell(TObject *Sender, int ACol, int ARow, bool &CanSelect) { if( ACol == 2 && ARow == 4 ) { ShowMessage(L"The content of this cell is not accessible.\n" L"Please select another cell!"); return; } } //--------------------------------------------------------------------------- If the user has selected a cell and wants to edit its content, you can find out the content of such a cell using the OnGetEditText() event: void OnGetEditText(TObject *Sender, int ACol, int ARow, UnicodeString &Value) This even fires as soon as the user has selected text included in a cell but just before the user has had a chance to edit it. This means that you can determine whether the user is allowed to change the contents of a particular cell. When this event fires, it communicates the grid coordinates of the cell that was clicked, allowing you to retrieve the content of that cell and do what you want. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int ACol, int ARow, AnsiString &Value) { UnicodeString Content = StringGrid1->Cells[ACol][ARow]; Label1->Caption = Content; } //--------------------------------------------------------------------------- On the other hand, when the user has changed the content of a cell, the string grid fires an OnSetEditText() event: void OnSetEditText(TObject *Sender, int ACol, int ARow, const UnicodeString Value) This is a good place to validate, accept, or reject the changes that the user has performed. This event also provides you with the grid coordinates of the cell whose contents the user has modified. To better control what type of text the user is allowed to enter in a cell, in all cells of a particular row, or in all cells of a particular column, you can use the OnGetEditMask() event. Its syntax is: void __fastcall OnGetEditMask(TObject *Sender, int ACol, int ARow, AnsiString &Value) The ACol and the ARow parameters represent the grid indexes of the cell. The Value is a string of the same type used for the EditMask property of the mask edit control. This event is used to set the EditMask needed for a particular cell. The following event restricts only US Social Security Numbers in all cells of the second column: //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1GetEditMask(TObject *Sender, int ACol, int ARow, AnsiString &Value) { if( StringGrid1->Col == 2 ) Value = "000-00-0000"; } //--------------------------------------------------------------------------- If you had let the compiler know that you would set the appearance of cells yourself, which would have been communicated by setting the DefaultDrawing property to False, you can use the OnDrawCell event to perform this customization. The syntax of this event is: void __fastcall StringGridDrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State) The cell whose characteristics need to be set is cell[ACol][ARow]. This means that you can locate any cell in the grid and set its properties as you like and as possible. For example, you can change the individual background color of a cell. The following code changes the background color of cell[3][2] to blue: //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State) { if( ACol == 3 && ARow == 2 ) { StringGrid1->Canvas->Brush->Color = clBlue; StringGrid1->Canvas->FillRect(Rect); } } //--------------------------------------------------------------------------- In the same way, you can change the text color of any cell of your choice independently of the other cells. The Rect parameter is the location and dimension of the cell whose characteristics you want to change. The State argument is a member of the TGridDrawState set which is defined as follows: enum Grids__3 { gdSelected, gdFocused, gdFixed }; typedef Set<Grids_3, gdSelected, gdFixed> TGridDrawState;arajust" This set allows you to examine the state of a particular cell. Because this value is a set, a particular cell can have more than one of these values. If a cell is selected, which by default gives it a background color different than the others, then its State contains the gdSelected value. If a cell has focus, which could mean that the user has just clicked it, sometimes to edit, the cell has the gdFocused value. Note that a cell can be selected and have focus, which means it would have both gdSelected and gdFocused. If a cell is a fixed cell as we described previously, then the cell has the gdFixed value. Here is an example of using the OnDrawCell event to customize the appearance of a string grid: //--------------------------------------------------------------------------- #include <vcl.h> #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { StringGrid1->DefaultDrawing = False; } //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State) { if( State.Contains(gdFixed) ) { StringGrid1->Canvas->Brush->Color = static_cast<TColor>(RGB(255, 155, 0)); StringGrid1->Canvas->Font->Style = TFontStyles() << fsBold; StringGrid1->Canvas->Font->Color = static_cast<TColor>(RGB(250, 245, 135)); StringGrid1->Canvas->Rectangle(Rect); } else if( State.Contains(gdSelected) ) { StringGrid1->Canvas->Brush->Color = static_cast<TColor>(RGB(255, 205, 155)); StringGrid1->Canvas->Font->Style = TFontStyles() >> fsBold; StringGrid1->Canvas->Font->Color = clNavy; StringGrid1->Canvas->FillRect(Rect); } else { StringGrid1->Canvas->Brush->Color = clWhite; StringGrid1->Canvas->Font->Color = clBlue; StringGrid1->Canvas->FillRect(Rect); } StringGrid1->ColWidths[0] = 15; StringGrid1->ColWidths[1] = 75; StringGrid1->ColWidths[2] = 75; StringGrid1->ColWidths[3] = 90; StringGrid1->ColWidths[4] = 120; StringGrid1->RowHeights[0] = 16; StringGrid1->RowHeights[1] = 16; StringGrid1->RowHeights[2] = 16; StringGrid1->RowHeights[3] = 16; StringGrid1->RowHeights[4] = 16; UnicodeString text = StringGrid1->Cells[ACol][ARow]; StringGrid1->Canvas->TextRect(Rect, Rect.Left, Rect.Top, text); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { StringGrid1->Cells[0][1] = "1"; StringGrid1->Cells[0][2] = "2"; StringGrid1->Cells[0][3] = "3"; StringGrid1->Cells[0][4] = "4"; StringGrid1->Cells[1][0] = "First Name"; StringGrid1->Cells[2][0] = "Last Name"; StringGrid1->Cells[3][0] = "Phone Number"; StringGrid1->Cells[4][0] = "Email Address"; StringGrid1->Cells[1][1] = "Alex"; StringGrid1->Cells[2][1] = "Walters"; StringGrid1->Cells[3][1] = "(202) 133-7402"; StringGrid1->Cells[4][1] = "waltersa88@yahoo.com"; StringGrid1->Cells[1][2] = "Bertrand"; StringGrid1->Cells[2][2] = "Kumar"; StringGrid1->Cells[4][2] = "kumarb@mailman.com"; StringGrid1->Cells[3][3] = "Hermine"; } //---------------------------------------------------------------------------
|
|
|||||||||
|