[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Writing to form in DLL

Hello,

I am not sure what I am doing wrong.

I have made a DLL.

Inside the DLL I have include a form and unit1. On the form is a Memo control.

My guess is I have some kind of scoping issue. I can create the form, show the form, but when I go to write in the Memo control the program crashes.

I have to fully qualify the Memo for intellisense to work. Form1.Memo1.Lines.add(s);

I am guessing that it is because once I leave the procedure that creates the form that the form is no longer recognized?

Can someone show me the proper way to create a form in a DLL and be able to write to a memo from procedures.

I have something along the lines of: (I just sketched this out.. probably not accurate on class names)

Procedure WriteMemo(s:string);
Begin
  Form1.memo1.lines.add(s);
End;

Procedure DoTheWork();
Var
  Form1 : TForm;
Begin
  Form1 := TForm.Create(nil)
  Form1.show;
 [code]
  writememo(string);
end;

I am calling DoTheWork from the Library unit.

Any help is appreciated.
Thanks
kent




0
KentDRuddick
Asked:
KentDRuddick
  • 2
2 Solutions
 
DragonSlayerCommented:
if Form1 is a var declared in DoTheWork, how can WriteMemo access Form1?

perhaps you can modify WriteMemo to take, as parameter, a TMemo and a String? as in

procedure WriteMemo(Memo: TMemo; const S: string);
begin
  Memo.Lines.Add(S);
end;

and your code will be

Form1 := TForm1.Create(nil);
Form1.Show;
WriteMemo(Form1.Memo, 'SomeText');
0
 
DragonSlayerCommented:
or, instead of local, you can make your Form1 to be global, so that both procedures are accessing the same Form1 variable. I think you might have 2 declarations of Form1 in your unit, one global and one local, hence the crash.
0
 
MikhoCommented:
your problem is indeed that you declared Form1 as a local var in Procedure DoTheWork();

AND if your create your form by code only ( Form1 := TForm.Create(nil))
you also need to create the Memo on the form.

best way would probably be to add a new form to the project and design it there and then call that form from main dll project.
0
 
KentDRuddickAuthor Commented:

I split the points because both answers led me to my solution.

It was definitely a lack of understanding of the language and some creative programming on my part.

Thanks for the help.
Kent
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now