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

How to pass from name to a unit to print stringgrids 1

Status
Not open for further replies.

nobbby

Programmer
Feb 5, 2006
2
GB
Hi can any one help I need to print stringgirds on different forms by using a separate unit how can I send the form name to the print unit

Cheers
nobbby
 
Why do you need the form name? Cant you solve the problem passing a pointer? How are you going to access the form having only the name? Have you more than one instance of your form?

BTW, the form name is in MyForm.Name.

buho (A).
 
I have a Form called movement and another called list both have stringgrids my print routine is in a unit called 'printunit' I want to do

with 'Form name' do (i.e movement or list)
begin
printat(15,15,StringGrid1.cells[0,1]);
end;

I get an Error] PrintUnit.pas(29): Undeclared identifier: 'StringGrid1'

Ps I am as you can see a beginner

*********************************************************
procedure PrintAt(Xpos,Ypos:integer;Temp:String);
begin
Ypos:=Ypos*(Printer.PageHeight div 2000);
Xpos:=Xpos*(Printer.PageWidth div 1000);
Printer.Canvas.Textout(Xpos,Ypos,Temp);
end;
//*********************************************************
 
I've not had a chance to test this, but then entry procedure in your print unit could take an object as a parameter (maybe called sender). The calling form could pass self as the parameter and the code in the print unit could use something like if sender.name = 'forma' then with sender as forma etc etc. or if sender is forma then with sender as forma etc. etc.

This would then give you access to the controls on forma


Hope this helps.

[vampire][bat]
 

All of us started as begginers :).

Your best option is to make a method receving not the form but the string grid itself.

Code:
procedure GridPrinter(AGrid : TStringGrid);
begin
  PrintAt(15, 15, AGrid.Cells[0,1]);
end;

Now you can call it from any form having a string grid:

procedure TForm1.PrintGrid;
begin
  GridPrinter(StringGrid1);
end;

What you are trying to do makes no sense in PASCAL; you can't access a form by name, you need to use a variable cointaining the form object (actually a pointer in disguise):

Code:
var
  MyForm : TForm;
begin
  MyForm := TForm.Create(...);
  // Now MyForm points to the object and you can use it.
  MyForm.SomeMethod;
  CallSomeProcedure(MyForm);
  MyForm.Free;
end;

Try to get a good book, you can find some ones for free in the net.

buho (A).







 
What you are trying to do makes no sense in PASCAL;
True. But Delphi doesn't use Pascal these days. Since at least v7, the language is called the Delphi language and not Pascal.
All objects which are created from classes descended from TComponent have a Name property and could be accessed using that Name although it is not usually a good idea to do so.

Andrew
Hampshire, UK
 
I do not use D7, but I'm curious: how can you diferentiate instances based on the TComponent name property?

buho (A).
 
The Name property is the name of the object not of the class.

Suppose you had a form, Form1, with ten TEdit controls on it, named Edit1 to Edit10. You could set the Text property of a particular TEdit control by referring to it by its Name property.

Here is some sample code:
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  GetEditInstance('Edit3').Text := 'Test';
end;

function TForm1.GetEditInstance(name: string): TEdit;
var
  i: integer;
begin
  for i := 0 to ComponentCount - 1 do
    if name = Components[i].Name then
      result := Components[i] as TEdit;
end;
Obviously not as efficient as
Code:
  Edit3.Text := 'Test';
but there may be circumstances when it might be useful.

Andrew
Hampshire, UK
 
Well...

I was wondering if D7 "Delphi Language" added some language construct able to manage things like "with MyForm.Name do".

For what you say, that still makes no sense neither in PASCAL nor "Delphi Languague".

What you are resorting to is not a language construct but a VCL architectural characteristic and you still can't access an object by the name property. You can check it and act in consecuence but not access it.

Please, disregard my question about how to differentiate instances based on the name property. I asked it based on the supposition that a new language contruct existed (wondering how the compiler itself can be related to a high level abstraction like the TComponent class).

buho (A).
 
buho

I agree that I am not describing a language construct but a feature of the VCL.

I suppose it depends on what one means by access. I suspect that we are simply disagreeing on the semantics of access.

I think there is a rough analogy with accessing fields in a database record using the TDataset method FieldByName.
Code:
  customer := dataset.FieldByName('Id').AsInteger;
as an alternative to
Code:
  customer := datasetId.AsInteger;
Using FieldByName is slower but there are circumstances when it is the best approach.
I would say that one can access an object, providing it is descended from TComponent, by its name property. Admittedly, one can't access it directly, but it can still be accessed. And for the vast majority of cases, using the name property is not the best solution.

I hadn't intended to imply a link betweenDelphi Language and the discussion about accessing objects. It would have been better if I'd inserted a blank line. Sorry.

Andrew
Hampshire, UK
 
Yep, may be we have a tongue/jargon issue here.

"Accessing", in my parlance, implies operating with a pointer to the entity, as Von Neumann precludes any other accessing mechanism, no matter Bill Gates efforts to hide the fact. :) :).

When you code "for... result := ... end" you are making a prior lookup before actually accessing the entity. Say: you get the pointer, then you access the entity with it.

In your example (a very good one, BTW), the VCL is making the said prior lookup in the fields entity list, to return a pointer you can act on.

But I think we lost nobbby like three post ago. :).

Cheers.
buho (A).


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top