Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can one AutoTab in a Grid? 2

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA
I need something like ....

if (grdMyGrid.SelectedIndex = 2) and (grdMyGrid.SelectedIndex = 'D') then
TabTo grdMyGrid.SelectedIndex = 6;

Which is to say having entered 'D' in column 2 the cursor automatically tabs to column 6.

Is this possible? Anyone?

Thanks in advance.
 
You can write an OnKeyUp event handler to achieve this. Note that when OnKeyUp is called the cursor has already moved to the next cell so we need to test if the cursor is in column 3.
It's not clear to me if you want this automatic tabbing to occur if the cell contains only a 'D' or if the last key entered in the cell is a 'D'. So you may need to modify the following code.
Code:
procedure TForm1.grdMyGridKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  with grdMyGrid do
    if (key=VK_TAB) and (Col=3) and (Cells[2,Row]='D') then
      Col := 6;
end;

Andrew
Hampshire, UK
 
Andrew!
You remain a great credit to this great website!
Thanks from downunder Mate!
 
Huston! We have a problem!!


With the following code ....


procedure TfrmCB.grdCBKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
with grdCB do
if (key=VK_TAB) and (Col=3)[col = b]X[/col] and (Cells[2,Row]='D') then
Columns := 6;
end;


....I get the following exceptions.


[Error] CB.pas(141): Undeclared identifier: 'Col'
[Error] CB.pas(141): Undeclared identifier: 'Cells'
[Error] CB.pas(141): Operator not applicable to this operand type


So I tried .....

procedure TfrmCB.grdCBKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
with grdCB do
if (key=VK_TAB) and (Columns=3) and (Cells[2,Row]='D') then
Columns := 6;
end;


.... to get


[Error] CB.pas(142): Incompatible types
[Error] CB.pas(142): Undeclared identifier: 'Row'
[Error] CB.pas(143): Incompatible types: 'TDBGridColumns' and 'Integer'

What am I missing?
 
To get the second example working again you need to change the if statement 'Columns' variable back to 'Col' as this is a property of the grid that you want. What were you trying to do in the first example (in your most recent post), particularly the part containing square brackets?

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
>you need to change the if statement 'Columns' variable >back to 'Col'

And then? Haven't you noticed that I say I get the following exceptions.


[Error] CB.pas(141): Undeclared identifier: 'Col'
[Error] CB.pas(141): Undeclared identifier: 'Cells'
[Error] CB.pas(141): Operator not applicable to this operand type

>What were you trying to do..

As at the top of this thread...!!
 
What I was specifically trying to ask was about the following code snippet:
Code:
 if (key=VK_TAB) and (Col=3)[b][col = b]X[/col][/b] and (Cells[2,Row]='D') then

I asked
What were you trying to do in the first example (in your most recent post), particularly the part containing square brackets?

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Andrew's example code seems to be designed for a TStringGrid rather than a TDBGrid. Try using "SelectedIndex" instead of "Col" and perhaps "SelectedField.DisplayLabel" instead of "Cells".

Hope this helps!

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Just returned from a short holiday.

Clive is correct (and deserves a star). I assumed a TStringGrid rather than a TDBGrid which was a bit silly of me as you specified SelectedIndex which is a TDBGrid property.

You could try the following:-
Code:
procedure TForm1.grdMyGridKeyUp(Sender: TObject; var Key: Word;
 Shift: TShiftState);
begin
 with grdMyGrid do 
  if (Key=VK_TAB) and (SelectedIndex=3) and (Fields[2].AsString='D') then
   SelectedIndex := 6;
end;
Note that you need to use Fields[2].AsString because SelectedField refers to Fields[3] (when SelectedIndex is 3) in the OnKeyUp handler.

Andrew
Hampshire, UK
 
Thanks Andrew!

That did the trick.




Terry
Melbourne.
Australia.
 
On holiday Andrew? That's unlike you!

Thanks for posting a corrected solution, I haven't really played around with TDBGrid before and could see that my suggestions were not exactly viable - that's why I didn't post a code solution.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top