Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to create a TStringGrid with one column of runtime created buttons in it Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
04-Nov-02
Category
VCL-General
Language
Delphi 2.x
Views
146
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I want the following: A StringGrid with 1 column of buttons in it. The number of 
rows in the grid is not known at design time, so the buttons are created at runtime.

Answer:

TSpeedButton will work and you won't have to worry about the TabStop. The problem 
with using the Rect that comes in as a param, it doesn't hit all the cells in the 
column. So what you end up with is buttons displaying in the wrong cells. If it 
doesn't matter, then you're ok. But if it does, then you'll need to update the 
entire column for all the visible cells. Here's what I came up with:

1   { ... }
2   var
3     HelpButtons: array of TSpeedButton;
4   
5   procedure Form1.CreateTheButtons;
6   var
7     i: Integer;
8   begin
9     SetLength(HelpButtons, ParamGrid.RowCount - 1);
10    for i := 0 to ParamGrid.RowCount - 2 do
11    begin
12      HelpButtons[i] := TSpeedButton.Create(Self);
13      HelpButtons[i].Visible := False;
14      HelpButtons[i].Parent := ParamGrid;
15      HelpButtons[i].Caption := IntToStr(i) + ' ?';
16      HelpButtons[i].Width := 34;
17      HelpButtons[i].Height := 18;
18      HelpButtons[i].Tag := i;
19      HelpButtons[i].OnClick := ParamGridButtonClick;
20    end;
21    {Force the buttons to show}
22    ParamGrid.Refresh;
23  end;
24  
25  procedure TForm1.ParamGridDrawCell(Sender: TObject; ACol, ARow: Integer;
26    Rect: TRect; State: TGridDrawState);
27  
28    procedure UpdateTheColumn;
29    var
30      i: Integer;
31      R: TRect;
32    begin
33      for i := ParamGrid.TopRow to (ParamGrid.VisibleRowCount + ParamGrid.TopRow) do
34      begin
35        if i >= ParamGrid.RowCount then
36          Break;
37        R := ParamGrid.CellRect(2, i);
38        HelpButtons[i - 1].Top := R.Top;
39        HelpButtons[i - 1].Left := R.Left;
40        if not HelpButtons[i - 1].Visible then
41          HelpButtons[i - 1].Visible := True;
42      end;
43    end;
44  
45  begin
46    if Length(HelpButtons) = 0 then
47      Exit;
48    if not FRefresh then
49      Exit;
50    if ((ACol = 2) and (ARow > 0)) then
51    begin
52      UpdateTheColumn;
53    end;
54  end;
55  
56  procedure TForm1.ParamGridButtonClick(Sender: TObject);
57  begin
58    ShowMessage('Click ' + Sender.ClassName + ' ' + IntToStr(TControl(Sender).Tag));
59  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC