Watch, Follow, &
Connect with Us

Welcome, Guest
Guest Settings
Help

Thread: Put StringGrid cell in edit mode



Permlink Replies: 6 - Last Post: Jun 20, 2017 4:32 PM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Dean Clark

Posts: 45
Registered: 6/12/08
Put StringGrid cell in edit mode
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 19, 2017 4:51 PM
We've been trying to get a TStringGrid to go to editing mode programmatically. Based on a key press, we set goEditing and goAlwaysShowEditor, and unset goRangeSelect, and we make sure only one cell is selected. However, the cell still requires the user to click on it before it can be edited. We would like to get directly to the editor without the additional mouse click. Is this possible?

Thanks!
Remy Lebeau (Te...


Posts: 8,477
Registered: 12/23/01
Re: Put StringGrid cell in edit mode
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 19, 2017 7:55 PM   in response to: Dean Clark in response to: Dean Clark
Dean Clark wrote:

We've been trying to get a TStringGrid to go to editing mode
programmatically.

And what have you tried so far? Please show your actual code.

Based on a key press, we set goEditing and goAlwaysShowEditor, and
unset goRangeSelect, and we make sure only one cell is selected.

However, the cell still requires the user to click on it before it can
be edited.

Does the TStringGrid have input focus when the key is pressed? Are you
using the StringGrid's own OnKeyPress event, or another event, like on
the Form instead?

We would like to get directly to the editor without the additional
mouse click. Is this possible?

Of course. Works fine for me when I try it:

procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    'e': begin
      StringGrid1.Options := StringGrid1.Options + [goEditing,
goAlwaysShowEditor] - [goRangeSelect];
    end;
    #13: begin
      StringGrid1.Options := StringGrid1.Options - [goEditing,
goAlwaysShowEditor] + [goRangeSelect];
    end;
  end;
end;


--
Remy Lebeau (TeamB)
Dean Clark

Posts: 45
Registered: 6/12/08
Re: Put StringGrid cell in edit mode
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 20, 2017 10:29 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Dean Clark wrote:

We've been trying to get a TStringGrid to go to editing mode
programmatically.
Of course. Works fine for me when I try it:

procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    'e': begin
      StringGrid1.Options := StringGrid1.Options + [goEditing,
goAlwaysShowEditor] - [goRangeSelect];
    end;
    #13: begin
      StringGrid1.Options := StringGrid1.Options - [goEditing,
goAlwaysShowEditor] + [goRangeSelect];
    end;
  end;
end;

Thanks Remy. Here's what doesn't seem to work for us:

void __fastcall TformPlotDataEditor::gridDataKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
    if (Key == VK_F2)
    {
        Key  = 0;
        //
        // Make sure just one cell is selected
        TGridRect r = gridData->Selection;
        r.Bottom = r.Top;
        r.Right = r.Left;
        gridData->Selection = r;
        //
        // Enable editing
        gridData->Options = gridData->Options >> goRangeSelect;
        gridData->Options = gridData->Options << goEditing << goAlwaysShowEditor;
    }


After this code, you still have to click on a cell to start editing. Have tried explicitly setting focus to the grid, setting the options before the selection. Made a 'cracker' class to post a EM_SETSEL message to the InplaceEditor directly (something I found online), but that usually raises an exception.

-Dean
Remy Lebeau (Te...


Posts: 8,477
Registered: 12/23/01
Re: Put StringGrid cell in edit mode
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 20, 2017 11:14 AM   in response to: Dean Clark in response to: Dean Clark
Dean Clark wrote:

Thanks Remy. Here's what doesn't seem to work for us:

Sorry, didn't notice this was a C++ group. My earlier Delphi example
would be the following in C++:

void __fastcall TForm1::StringGrid1KeyPress(TObject *Sender,
System::WideChar &Key)
{
    switch (Key)
    {
        case L'e':
            StringGrid1->Options = StringGrid1->Options << goEditing << 
goAlwaysShowEditor >> goRangeSelect;
            break;
 
        case 13:
            StringGrid1->Options = StringGrid1->Options >> goEditing >>
goAlwaysShowEditor << goRangeSelect;
            break;
    }
}


After this code, you still have to click on a cell to start editing.

The example you showed works fine for me. As soon as I press F2, I can
start typing into the selected cell without having the click on the
StringGrid.

Granted, there is no visual indication that the cell editor is now
active, but it is still editable nontheless.

--
Remy Lebeau (TeamB)
Dean Clark

Posts: 45
Registered: 6/12/08
Re: Put StringGrid cell in edit mode
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 20, 2017 1:46 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Dean Clark wrote:

Sorry, didn't notice this was a C++ group. My earlier Delphi example
would be the following in C++:

void __fastcall TForm1::StringGrid1KeyPress(TObject *Sender,
System::WideChar &Key)
{
    switch (Key)
    {
        case L'e':
            StringGrid1->Options = StringGrid1->Options << goEditing << 
goAlwaysShowEditor >> goRangeSelect;
            break;
 
        case 13:
            StringGrid1->Options = StringGrid1->Options >> goEditing >>
goAlwaysShowEditor << goRangeSelect;
            break;
    }
}

Thanks again Remy, we're getting partial love... If I remove the statement, Key = 0, then indeed typing is captured by the cell, even though it doesn't change appearance. Now to figure out a way to change appearance... :-)

-Dean
Dean Clark

Posts: 45
Registered: 6/12/08
Re: Put StringGrid cell in edit mode
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 20, 2017 4:06 PM   in response to: Dean Clark in response to: Dean Clark
Dean Clark wrote:
Remy Lebeau (TeamB) wrote:
Dean Clark wrote:

Sorry, didn't notice this was a C++ group. My earlier Delphi example
would be the following in C++:

void __fastcall TForm1::StringGrid1KeyPress(TObject *Sender,
System::WideChar &Key)
{
    switch (Key)
    {
        case L'e':
            StringGrid1->Options = StringGrid1->Options << goEditing << 
goAlwaysShowEditor >> goRangeSelect;
            break;
 
        case 13:
            StringGrid1->Options = StringGrid1->Options >> goEditing >>
goAlwaysShowEditor << goRangeSelect;
            break;
    }
}

The following seems to work setting editing mode and enabling the inplace editor:
    if (Key == VK_F2)
    {
        Key  = 0;
        //
        // Make sure just one cell is selected
        TGridRect r = gridData->Selection;
        r.Bottom = r.Top;
        r.Right = r.Left;
        //
        // Enable editing
        gridData->Options = gridData->Options << goEditing >> goRangeSelect;
        gridData->Selection = r;
        gridData->EditorMode = true;
        if (r.Top < gridData->TopRow || r.Bottom > gridData->TopRow + gridData->VisibleRowCount)
        {
            gridData->TopRow = std::max(1, r.Top - gridData->VisibleRowCount / 2);
        }
        ((TGridCracker *)gridData)->InplaceEditor->Perform(EM_SETSEL, 0, -1);
    }


The TGridCracker is very simple:

class TGridCracker : public TStringGrid
{
public:
	__property InplaceEditor;
};


Setting the EditorMode to true was the key to seeing the editor / cursor without having to click in the cell. Then the Perform call on the InplaceEditor highlighted the cell as expected.

I don't think the Key = 0 statement was relevant after all.

In any event it works nicely now, thanks for your help!

-Dean
Remy Lebeau (Te...


Posts: 8,477
Registered: 12/23/01
Re: Put StringGrid cell in edit mode
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 20, 2017 4:32 PM   in response to: Dean Clark in response to: Dean Clark
Dean Clark wrote:

gridData->EditorMode = true;

That is what I was looking for earlier and couldn't remember it.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02