以相反的顺序聚焦单元格 [英] Focus cells in reverse order

查看:277
本文介绍了以相反的顺序聚焦单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DBGRID中以相反的顺序聚焦单元格

如果按下按键将Tab键的动作反转为如何更改击键操作,如下:

How to change the keystroke action when keys are pressed to reverse action of the Tab key as follows:


  • Tab键---> Shift Tab;

  • Tab key ---> Shift Tab;

Shift Tab ---> Tab键;

Shift Tab ---> Tab key;

推荐答案

以下代码没有一个OnKeyUp处理程序,会执行您想要的

The following code, without an OnKeyUp handler, does what you seem to want

    type
      TMyDBGrid = class(TDBGrid);

    procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift:
        TShiftState);
    begin
      if Key = VK_Tab then begin
        Key := 0;
        if ssShift in Shift then
          DBGrid1.SelectedIndex := DBGrid1.SelectedIndex + 1
        else begin
          if TMyDBGrid(DBGrid1).Col = 1 then begin
            //  The following goes to the rightmost cell in the next row 
            //  if the focus is already on the leftmost column, as specified
            //  in the original version of the q
            DBGrid1.DataSource.DataSet.Next;
            TMyDbGrid(DBGrid1).Col := DBGrid1.Columns.Count;
          end
          else
            DBGrid1.SelectedIndex := DBGrid1.SelectedIndex - 1;
        end;
      end;
    end;

更新在对此的评论中,您要求进行更全面的实施。这里是:

Update In a comment to this, you asked for a fuller implementation. Here it is:

procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift:
    TShiftState);

  procedure TabForwards;
  begin
    if TMyDBGrid(DBGrid1).Col = DBGrid1.Columns.Count then begin
      DBGrid1.DataSource.DataSet.Next;
      if DBGrid1.DataSource.DataSet.Eof then
        DBGrid1.DataSource.DataSet.Append;
      TMyDbGrid(DBGrid1).Col := 1;
    end
    else
      DBGrid1.SelectedIndex := DBGrid1.SelectedIndex + 1;
  end;

  procedure TabBackwards;
  begin
    if DBGrid1.DataSource.DataSet.State = dsInsert then begin
       DBGrid1.DataSource.DataSet.Cancel;
       Exit;
    end;

    if TMyDBGrid(DBGrid1).Row = 1 then begin
      if TMyDBGrid(DBGrid1).Col = 1 then
        TMyDBGrid(DBGrid1).Col := DBGrid1.Columns.Count
      else
        DBGrid1.SelectedIndex := DBGrid1.SelectedIndex - 1;
    end
    else begin
      if TMyDBGrid(DBGrid1).Col = 1 then begin
        DBGrid1.DataSource.DataSet.Prior;
        TMyDbGrid(DBGrid1).Col := DBGrid1.Columns.Count;
      end
      else
        DBGrid1.SelectedIndex := DBGrid1.SelectedIndex - 1;
    end;
  end;

begin
  Caption := IntToStr(TMyDbGrid(DBGrid1).RowCount);
  if cbNormal.Checked then
    Exit;
  if Key = VK_Tab then begin
    Key := 0;
    if ssShift in Shift then
      TabForwards
    else
      TabBackwards;
  end;
end;

请注意,这使用 TabForwards TabBackwards 子程序,并有效地反转Tab和Shift Tab的角色。这样做的原因是因为它将思维与谈话分开,从产生它的关键组合中的运动行为,我发现更容易编写代码,并描述运动行为实际上是什么。

Notice that this uses TabForwards and TabBackwards sub-procedures and effectively reverses the roles of Tab and Shift Tab. The reason for this is because it separates thinking, and talking, about the movement behaviour from the key combination which produces it, which I found easier easier to code and to describe what the movement behaviour actually is.

使用Tab和Shift Tab作为标准,这是DBGrid的行为方式:

With Tab and Shift Tab behaving as standard, this is how the DBGrid behaves:


  1. 向前标签>
  1. Tabbing forwards

如果光标位于新记录行中,焦点移动到行中的下一个单元格,直到它到达RH列,然后将其包装到第一列。

If the cursor is in the New Record row, focus move to the next cell in the row until it reaches the RH column, then it wraps to the first column.

否则,焦点移动到当前行中的下一个单元格,直到它到达RH列,然后将它包装到下一行的第一列,如果有一个,否则它会在网格的数据集上调用附加,并移动到新记录行的第一列。

Otherwise, focus moves to the next cell in the current row until it reaches the RH column, then it wraps to the first column of the next row, if there is one, otherwise it calls Append on the grid's Dataset, and moves to the first column of the New Record row.

在新建记录行中,按Tab

In the New Record row, pressing the Tab Backwards key abandons the new record.


  1. 向后键入

如果光标位于新建记录行中,则新记录将被放弃。否则,焦点移动到当前行中的先前单元格,直到它到达LH列,然后将它包装到上一行的RH列。一旦达到第一行的LH列,它就会转到该行的RH列。

If the cursor is in the New Record row, the new record is abandoned. Otherwise, focus moves to the prior cell in the current row until it reaches the LH column, then it wraps to the RH column of the prior row. Once it reaches the LH column of the first row, it wraps to the RH column in that row.

这是第二个例子。

然而,当我到达最后一个单元格时,这不会在您的评论中说出您的评论

This does not, however, do what you said in your comment


DBgrid,tab键(not shift-Tab)需要打开新的记录。

when I reach last cell in DBgrid, the tab key (not shift-Tab) need to open new record.

这就是为什么:

您的方式,响应Tab键的网格的行为在最后一个单元格中与其他单元格不同。在其他单元格中,Tab将向左移动并向上卷到前一行,而在最后一个单元格中,它将向下移动。这将使我成为一个用户。无论如何,如果这真的是你想要的,你可以重新排列第二个例子,以这种方式行事。

Your way, the behaviour of the grid in response to the Tab key is different in the last cell compared with every other one. In the other cells, Tab will move left and wrap upwards to the prior row, whereas in the last cell it will move downwards. That would puzzle me as a user. Anyway, if that's really what you want, you can rearrange the second example to behave that way.

当以正常方式使用Tab和Shift Tab时,行为是如下:

When you use Tab and Shift Tab in the normal way, the behaviour is as follows:

这篇关于以相反的顺序聚焦单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆