Sign in Join
Wiki copytemporary

TMemo/ja: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TMemo/ja

OldNewLine
1+**TMemo** ![](/wiki-images/1/1b/Tmemo.png)は複数行を編集できるコントロールである。[[Component Palette/ja|コンポーネントパレットタブの]][[Standard tab/ja|Standardタブから]]利用できる。
2+
3+## 使い方
4+
5+TMemoを用いるには[[TForm|formの]]*Standard*コンポーネントパレットで選択しフォーム上でクリックすればよい。このテキストボックスで実行時に複数行のテキストを編集できる。
6+
7+例えば、フォーム、*Form1*へTMemo *Memo1*を加えると[[String|Stringに]]**`Memo1.Text:='これは1行のテキストです';`**と割り当てることができる。
8+
9+また、**`myString:=Memo1.Text;`**として、保存されたテキスト*Memo1*をソースの中のどこでも使うことができる。
10+
11+複数行のテキスト、**`Memo1.Text:=' これは '+LineEnding+' 複数行の '+LineEnding+' テキスト '+LineEnding+' です ';`**を割り当てることも可能である。
12+
13+### TStringsもしくはTStringListを割り当てる
14+
15+通常、TMemoにテキストを割り当てるには、[[TStringList-TStrings Tutorial|TStringList]]、もしくはその親の[[TStringList-TStrings Tutorial|TStringsを]]利用する。以下の例([[TForm|form]] *Form1*とTMemo *Memo1*を用いて挿入された[[TButton/ja|TButton]] *Button1*イベントハンドラで)はこれを示している:
16+
17+``` pascal
18+procedure TForm1.Button1Click(Sender: TObject);
19+var
20+ myStringList: TStringList;
21+begin
22+ myStringList:=TStringList.Create; //StringListを作る
23+ myStringList.Add('This is the first line.'); //1行加える
24+ myStringList.Add('This is the second line.');
25+ myStringList.Add('This is the third line.');
26+ myStringList.Add('etc.');
27+ Memo1.Lines.Assign(myStringList); //テキスト内容を割り当てる
28+ myStringList.Free; //StringListを解放
29+end;
30+```
31+
32+### 直接行を挿入する
33+
34+この例ようにメモの内容を直接加えることもできる:
35+
36+``` pascal
37+procedure TForm1.Button1Click(Sender: TObject);
38+begin
39+ Memo1.Lines.Clear; //Memo1のすべての行を削除
40+ Memo1.Lines.Add('This is the first line.'); //1行加える。
41+ Memo1.Lines.Add('This is the second line.');
42+ Memo1.Lines.Add('This is the third line.');
43+ Memo1.Lines.Add('etc.');
44+end;
45+```
46+
47+### 行を読む
48+
49+特定の行に何が入っているがを知りたい場合には、直接**`myString:=Memo1.Lines[Index];`**で調べることができる。注意、*TMemo.Lines*はゼロから始まっている。即ち、最初の行は: **`myString:=Memo1.Lines[0];`**となる。
50+
51+続く例はさらにTButton *Button2*を加えて、以下のように3つ目の行を表示している:
52+
53+``` pascal
54+procedure TForm1.Button2Click(Sender: TObject);
55+begin
56+ ShowMessage(Memo1.Lines[2]);
57+end;
58+```
59+
60+### テキストを選択する
61+
62+テキスト部分をマウスの左ボタンを押し続けるか、\[Shift\]キーを押すことで、マウスやキーボードでテキストを選択することができる。このテキスト[[String|Stringをこのように]]表示することができる:
63+
64+``` pascal
65+procedure TForm1.Button2Click(Sender: TObject);
66+begin
67+ ShowMessage(Memo1.SelText);
68+end;
69+```
70+
71+### テキストを検索する
72+
73+以上の例とは対照的に、TMemoにある([[String]])を探し、その場所を返すこともできる: **`Position:=Memo1.SelStart;`**
74+
75+以下の例ではmemoのなかにあるテキストを探し、さらに探している:
76+
77+- [[TEdit]] *Edit1*、TMemo *Memo1*、[[TButton/ja|TButton]] *Button1* と *Button2*を持つ新しいアプリケーションを作る。
78+- Uses節に **LCLProc** と **strutils** を加える。
79+- [[TMemo|Insert lines directly]]例のように、*Button1*の*OnClick*イベントを何らかのテキストで埋める
80+- ソーステキストエディタで以下の関数を(ドイツのLazarusフォーラム \[<http://www.lazarusforum.de/viewtopic.php?p=39260#p39260%5Dに基づく)入力する>:
81+
82+``` pascal
83+// FindInMemo: 探す文字列が見つかった場所を返す
84+function FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;
85+begin
86+ Result := PosEx(AString, AMemo.Text, StartPos);
87+ if Result > 0 then
88+ begin
89+ AMemo.SelStart := UTF8Length(PChar(AMemo.Text), Result - 1);
90+ AMemo.SelLength := Length(AString);
91+ AMemo.SetFocus;
92+ end;
93+end;
94+```
95+
96+- ここで、*Button2*のイベントハンドラで*OnClick*に以下のコードを加える:
97+
98+``` pascal
99+procedure TForm1.Button2Click(Sender: TObject);
100+const
101+ SearchStr: String = ''; // 探す文字列
102+ SearchStart: Integer = 0; // 探す文字列の最後の場所
103+begin
104+ if SearchStr <> Edit1.Text then begin // Falls sich der zu suchende String geändert hat
105+ SearchStart := 0;
106+ SearchStr := Edit1.Text;
107+ end;
108+ SearchStart := FindInMemo(Memo1, SearchStr, SearchStart + 1);
109+
110+ if SearchStart > 0 then
111+ Caption := 'Found at position['+IntToStr(SearchStart)+']!'
112+ else
113+ Caption := 'No further finds!';
114+end;
115+```
116+
117+- 実行時に*Button1*でmemoを埋め、*Edit1*に探されるべきテキストを入れ、*Button2*でそれらを探す、探し続けることができる。
118+
119+#### EM_SCROLLCARET
120+
121+Windowsで用いられているように EM_SCROLLCARET のようなメッセージはLazarusでは使えないので、別法が必要である...
122+
123+### 保存と読み込み
124+
125+全く簡単に、[[TStringList-TStrings Tutorial|TStringsクラスのメソッド]]、*SaveToFile*と*LoadFromFile*を用いて、memoの内容を保存、読み込みできる。
126+
127+以下の例でどのようにするかを示す:
128+
129+- TMemo *Memo1*と3つのボタン、[[TButton/ja|TButton]] *Button1*、*Button2*、*Button3*を持つ新しいアプリケーションを作る。
130+- さらに、コンポーネントパレット*Dialogs*から[[TSaveDialog|TSaveDialogと]][[TOpenDialog|TOpenDialogをとり]]、フォームに置く。
131+- *Button1*のキャプション「メモを記入」へ変える。
132+- *Button1*の*OnClick*イベントハンドラで、[[TMemo|Insert lines directlyの]]例にあるように、何かのテキストで、memoを埋める。
133+- *Button2*のキャプションを「メモを保存」と変える。
134+- *Button3*のキャプションを「メモを開く」と変える。
135+- これで、ボタンの*OnClick*イベントハンドラを変更できる。
136+
137+``` pascal
138+procedure TForm1.Button2Click(Sender: TObject);
139+begin
140+ if SaveDialog1.Execute then
141+ Memo1.Lines.SaveToFile(SaveDialog1.FileName);
142+end;
143+
144+procedure TForm1.Button3Click(Sender: TObject);
145+begin
146+ if OpenDialog1.Execute then
147+ Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
148+end;
149+```
150+
151+## 以下も参照のこと
152+
153+- [[doc:lcl/stdctrls/tmemo.html|TMemo doc]]
154+- [[RichMemo|TRichMemo]] - Like Delphi TRichEdit component: formatted text (color, size, etc.)
155+- [[TListBox]] - A scrollable list of strings
156+
157+---
158+*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](/wiki-contact).*

Back to history