Board index » cppbuilder » Selecting (ranges of) Cells and Editing Cells doesn't work quite right

Selecting (ranges of) Cells and Editing Cells doesn't work quite right

Please consider the appended code (I have included both the header and the
implementation for the main form of a trivial test program).

This problem appears with TStringGrid, and grids derived from it.  And it
appears the problem arises from the grid options goEditing and goRangeSelect
not playing nicely together.

What I see happening is that when I start typing, the first character typed
is lost.  I do not understand this since my KeyPress event handler does not
alter the value of 'Key' at all.

I have been told that if I pressed '<F2>', I could then select a position
within the text with a cell, but that does not seem to work at all.  it
seems that all of the function keys are ignored by the KeyPress handler (but
since I do not use the value of 'Key', I have not examined it's contents): I
am guessing this because using a function key to try to toggle the grid
options into editing mode has no apparent effect: the first character
entered after the function key does not appear in the cell.  Just as
annoying, the contents of the cell are selected in their entirety, and
completely replaced by the second and subsequent characters I type.  I have
not found a way to position the cursor within the text within a cell.

What I would like to be able to do is seemlessly support allowing: 1) the
user to select any range within the grid (upon which a number of the
functions I provide would operate), 2) allow the user to begin editing
within the selected cell, as long as there is only one cell, and have all
printable characters entered appear in the cell (i.e. I do not want to lose
the first character entered), and 3) allow the user to use <F2> to begin
editing within the selected cell, as long as there is only one cell, and use
the mouse to either place the cursor within any text there may be within the
cell or select any or all of the text present within the cell (without
ending the editing session).

Can anyone help me sort this out?  I suspect I have missed something simple,
...

Thanks,

Ted

r.ted.by...@sympatico.ca

//--------------------------------------------------------------------------
-

#ifndef GridEditFormH
#define GridEditFormH
//--------------------------------------------------------------------------
-
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Grids.hpp>
//--------------------------------------------------------------------------
-
class TForm1 : public TForm
{
__published: // IDE-managed Components
  TStringGrid *ReferenceGrid;
  void __fastcall ReferenceGridKeyPress(TObject *Sender, char &Key);
  void __fastcall ReferenceGridSelectCell(TObject *Sender, int ACol,
          int ARow, bool &CanSelect);
private: // User declarations
  bool  weAreEditing;
public:  // User declarations
  __fastcall TForm1(TComponent* Owner);

Quote
};

//--------------------------------------------------------------------------
-
extern PACKAGE TForm1 *Form1;
//--------------------------------------------------------------------------
-
#endif
========================================================
//--------------------------------------------------------------------------
-

#include <vcl.h>
#pragma hdrstop

#include "GridEditForm.h"
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner), weAreEditing(false)
{

Quote
}

//--------------------------------------------------------------------------
-

void __fastcall TForm1::ReferenceGridKeyPress(TObject *Sender, char &Key)
{
  // don't reset if we are already editing
  if ( (!weAreEditing ) &&
    // make sure we have selected only one cell
    (ReferenceGrid->Selection.Right == ReferenceGrid->Selection.Left) &&
    (ReferenceGrid->Selection.Bottom == ReferenceGrid->Selection.Top) ) {
    weAreEditing = true;
    ReferenceGrid->Options = ReferenceGrid->Options << goEditing;
  };

Quote
}

//--------------------------------------------------------------------------
-
void __fastcall TForm1::ReferenceGridSelectCell(TObject *Sender, int ACol,
      int ARow, bool &CanSelect)
{
  if (weAreEditing) {
    ReferenceGrid->Options = ReferenceGrid->Options >> goEditing;
    weAreEditing = false;
  };
Quote
}

//--------------------------------------------------------------------------
-
 

Re:Selecting (ranges of) Cells and Editing Cells doesn't work quite right


OOPS: sorry, this belongs in vcl.components.using, but I didn't notice I was
in the wrong newsgroup when I created it.

Sorry,

Ted

Other Threads