-1

I have a StrinGrid component and a procedure:

procedure TForm3.StringGrid1Click(Sender: TObject);
begin
    SelectedElement := stringgrid1.Cells[0,stringgrid1.Row];
end

SelectedElement is declared in public section:

public
SelectedElement : String;
end;

When I use it in this unit, for example Label1.Caption := SelectedElement, it works fine. But in another unit, where I specified uses unit1 in implementation, and I try to use this variable like this Label1.Caption := Form1.SelectedElement it sets label to empty string. But when I set variable manually for example on first form create, then this value shows up in second form, even if variable is later changed to value from stringgrid.

maciejjo
  • 213
  • 3
  • 10
  • I have a vague feeling that you want the label on the second form to reflect the selected element whenever you click on a cell of the grid of the first form. Is this correct? – Sertac Akyuz Jan 05 '13 at 00:06
  • Yes, but it doesn't have to wait for change, only show the element that was selected while opening second form via button on first form – maciejjo Jan 05 '13 at 00:09
  • I can't guess why it shouldn't work. – Sertac Akyuz Jan 05 '13 at 00:22

2 Answers2

2

Given the small amount of code you have shown so far, it is difficult to diagnose your problem for sure, but based on your comments so far, it sounds to me like you are probably dynamically creating your TForm3 object at run-time using TForm3.Create() and not assigning the object to the global Form3 pointer, but are trying to use the global Form3 pointer to access the SelectedElement value. Is that correct?

Also, you show TForm3.StringGrid1Click() is setting TForm3.SelectedElement, but you are accessing Form1.SelectedElement instead of Form3.SelectedElement. Does TForm1 have its own SelectedElement member? Or are you not showing your real code copy/pasted from your real project?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    +1 this is the only explanation that I can believe matches the stated behaviour – David Heffernan Jan 05 '13 at 08:33
  • The code is real, Form1 does not have it's own SelectedElement. Form3 is my main form, and it creates Form1 by `Form1 := TForm1.Create(Application);`. `Form1.SelectedElement` was a mistake, I changed it to `Form3.SelectedElement`. – maciejjo Jan 05 '13 at 10:00
1

You should add a property to the form that returns the desired value:

....
private
  function GetSelectedElement: string;
public
  property SelectedElement: string read GetSelectedElement;
....

And implement it like this:

function TForm3.GetSelectedElement: string;
begin
  Result := StringGrid1.Cells[0, StringGrid1.Row];
end;

This will always return the current state which I believe is what you want.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Now this always sets the SelectedElement value to column 0 row 0, and not the selected row when I read the value in the second form; when I read it in first form it works. – maciejjo Jan 05 '13 at 00:23
  • @user1766392: The only way it can do that is if `StringGrid1.Row` really is 0. Reading `Form3.SelectedElement` in `Form2` will work fine, provided that `Form3` is pointing at a valid `TForm3` object to begin with. – Remy Lebeau Jan 05 '13 at 00:33
  • @user Ask yourself how that could be so. As Remy says, you must be doing something wrong. – David Heffernan Jan 05 '13 at 08:31
  • I got it to work, but only if I put code to set label in form2 in unit1. Does the job. Thanks for help! – maciejjo Jan 05 '13 at 09:53
  • 1
    It will work either way if you do it right. I think you need to learn some basics. Clearly you have multiple instances of the form. Remy probably diagnosed it right. – David Heffernan Jan 05 '13 at 10:07
  • 1
    @user - form2? Please consider posting real code when asking a question. – Sertac Akyuz Jan 05 '13 at 12:24