Groups > Borland > Delphi Object Oriented design > Re: I'm really stuck any help will be very appreciated (Form Template)




I'm really stuck any help will be very appreciated (Form
Template)

I'm really stuck any help will be very appreciated (Form Template)
Tue, 27 Nov 2007 01:34:39 +020
Hi to all Delphi Masters.
I'm trying to solve one problem bum I'm stuck because a lack of knowledge .
I'm trying to use TObjectList to hold a form .
What I'm trying to do in simple words is. I'm trying to make something
like chat program. I have one form like a template and when I receive
message if form is not created wet I must create it and add some text to
 one memo in the form . If form is already created i  must just make it
blink and add some text to one memo in the form .

Here is what i try to do but without any success .

I;m creating template form without any problem and add it into
TObjectList but i can't find and show the form.





unit Main_Unit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, JvExStdCtrls, JvMemo,Contnrs,Template;

type
  TMain_Form = class(TForm)
    btnAdd: TButton;
    ebMsg: TEdit;
    btnSend: TButton;
    lbForms: TListBox;
    Edit1: TEdit;

    procedure FormCreate(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnSendClick(Sender: TObject);

  private
  fForms: TObjectList;
  function FindForm(FormName: string): TMain_Form;
  function AddNewForm(FormName: string): TMain_Form;

    { Private declarations }

  public

   { Public declarations }
  procedure AddChatText(FormName,ChatText: string);
  published

  end;

var
  Main_Form: TMain_Form;

implementation

{$R *.dfm}
procedure TMain_Form.AddChatText(FormName,ChatText: String);
// finds (or creates) form for given FormName, and adds given ChatText
var Form: TForm_Template;
begin
Form := AddNewForm(FormName);     // find (or create) the form
Form.MemoAnswer.Lines.Add(ChatText); //Add Text to memo
end;

function TMain_Form.AddNewForm(FormName: string): TMain_Form;
begin
{
if findForm(Name) has no result then
  create form
else
  esle return result form
 }
end;

function TMain_Form.FindForm(FormName: string): TMain_Form;
var i: integer;
oForm: TForm_Template;
begin
 result := nil;

 for i := 0 to fForms.Count - 1 do
  oForm := TForm_Template(fForms.Items[i]);
  if oForm.Name = FormName then
  begin
   //result := TForm_Template(fForms.Items[i]);   Return result Form
   //break;  Doesn't Work :(
  end;

end;

procedure TMain_Form.FormCreate(Sender: TObject);
begin
fForms := TObjectList.Create();
end;

procedure TMain_Form.btnAddClick(Sender: TObject);
var
oForm: TForm_Template;
begin
oForm:=TForm_Template.Create(nil); This part only work :((
FForms.Add(oForm);
oForm.Name := ebMsg.Text;
lbForms.Items.Add( oForm.Name);
end;

procedure TMain_Form.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FForms.Free;
end;

procedure TMain_Form.btnSendClick(Sender: TObject);
begin
AddChatText('FormName','Text');
end;

Post Reply
Re: I'm really stuck any help will be very appreciated (Form Template)
Tue, 27 Nov 2007 09:51:58 +010
En/na Stanislav Nedelchev ha escrit:
> uses

     + "Uses contnrs" for TObjectList, otherwise it won't compile

>   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
>   Dialogs, StdCtrls, JvExStdCtrls, JvMemo,Contnrs,Template;
> 
>   function FindForm(FormName: string): TMain_Form;
>   function AddNewForm(FormName: string): TMain_Form;

Shoudln't these function return a TForm_Template?
> 
>     { Private declarations }
> 
> function TMain_Form.FindForm(FormName: string): TMain_Form;
> var i: integer;
> oForm: TForm_Template;
> begin
>  result := nil;
> 

Indenting is nice, but try with "begin"..."end" <g>

>  for i := 0 to fForms.Count - 1 do
    begin
>   oForm := TForm_Template(fForms.Items[i]);
>   if oForm.Name = FormName then
>   begin
>    //result := TForm_Template(fForms.Items[i]);   Return result Form
>    //break;  Doesn't Work :(
>   end;
    end;
> 

Regards,

Post Reply
Re: I'm really stuck any help will be very appreciated (Form Template)
Thu, 29 Nov 2007 09:41:39 +110
> function TMain_Form.FindForm(FormName: string): TMain_Form;
> var i: integer;
> oForm: TForm_Template;
> begin
> result := nil;
>
> for i := 0 to fForms.Count - 1 do
>  oForm := TForm_Template(fForms.Items[i]);
>  if oForm.Name = FormName then
>  begin
>   //result := TForm_Template(fForms.Items[i]);   Return result Form
>   //break;  Doesn't Work :(
>  end;
>
> end;

Tform_Template is not TMain_Form, it won't be compiled.

Roland 

Post Reply
about | contact