开发者

Live update of StringGrid when horizontal scroll bar is moving?

开发者 https://www.devze.com 2023-01-23 23:38 出处:网络
In Delphi 2010, I need to display a grid that has a horizontal scroll bar with about 15 columns x 5 rows.

In Delphi 2010, I need to display a grid that has a horizontal scroll bar with about 15 columns x 5 rows.

I chose to use a StringGrid.

However, while the mouse button is down dragging the horizontal scroll bar, I want the grid to scroll live.

The StringGrid component, it appears, does not scroll live. It waits until the mouse button is released before updating the column and scrolling if necessary.

Also, the horizontal scroll bar button (is t开发者_如何转开发hat what it's called) is not proportional to the number of columns. And for a down-arrow when on the bottom row to move to the top of the next column to the right...

These seem like common needs, so I was surprised not to find them in TStringGrid.

Any suggestions on a way around these two problems? I can use a DbGrid or other standard component, but my preference is to not use a commercial grid if I can avoid it. And I'm not going to use shareware or freeware...

TIA


For the first question, you can set goThumbTracking in the StringGrid's Options at design-time, or at run-time:

StringGrid1.Options := StringGrid1.Options + [goThumbTracking];


For the third question, you can provide the functionality you need by using keyboard event handlers of the control. An example;

procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  StringGrid: TStringGrid;
begin
  StringGrid := Sender as TStringGrid;
  case Key of
    VK_DOWN:
      if StringGrid.Row = StringGrid.RowCount - 1 then begin
        Key := 0;
        StringGrid.Row := StringGrid.FixedRows;
        if StringGrid.Col = StringGrid.ColCount - 1 then
          StringGrid.Col := StringGrid.FixedCols
        else
          StringGrid.Col := StringGrid.Col + 1;
      end;
    VK_UP:    //...;
    VK_RIGHT: //;
    VK_LEFT:  //;
  end;
end;


For the second question, the scrolling code seems to be buried in private methods of TCustomGrid. I have no clue how to achieve that..


If noticed you are not interested in third party components - Freeware, I am not fond of these either, but we all must make sacrifices sometimes if we want to get the problems solved. This is one of these sacrifices! This component is to good to be ignored. You will not create something like it yourself if you don't have a couple of years of free time.

Either write a new component based on TStringGrid (I would not - it is not the best tool in the box to begin with)

But take some time and learn TVirtualStringTree. The component is years ahead of TStrignGrid. The source is available and there are many who uses it.

And there are events already implemented to react on scrollbar changes OnScroll, OnShowScrollbar

http://www.delphi-gems.com/index.php?option=com_content&task=view&id=12&Itemid=38

Search on stackoverflow and you can read much more about tvirtualstringtree


Second the suggestion to use TVirtualStringTree. Working with the TStringGrid component is like stabbing yourself in the belly with a rusty scissor.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号