Problem with TStringGrid

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Post Reply
allanoptical
Posts: 4
Joined: Tue Jan 05, 2021 9:41 am

Problem with TStringGrid

Post by allanoptical »

Please excuse what I fear is a rather simple question.

Using C++ Builder 10.3 Community edition I have a very simple application. It is the SDIapp template with just one VCL TStringGrid placed on it.
Entering text into a cell does not result in it showing. Using the up/down and left/right keys causes the selected cell to be changed and a very small function will respond to the KeyPress event but the characters to not appear in the cell.

I suspect I have inadvertently messed up a setting in the property settings but cannot find it. Any help would be very welcome, thanks.
rlebeau
BCBJ Author
BCBJ Author
Posts: 1759
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: Problem with TStringGrid

Post by rlebeau »

allanoptical wrote: Tue Jan 05, 2021 10:10 am Entering text into a cell does not result in it showing.
Entering HOW exactly? Via code, using the Cells property? Or via user input, using the built-in inplace editor?
allanoptical wrote: Tue Jan 05, 2021 10:10 am the characters to not appear in the cell.
Do you, by chance, have an OnDrawCell event handler assigned, that is not drawing the text? Or, maybe, have you set the DefaultDrawing property to false?
allanoptical wrote: Tue Jan 05, 2021 10:10 am I suspect I have inadvertently messed up a setting in the property settings but cannot find it.
Hard to say without seeing your actual code and property settings.
Remy Lebeau (TeamB)
Lebeau Software
allanoptical
Posts: 4
Joined: Tue Jan 05, 2021 9:41 am

Re: Problem with TStringGrid

Post by allanoptical »

Sorry for a badly formed question, thanks for the comments.

I wish to enter data from the keyboard

The Default drawing property is True. The only event handler defined is the KeyDown event.

To focus on the problem I have created the simplest application which comprises simply Form1 and StringGrid1. The only code I have added is:

void __fastcall TForm1::StringGrid1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
static int i = 0 ;
wchar_t buf[10];

swprintf(buf, L"%d", i);
StringGrid1->Cells[1][1] = buf ;

i++ ;
}

I have not altered any of the properties of the StringGrid after placing it on the form.

The code shows that every key press I have tried is actually responded to by the code, just nothing appears in the cell.
rlebeau
BCBJ Author
BCBJ Author
Posts: 1759
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: Problem with TStringGrid

Post by rlebeau »

allanoptical wrote: Thu Jan 07, 2021 9:21 am The code shows that every key press I have tried is actually responded to by the code, just nothing appears in the cell.
Are you wanting the text to appear in the currently selected cell? Because right now, you are sending the text to the same cell 1x1 regardless of whether it is actually selected or not. If you want to update the selected cell, you need to use the grid's Col and Row properties.

Also, you don't need the wchar_t[] buffer at all (besides, it is not large enough to hold every possible int value - your code will overflow the buffer once the int reaches 1,000,000,000). You can assign an int directly to a String and it will convert the value to text for you.

Code: Select all

void __fastcall TForm1::StringGrid1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
    static int i = 0;
    StringGrid1->Cells[StringGrid1->Col][StringGrid1->Row] = i++;
}
Remy Lebeau (TeamB)
Lebeau Software
allanoptical
Posts: 4
Joined: Tue Jan 05, 2021 9:41 am

Re: Problem with TStringGrid

Post by allanoptical »

Thank you for your reply.

What I want to do is use the String Grid as an array of edit boxes where the user can enter and edit data using the keyboard and mouse whilst the programme can display data to the user.

The code I quoted was simply intended to prove to me that the key strokes were being received by the StringGrid component.

It appears I am either misusing the StringGrid component or it is not the appropriate one to use. Suggestions either way will be very much appreciated.
rlebeau
BCBJ Author
BCBJ Author
Posts: 1759
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: Problem with TStringGrid

Post by rlebeau »

allanoptical wrote: Fri Jan 08, 2021 10:58 am What I want to do is use the String Grid as an array of edit boxes where the user can enter and edit data using the keyboard and mouse whilst the programme can display data to the user.
This is not how TStringGrid should be used for that purpose. Don't capture individual keystrokes and populate the cells manually at all. Use the built-in cell editor instead. Look at the grid's EditorMode property, and the goEditing and goAlwaysShowEditor flags in the Options property. Let the editor do all of the work for you (it displays an actual Edit control on top of the grid, which the user can then type into).
Remy Lebeau (TeamB)
Lebeau Software
allanoptical
Posts: 4
Joined: Tue Jan 05, 2021 9:41 am

Re: Problem with TStringGrid

Post by allanoptical »

Many thanks, that has solved my problem.

Sorry my question was badly phrased which wasted some of your time.
Post Reply