Discussion:
String grid display problem
(too old to reply)
Harish
2008-07-01 06:30:46 UTC
Permalink
When the text "&100" is displayed in a
TStringGrid cell, it displays 100 with
a underline on 1. (Like a label caption that
has accelerator key underlined).
(Using && displays properly, but when user edits
the cell, the same && is displayed.)
This is undesired. Is it possible to make it display
without the underline?
-Harish
Mike Collins
2008-07-01 11:44:11 UTC
Permalink
Hay Harish,

You can use the OnGetEditText event to modify the string that is being
edited, for example, you can scan the value that is passed and look for the
&& and then remove one from the text that is going to be displayed for
editing purposes.

void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int ACol,
int ARow, AnsiString &Value)
{
if (Value.Pos("&&")>0)
Value = Value.SubString(2, Value.Length()-1);
}
- note, 30 second mock-up - i'm assuming here that my && is the first and
second character.

However, i can;t work out how you would then re-insert the && when the user
completes their editing...

HTH

Mike
Post by Harish
When the text "&100" is displayed in a
TStringGrid cell, it displays 100 with
a underline on 1. (Like a label caption that
has accelerator key underlined).
(Using && displays properly, but when user edits
the cell, the same && is displayed.)
This is undesired. Is it possible to make it display
without the underline?
-Harish
Remy Lebeau (TeamB)
2008-07-01 16:45:40 UTC
Permalink
Post by Mike Collins
void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int ACol,
int ARow, AnsiString &Value)
{
if (Value.Pos("&&")>0)
Value = Value.SubString(2, Value.Length()-1);
}
That code assumes that the accelerator is always at the front of the string.
But an accelerator can appear anywhere in the string. You need to take that
into account, ie:

void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int
ACol, int ARow, AnsiString &Value)
{
int pos = Value.Pos("&&");
if( pos > 0 )
Value = Value.SubString(1, pos) + Value.SubString(pos+2,
MaxInt);
}

Or:

void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int
ACol, int ARow, AnsiString &Value)
{
int pos = Value.Pos("&&");
if( pos > 0 )
Value.Delete(pos, 1);
}

Or:

void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int
ACol, int ARow, AnsiString &Value)
{
Value = StringReplace(Value, "&&", "&", TReplaceFlags());
}
Post by Mike Collins
However, i can;t work out how you would then re-insert the
&& when the user completes their editing...
void __fastcall TForm1::StringGrid1SetEditText(TObject *Sender, int
ACol, int ARow, const AnsiString Value)
{
int pos = Value.Pos("&");
if( pos > 0 )
StringGrid1->Cells[ACol][ARow] = Value.SubString(1, pos) + "&" +
Value.SubString(pos+1, MaxInt);
}

Or:

void __fastcall TForm1::StringGrid1SetEditText(TObject *Sender, int
ACol, int ARow, const AnsiString Value)
{
int pos = Value.Pos("&");
if( pos > 0 )
{
AnsiString tmp = Value;
tmp.Insert("&", pos);
StringGrid1->Cells[ACol][ARow] = tmp;
}
}

Or:

void __fastcall TForm1::StringGrid1SetEditText(TObject *Sender, int
ACol, int ARow, const AnsiString Value)
{
StringGrid1->Cells[ACol][ARow] = StringReplace(Value, "&", "&&",
TReplaceFlags());
}


Gambit
Mike Collins
2008-07-01 18:30:07 UTC
Permalink
Lol,

I did say that in my post:

- note, 30 second mock-up - i'm assuming here that my && is the first and
second character.

Mike
Remy Lebeau (TeamB)
2008-07-01 17:57:33 UTC
Permalink
Post by Mike Collins
- note, 30 second mock-up - i'm assuming here that my && is the first and
second character.
Ah, but even then, you still had a bug in the code. Let's say the "&&" was
in the middle of the string. Your SubString() code still thought it was at
the beginning instead, and would thus butcher the user's string. You would
have had to do one of the following instead:

void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int
ACol, int ARow, AnsiString &Value)
{
if (Value.Pos("&&") == 1)
Value = Value.SubString(2, Value.Length()-1);
}

void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int
ACol, int ARow, AnsiString &Value)
{
if (Value.SubString(1, 2) == "&&" )
Value = Value.SubString(2, Value.Length()-1);
}


Gambit
Mike Collins
2008-07-01 23:17:49 UTC
Permalink
LOL,

You are right and i was wrong'ish. It was really crappy code - i wrote
pre-coffee this morning and didn't really think anyone would take notice of
the actual content - just the fact that the Event occured. Lesson learned -
dont post crap :-)

Mike
Post by Remy Lebeau (TeamB)
Post by Mike Collins
- note, 30 second mock-up - i'm assuming here that my && is the first and
second character.
Ah, but even then, you still had a bug in the code. Let's say the "&&"
was in the middle of the string. Your SubString() code still thought it
was at the beginning instead, and would thus butcher the user's string.
void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int
ACol, int ARow, AnsiString &Value)
{
if (Value.Pos("&&") == 1)
Value = Value.SubString(2, Value.Length()-1);
}
void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int
ACol, int ARow, AnsiString &Value)
{
if (Value.SubString(1, 2) == "&&" )
Value = Value.SubString(2, Value.Length()-1);
}
Gambit
Remy Lebeau (TeamB)
2008-07-01 16:50:07 UTC
Permalink
Is it possible to make it display without the underline?
You would have to use the OnDrawCell event to draw the cells yourself, such
as by calling the DrawText() function with the DT_NOPREFIX flag specified,
ie:

void __fastcall TForm1::StringGrid1DrawCell(TObject* Sender, int ACol,
int ARow, TRect Rect, TGridDrawState State)
{
::DrawText(StringGrid1->Canvas->Handle,
StringGrid1->Cells[ACol][ARow].c_str(), -1, &Rect, DT_SINGLELINE |
DT_VCENTER | DT_NOPREFIX);
}


Gambit
Harish
2008-07-02 03:13:14 UTC
Permalink
Post by Remy Lebeau (TeamB)
You would have to use the OnDrawCell event to draw
the cells yourself, such as by calling the DrawText()
function with the DT_NOPREFIX flag specified,
It worked great, thanks!
-Harish

Loading...