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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print selection in string grid 2

Status
Not open for further replies.

MerryMadDad

Programmer
Dec 11, 2001
47
GB
Hello, could anyone help me with any of the following three questions please ?

1/ How do I print a selection of rows from a string grid

2/ add buttons to the string grid so that they always remain below whatever rows are displayed in the grid and move their position according to the size of the grid

3/ Print preview contents of string grid

any help much appreciated

Thanks

MMD
 
1. This should give you enough clues. Write your own fn() to get the desired layout.

procedure WriteToCanvas(ACanvas:TCanvas);
begin
for j:=RowStart to RowFinish do
for i:=ColStart to ColFinish do begin
LRect:=fn(i,j);
Canvas.TextRect(LRect,LRect.Left,LRect.Top,StringGrid1.Cells(i,j));
end;
end;

with Printer do begin
BeginDoc;
WriteToCanvas(Canvas);
EndDoc;
end;

2. Show a button on a grid

I'm not sure where you want the button, but this shodl help.

Place a Button on the form, I'll call in GridButton.

In the FormCreate:
GridButton.Parent:=StringGrid1;

In the StringGrid.OnDrawCell code like
if(ACol=2)and(ARow=3)then
TryOneButt.BoundsRect:=Rect;

StringGrid properties
ColCount
FixedCols
VisibleColCount
are probably useful to you


3. For preview

var
LForm:TForm;
begin
LForm:=TForm.Create(nil);
try
LForm.ClientWidth:=400;
LForm.ClientHeight:=400;
with TImage.Create(LForm)do begin
Parent:=LForm;
BoundsRect:=LForm.ClientRect;
WriteToCanvas(Canvas); {See answer 1}
end;
LForm.ShowModal
finally
LForm.Free;
end;
end;

Good luck
Simon
 
the problem with this code snippet is that the grid column where you want the button to appear flickers and you don't really see what's there... is there any sollution to avoid this?
 
I'm not sure what you mean by flickers.

Using the OnTopLeftChanged event rather than OnDrawCell produces a cleaner result. Code in StringGrid1TopLeftChanged like:

GridButton.BoundsRect:=StringGrid1.CellRect(2,3);

You also need to call the event from the FormCreate as the button would not otherwise be positioned until the grid scrolled.

Hope this helps
Simon
 
how do i do this with DBGrid? (these events don't exist with DBGrid)
 
Nightmare.

You may well need to sub class TDBGrid to surface the required properties, or even write them yourself.

Check out
TDBGrid.OnDrawColumnCell
TDataSource.OnDataChange
TDataSet.AfterScroll

Another approach that I have used successfully is to draw buttons as required onto the grid (in OnDrawColumnCell), then in OnMouseUp (from memory OnMouseDown did not work) use MouseCoord and some calcs to determine if my button had been clicked on.

With TDBGrid you run into brick walls fast, which is why there are so many third party grids available, virtually none of which are sub classed from TDBGrid.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top