• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

add text to text document from editbox.

Hi, what would the code be to send text located inside DBemail.text ((which is an email address )) into a .txt document located in f:/BACKUP/special.txt and then auto save the text document.

DBemail.text = Teditbox
special.txt = windows text document

Now, there will be other emails in that text doc also. I dont want to overwrite them, So add the email address address in the following format:


shhhdff@djfjfd.com
dededdde@dejdn.com
wdddw@dlkdeldl.com


List view.
0
boycoder
Asked:
boycoder
  • 6
  • 5
  • 2
1 Solution
 
fromerCommented:
var
  Saver : TStringList;
begin
  Saver := TStringList.Create();
  Saver.LoadFromFile(f:/BACKUP/special.txt);
  Saver.Add(DBemail.text);
  Saver.SaveToFile(f:/BACKUP/special.txt);
end;

  Also A FileStream approach can be applied...
0
 
systanCommented:
I think some lines of code is connected to your q
//Author:carcotasu@ExEx
unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function GetOtherWindowMemoText(const sCaption : String) : WideString;
    function SetOtherWindowMemoText(const sCaption : String; const sText : String) : WideString;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
SetOtherWindowMemoText('Untitled - Notepad',RichEdit1.Lines.Text);
end;

Function TForm1.GetOtherWindowMemoText(const sCaption : String) : WideString;
var
  hWindow : THandle;
  hChild : THandle;
  aTemp: array[0..5000] of Char;
  sClassName : String;
begin
  Result := '';
  hWindow := FindWindow(Nil,PChar(sCaption));
  if hWindow = 0 then begin
    ShowMessage('Could NOT find the other program');
  exit;
end;
  hChild := GetWindow(hWindow, GW_CHILD);
  while hChild <> 0 do Begin
    if GetClassName(hChild, aTemp, SizeOf(aTemp)) > 0 then begin
      sClassName := StrPAS(aTemp);
      if sClassName = 'Edit' then begin
        SendMessage(hChild,WM_GETTEXT,SizeOf(aTemp),Integer(@aTemp));
        Result := StrPAS(aTemp);
      end;
    end;
    hChild := GetWindow(hChild, GW_HWNDNEXT);
  end;
end;

Function TForm1.SetOtherWindowMemoText(const sCaption : String; const sText : String) : WideString;
var
  hWindow : THandle;
  hChild: THandle;
  aTemp: array[0..5000] of Char;
  sClassName : String;
begin
  Result := '';
  hWindow := FindWindow(Nil,PChar(sCaption));
  if hWindow = 0 then begin
    ShowMessage('Could NOT find the other program');
    exit;
  end;
  hChild := GetWindow(hWindow, GW_CHILD);
    while hChild <> 0 do Begin
      if GetClassName(hChild, aTemp, SizeOf(aTemp)) > 0 then begin
        sClassName := StrPAS(aTemp);
        if sClassName = 'Edit' then begin
          StrPCopy(aTemp,sText);
          SendMessage(hChild,WM_SETTEXT,SizeOf(aTemp),Integer(@aTemp));
        end;
      end;
      hChild := GetWindow(hChild, GW_HWNDNEXT);
    end;
end;

//Let say you want to get the text from Notepad, then you should pass the title of notepad to the previous function and assign the return value to Rich edit.    An example:

//...............................................
//Get text from noteppad.
//RichEdit1.Lines.Text := GetOtherWindowMemoText('Untitled - Notepad');
//...............................................
///Set notepad memo text.
//SetOtherWindowMemoText('Untitled - Notepad',RichEdit1.Lines.Text);



end.

Open in new window

0
 
fromerCommented:
Method1:
var
  Saver : TStringList;
begin
  Saver := TStringList.Create();
  if FileExists('f:/BACKUP/special.txt') then
  begin
    Saver.LoadFromFile('f:/BACKUP/special.txt');
  end;
  Saver.Add(DBemail.text);
  Saver.SaveToFile('f:/BACKUP/special.txt');
end;


Method2:
var
  S : String;
  __FileStream : TFileStream;
begin
  S := DBemail.text;
  if FileExists('f:/BACKUP/special.txt') then
  begin
    __FileStream := TFileStream.Create('f:/BACKUP/special.txt', fmOpenReadWrite);
  end
  else
  begin
    __FileStream := TFileStream.Create('f:/BACKUP/special.txt', fmCreate);
  end;
  __FileStream.Position := __FileStream.Size;
  __FileStream.WriteBuffer(Pointer(S)^, Length(S));
  __FileStream.Free;
end;

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
fromerCommented:
for the newline, you may set S in method2 as

S := DBemail.text + Char(13);

or more safely
S := DBemail.text + SLineBreak;



0
 
boycoderAuthor Commented:
Fromer, above works.. but it doesnt add them to the text document as a list. Can you take a look at that please?
0
 
boycoderAuthor Commented:
A++++++++++++ GREAT! Thanks
0
 
fromerCommented:
for the newline, you may set S in method2 as

S := DBemail.text + Char(13);

or more safely
S := DBemail.text + SLineBreak;

This works?
0
 
boycoderAuthor Commented:
carry on from my last Q sorry...

If when the form13 loads in the onshow event, how would i have it so that if the email inside DBemail.text is already inside the text file, dont add it... this is what i am working with


if
DBemail.text=(f:\special.txt')
then begin
showmessage('already added')
  MakeSpecialMember1.enabled := false
  end
  else begin
    MakeSpecialMember1.enabled := true ;
0
 
systanCommented:
I misunderstood the post again, always. LoL for me, stupid eyes.
0
 
fromerCommented:

var
  SL : TStringList;
  EmailExist : Boolean;
begin
  if FileExists('f:/BACKUP/special.txt') then
  begin
    SL := TStringList.Create;
    SL.LoadFromFile('f:/BACKUP/special.txt');
    EmailExist := SL.IndexOf(DBemail.Text) >= 0;
    SL.Free;
  end
  else
  begin
    EmailExist := False;
  end;

  if EmailExist then
  begin
    showmessage('already added');
    MakeSpecialMember1.enabled := false;
  end
  else
  begin
    MakeSpecialMember1.enabled := true;
  end;
end;
0
 
fromerCommented:
Do you add the "DBemail.Text" to the file  "f:/BACKUP/special.txt" in OnShow event of Form13?

if so


var
  SL : TStringList;
begin
  SL := TStringList.Create;
  if FileExists('f:/BACKUP/special.txt') then
  begin
    SL.LoadFromFile('f:/BACKUP/special.txt');
    if SL.IndexOf(DBemail.Text) >= 0 then
    begin
      showmessage('already added');
      MakeSpecialMember1.enabled := false;
    end
    else
    begin
      SL.Add(DBemail.Text);
      SL.SaveToFile('f:/BACKUP/special.txt');
      MakeSpecialMember1.enabled := true;
    end;
  end
  else
  begin
    SL.Add(DBemail.Text);
    SL.SaveToFile('f:/BACKUP/special.txt');
    MakeSpecialMember1.enabled := true;
  end;
  SL.Free;
end;

 
0
 
boycoderAuthor Commented:
it works the way i want it to, but.. this is what happens


when i select a customer and click  Addcustomertospeciallist, it adds them to the text file via the email field fine.
When i close the account down, and select another user, it says already added and the  Addcustomertospeciallist1 is disabled still, when i redo it, its fine.
It is like it is remembering the previous entry but not clearing it.,

This is my current code i am using ..

update:#

procedure TForm13.FormShow(Sender: TObject);
    var
  SL : TStringList;


begin
  SL := TStringList.Create;
  if FileExists('C:\Users\defcon\Documents\special.txt') then
  begin
    SL.LoadFromFile('C:\Users\defcon\Documents\special.txt');
    if SL.IndexOf(DBadd1.Text) >= 0 then
    begin

      Addcustomertospeciallist1.enabled := false;
    end
    else
    begin
      SL.Add(DBadd1.Text);
      SL.SaveToFile('C:\Users\defcon\Documents\special.txt');
      Addcustomertospeciallist1.enabled := true;
    end;

  SL.Free;


my other code......



0
 
boycoderAuthor Commented:
also if i click.. add .. and it adds the email fine.
if i click same user it says already added.
if i click on him again it says i can add him again..

thanks for helping me
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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