Sign in Join
Wiki copytemporary

TMemo/ja

Deutsch · English · Français · Suomi · Русский · 日本語

TMemo は複数行を編集できるコントロールである。コンポーネントパレットタブのStandardタブから利用できる。

使い方

TMemoを用いるにはformのStandardコンポーネントパレットで選択しフォーム上でクリックすればよい。このテキストボックスで実行時に複数行のテキストを編集できる。

例えば、フォーム、Form1へTMemo Memo1を加えるとStringにMemo1.Text:='これは1行のテキストです';と割り当てることができる。

また、myString:=Memo1.Text;として、保存されたテキストMemo1をソースの中のどこでも使うことができる。

複数行のテキスト、Memo1.Text:=' これは '+LineEnding+' 複数行の '+LineEnding+' テキスト '+LineEnding+' です ';を割り当てることも可能である。

TStringsもしくはTStringListを割り当てる

通常、TMemoにテキストを割り当てるには、TStringList、もしくはその親のTStringsを利用する。以下の例(form Form1とTMemo Memo1を用いて挿入されたTButton Button1イベントハンドラで)はこれを示している:

pascal
procedure TForm1.Button1Click(Sender: TObject);
var
  myStringList: TStringList;
begin
  myStringList:=TStringList.Create;               //StringListを作る
  myStringList.Add('This is the first line.');    //1行加える
  myStringList.Add('This is the second line.');
  myStringList.Add('This is the third line.');
  myStringList.Add('etc.');
  Memo1.Lines.Assign(myStringList);               //テキスト内容を割り当てる
  myStringList.Free;                              //StringListを解放
end;

直接行を挿入する

この例ようにメモの内容を直接加えることもできる:

pascal
procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;                              //Memo1のすべての行を削除
  Memo1.Lines.Add('This is the first line.');     //1行加える。
  Memo1.Lines.Add('This is the second line.');
  Memo1.Lines.Add('This is the third line.');
  Memo1.Lines.Add('etc.');
end;

行を読む

特定の行に何が入っているがを知りたい場合には、直接myString:=Memo1.Lines[Index];で調べることができる。注意、TMemo.Linesはゼロから始まっている。即ち、最初の行は: myString:=Memo1.Lines[0];となる。

続く例はさらにTButton Button2を加えて、以下のように3つ目の行を表示している:

pascal
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(Memo1.Lines[2]);
end;

テキストを選択する

テキスト部分をマウスの左ボタンを押し続けるか、[Shift]キーを押すことで、マウスやキーボードでテキストを選択することができる。このテキストStringをこのように表示することができる:

pascal
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(Memo1.SelText); 
end;

テキストを検索する

以上の例とは対照的に、TMemoにある(String)を探し、その場所を返すこともできる: Position:=Memo1.SelStart;

以下の例ではmemoのなかにあるテキストを探し、さらに探している:

pascal
// FindInMemo: 探す文字列が見つかった場所を返す
function FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;
begin
  Result := PosEx(AString, AMemo.Text, StartPos);
  if Result > 0 then
  begin
    AMemo.SelStart := UTF8Length(PChar(AMemo.Text), Result - 1);
    AMemo.SelLength := Length(AString);
    AMemo.SetFocus;
  end;
end;
pascal
procedure TForm1.Button2Click(Sender: TObject);
const
  SearchStr: String = '';                     // 探す文字列
  SearchStart: Integer = 0;                   // 探す文字列の最後の場所
begin
  if SearchStr <> Edit1.Text then begin       // Falls sich der zu suchende String geändert hat
    SearchStart := 0;
    SearchStr := Edit1.Text;
  end;
  SearchStart := FindInMemo(Memo1, SearchStr, SearchStart + 1);

  if SearchStart > 0 then
    Caption := 'Found at position['+IntToStr(SearchStart)+']!'
  else
    Caption := 'No further finds!';
end;

EM_SCROLLCARET

Windowsで用いられているように EM_SCROLLCARET のようなメッセージはLazarusでは使えないので、別法が必要である...

保存と読み込み

全く簡単に、TStringsクラスのメソッド、SaveToFileとLoadFromFileを用いて、memoの内容を保存、読み込みできる。

以下の例でどのようにするかを示す:

pascal
procedure TForm1.Button2Click(Sender: TObject);
begin
  if SaveDialog1.Execute then
    Memo1.Lines.SaveToFile(SaveDialog1.FileName);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
end;

以下も参照のこと


From the Free Pascal wiki, page "TMemo/ja", as it stood on 2024-03-20 after a change by Ariben, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 1 person, 2024

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
AribenCreated page
Ariben
Ariben使い方
Ariben以下も参照のこと

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.