Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Delphi 2009 Handbook

Project: StreamTroubles.dproj

Project Structure

StreamTroubles.dpr
program StreamTroubles;

uses
  Forms,
  StreamTroubles_MainForm in 'StreamTroubles_MainForm.pas' {FormStreamTroubles};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TFormStreamTroubles, FormStreamTroubles);
  Application.Run;
end.
StreamTroubles_MainForm.pas
unit StreamTroubles_MainForm;

interface

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

type
  TFormStreamTroubles = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    btnTrouble: TButton;
    btnFix: TButton;
    procedure btnTroubleClick(Sender: TObject);
    procedure btnFixClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormStreamTroubles: TFormStreamTroubles;

implementation

{$R *.dfm}

procedure TFormStreamTroubles.btnTroubleClick(Sender: TObject);
var
  memStr: TMemoryStream;
begin
  memStr := TMemoryStream.Create;
  try
    Memo1.Lines.LoadFromFile ('StreamTroubles_MainForm.pas');
    Memo1.Lines.SaveToStream(memStr, TEncoding.UTF8);
    memStr.Position := 0; // reset
    Memo2.Lines.LoadFromStream(memStr, TEncoding.Unicode);
  finally
    memStr.Free;
  end;
end;

procedure TFormStreamTroubles.btnFixClick(Sender: TObject);
var
  memStr: TMemoryStream;
begin
  memStr := TMemoryStream.Create;
  try
    Memo1.Lines.LoadFromFile ('StreamTroubles_MainForm.pas');
    Memo1.Lines.SaveToStream(memStr, TEncoding.UTF8);
    memStr.Position := 0; // reset
    Memo2.Lines.LoadFromStream(memStr);
  finally
    memStr.Free;
  end;
end;

end.
StreamTroubles_MainForm.pas.dfm
object FormStreamTroubles: TFormStreamTroubles
  Left = 0
  Top = 0
  Caption = 'StreamTroubles'
  ClientHeight = 593
  ClientWidth = 554
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 121
    Top = 25
    Width = 425
    Height = 249
    Lines.Strings = (
      'Memo1')
    TabOrder = 0
  end
  object Memo2: TMemo
    Left = 120
    Top = 280
    Width = 425
    Height = 297
    Lines.Strings = (
      'Memo2')
    TabOrder = 1
  end
  object btnTrouble: TButton
    Left = 16
    Top = 22
    Width = 75
    Height = 25
    Caption = 'btnTrouble'
    TabOrder = 2
    OnClick = btnTroubleClick
  end
  object btnFix: TButton
    Left = 16
    Top = 56
    Width = 75
    Height = 25
    Caption = 'btnFix'
    TabOrder = 3
    OnClick = btnFixClick
  end
end
HTML file generated by PasToWeb, a tool by Marco Cantù
Copyright 2008 Marco Cantù