Difference between revisions of "TListBox/ja"

From Lazarus wiki
Jump to navigationJump to search
Line 88: Line 88:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Owner-drawn ListBox==
+
==所有者によって書かれたリストボックス==
In general, it is advantageous to let the ListBox follow the  [http://en.wikipedia.org/wiki/Skin_%28computing%29 theme] set by the user. In some cases (for example, to program a game with a colorful surface), you can deviate from this standard and draw the control according to your own choice. You can try this now:
+
一般的には、リストボックスをユーザーによって設定された[http://en.wikipedia.org/wiki/Skin_%28computing%29 テーマ]に従わせることが有効である。ある場合(例えば、色彩の多い表面を持つゲームをプログラムするため)ではこの標準から逸脱し、自身の選択に従ってコントロールを描くことができる。
* You can modify the previous sample or create a new application with a TListBox ''ListBox1''.
+
これを、
* In the Object Inspector, change ''ListBox1'' property ''Style'' to ''lbOwnerDrawFixed''.
+
* 上の例を変更するか、TListBox ''ListBox1''をもつ新しいアプリケーションを作る
* With the Object Inspector, tab events, create the event handler for the event ''OnDrawItem'', by clicking on the button [...].
+
* オブジェクトインスペクタで''ListBox1''のプロパティ、''Style''''lbOwnerDrawFixed''へ変更する。
* You add the following code to the handler:
+
* オブジェクトインスペクタのイベントタブで[...]ボタンをクリックすることで、''OnDrawItem''イベントに対するイベントハンドラを作る。
 +
* イベントハンドラに以下のコードを加える:
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
 
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
 
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
 
   ARect: TRect; State: TOwnerDrawState);
 
   ARect: TRect; State: TOwnerDrawState);
 
var
 
var
   aColor: TColor;                      //Background color
+
   aColor: TColor;                      //背景色
 
begin
 
begin
   if (Index mod 2 = 0)                  //Index tells which item it is
+
   if (Index mod 2 = 0)                  //インデクスがどのアイテムか知らせる
     then aColor:=$FFFFFF                //every second item gets white as the background color
+
     then aColor:=$FFFFFF                //背景色として1秒ごとにアイテムが白になる
     else aColor:=$EEEEFF;              //every second item gets pink background color
+
     else aColor:=$EEEEFF;              //背景色として1秒ごとにアイテムが桃になる
   if odSelected in State then aColor:=$0000FF;  //If item is selected, then red as background color
+
   if odSelected in State then aColor:=$0000FF;  //もしアイテムが選択されると、背景色は赤となる。
   ListBox1.Canvas.Brush.Color:=aColor;  //Set background color
+
   ListBox1.Canvas.Brush.Color:=aColor;  //背景色を設定する
   ListBox1.Canvas.FillRect(ARect);      //Draw a filled rectangle
+
   ListBox1.Canvas.FillRect(ARect);      //塗りつぶされた四角形を描く
  
   ListBox1.Canvas.Font.Bold:=True;      //Set the font to "bold"
+
   ListBox1.Canvas.Font.Bold:=True;      //フォントを「太字」に設定する
   ListBox1.Canvas.TextRect(ARect, 2, ARect.Top+2, ListBox1.Items[Index]);  //Draw Itemtext
+
   ListBox1.Canvas.TextRect(ARect, 2, ARect.Top+2, ListBox1.Items[Index]);  //アイテムテキストを描く
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 15:12, 21 March 2024

Deutsch (de) English (en) suomi (fi) français (fr) 日本語 (ja)

日本語版メニュー
メインページ - Lazarus Documentation日本語版 - 翻訳ノート - 日本語障害情報

TListBox tlistbox.pngは、ユーザーが1つ選択する必要がある(スクロール可能な)短い文字列のリストを表示するコンポーネントである。コンポーネントパレットStandardタブで利用できる。

TListBoxにあるstringsTStrings型であるItemsプロパティに格納されている。そのため、TStringListまたはその親であるTStringsにあるように、リストボックス内に文字列を割り当て、もしくは取り除くことができる。

これはForm1のTListBox ListBox1の使い方のいくつかの例である:

リストボックスを埋める

オブジェクトインスペクタにより

  • フォーム上のListBoxを1回のクリックで選択する。
  • PropertiesタブでObject Inspectorに移動し、Itemsというプロパティを選択する。
  • 3つの点があるボタンをクリックします。String Editorが開く。
  • テキストを入力し、OKで作業を確認する。

ボタンクリックでコードにより

フォームに、名前がbtnFillでキャプションがfill ListBoxとなるTButtonを追加します。そのボタンのOnClickイベントハンドラには、以下のコードを記述する:

procedure TForm1.btnFillClick(Sender: TObject);
begin
  ListBox1.Items.Clear;             //Delete all existing strings
  ListBox1.Items.Add('First line');
  ListBox1.Items.Add('Line with random number '+IntToStr(Random(100)));
  ListBox1.Items.Add('Third line');
  ListBox1.Items.Add('Even a random number '+IntToStr(Random(100)));
end;

StringListの割り当て

フォームに、名前がbtnFillでキャプションがfill ListBoxとなるTButtonを追加する。そのボタンのOnClickイベントハンドラには、以下のコードを記述する:

procedure TForm1.btnFillClick(Sender: TObject);
var
  myStringList: TStringList;
begin
  myStringList:=TStringList.Create;               //StringListを作る
  myStringList.Add('This is the first line.');   //新しい行を追加する
  myStringList.Add('This is the second first line.');
  myStringList.Add('This is the third line.');
  myStringList.Add('etc.');
  ListBox1.Items.Assign(myStringList);            //ListBox1にStringListの内容を割り当てる
  myStringList.Free;                              //メモリからStringListを解放する
end;

文字列を追加する

  • ボタンクリックでコードにより埋める、を拡張し、TEditと名前がbtnAddでキャプションがadd stringのTButtonを追加し、Edit1のTextプロパティを"" - 空の文字列に変更する。
  • ボタンのイベントハンドラOnClickに以下のコードを書く:
procedure TForm1.btnAddClick(Sender: TObject);
begin
  ListBox1.Items.Add(Edit1.Text);
  Edit1.Text:='';
end;

文字列を削除する

デフォルトでは、ListBoxで1行のみを選択できるように設定されている。ListBoxで複数の行を選択する場合は、MultiSelectプロパティをTrueに設定する必要がある。

ItemIndexで

  • 文字列を追加するを拡張し"btnDel"の名前のTButton、そのキャプションを"delete string"とする。
  • ボタンのOnClickイベントハンドラに以下のコードを書く:
procedure TForm1.btnDelClick(Sender: TObject);
begin
  if ListBox1.ItemIndex > -1 then    //リストボックスの文字列が選択されたときのみ削除
    ListBox1.Items.Delete(ListBox1.ItemIndex);
end;

all selected strings

  • 文字列を追加するを拡張し"btnDel"の名前のTButton、そのキャプションを"delete string"とする。
  • ボタンのOnClickイベントハンドラに以下のコードを書く:
procedure TForm1.btnDelClick(Sender: TObject);
var
  i: Integer;
begin
  if ListBox1.SelCount > 0 then                 //リストボックスの少なくとも1つの文字列が選択されたときのみ削除する
    for i:=ListBox1.Items.Count - 1 downto 0 do //すべてのアイテムをイテレート
      if ListBox1.Selected[i] then              //もし選択されたら...
        ListBox1.Items.Delete(i);               //...そのアイテム(文字列)を削除
end;

所有者によって書かれたリストボックス

一般的には、リストボックスをユーザーによって設定されたテーマに従わせることが有効である。ある場合(例えば、色彩の多い表面を持つゲームをプログラムするため)ではこの標準から逸脱し、自身の選択に従ってコントロールを描くことができる。 これを、

  • 上の例を変更するか、TListBox ListBox1をもつ新しいアプリケーションを作る
  • オブジェクトインスペクタでListBox1のプロパティ、StylelbOwnerDrawFixedへ変更する。
  • オブジェクトインスペクタのイベントタブで[...]ボタンをクリックすることで、OnDrawItemイベントに対するイベントハンドラを作る。
  • イベントハンドラに以下のコードを加える:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  ARect: TRect; State: TOwnerDrawState);
var
  aColor: TColor;                       //背景色
begin
  if (Index mod 2 = 0)                  //インデクスがどのアイテムか知らせる
    then aColor:=$FFFFFF                //背景色として1秒ごとにアイテムが白になる
    else aColor:=$EEEEFF;               //背景色として1秒ごとにアイテムが桃になる
  if odSelected in State then aColor:=$0000FF;  //もしアイテムが選択されると、背景色は赤となる。
  ListBox1.Canvas.Brush.Color:=aColor;  //背景色を設定する
  ListBox1.Canvas.FillRect(ARect);      //塗りつぶされた四角形を描く

  ListBox1.Canvas.Font.Bold:=True;      //フォントを「太字」に設定する
  ListBox1.Canvas.TextRect(ARect, 2, ARect.Top+2, ListBox1.Items[Index]);  //アイテムテキストを描く
end;

Light bulb  Note: Parameters of ListBoxDrawItem:

Control:
If multiple controls (E.g. multiple ListBoxes) access this event handle, you know which threw this event. You could in our example, instead of
ListBox1.Canvas.FillRect(ARect)
also
TListBox(Control).Canvas.FillRect(ARect)
write, where you should query still possible before, whether it is a TListBox:
  if Control is TListBox then
    TListBox(Control).Canvas.FillRect(ARect);

Index:

Specifies the item location, so you have access to the string
<ListBox>.Items[Index]
.

ARect: Describes the rectangle, which is necessary for drawing the background.
State: Status of the items, whether normal, focused, selected etc.

  • Your example could look like:

ListBoxBsp1.png -> ListBoxBsp2.png

See also


LCL Components
Component Tab Components
Standard TMainMenu • TPopupMenu • TButton • TLabel • TEdit • TMemo • TToggleBox • TCheckBox • TRadioButton • TListBox • TComboBox • TScrollBar • TGroupBox • TRadioGroup • TCheckGroup • TPanel • TFrame • TActionList
Additional TBitBtn/ja • TSpeedButton/ja • TStaticText/ja • TImage/ja • TShape/ja • TBevel/ja • TPaintBox/ja • TNotebook/ja • TLabeledEdit/ja • TSplitter/ja • TTrayIcon/ja • TControlBar/ja • TFlowPanel/ja • TMaskEdit/ja • TCheckListBox/ja • TScrollBox/ja • TApplicationProperties/ja • TStringGrid/ja • TDrawGrid/ja • TPairSplitter/ja • TColorBox/ja • TColorListBox/ja • TValueListEditor/ja
Common Controls TTrackBar • TProgressBar • TTreeView • TListView • TStatusBar • TToolBar • TCoolBar • TUpDown • TPageControl • TTabControl • THeaderControl • TImageList • TPopupNotifier • TDateTimePicker
Dialogs TOpenDialog • TSaveDialog • TSelectDirectoryDialog • TColorDialog • TFontDialog • TFindDialog • TReplaceDialog • TTaskDialog • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog
Data Controls TDBNavigator/ja • TDBText/ja • TDBEdit/ja • TDBMemo/ja • TDBImage/ja • TDBListBox/ja • TDBLookupListBox/ja • TDBComboBox/ja • TDBLookupComboBox/ja • TDBCheckBox/ja • TDBRadioGroup/ja • TDBCalendar/ja • TDBGroupBox/ja • TDBGrid/ja • TDBDateTimePicker/ja
Data Access TDataSource/ja • TCSVDataSet/ja • TSdfDataSet/ja • TBufDataset/ja • TFixedFormatDataSet/ja • TDbf/ja • TMemDataset/ja
System TTimer • TIdleTimer • TLazComponentQueue • THTMLHelpDatabase • THTMLBrowserHelpViewer • TAsyncProcess • TProcessUTF8 • TProcess • TSimpleIPCClient • TSimpleIPCServer • TXMLConfig • TEventLog • TServiceManager • TCHMHelpDatabase • TLHelpConnector
Misc TColorButton • TSpinEdit • TFloatSpinEdit • TArrow • TCalendar • TEditButton • TFileNameEdit • TDirectoryEdit • TDateEdit • TTimeEdit • TCalcEdit • TFileListBox • TFilterComboBox • TComboBoxEx • TCheckComboBox • TButtonPanel • TShellTreeView • TShellListView • TXMLPropStorage • TINIPropStorage • TJSONPropStorage • TIDEDialogLayoutStorage • TMRUManager • TStrHolder
LazControls TCheckBoxThemed • TDividerBevel • TExtendedNotebook • TListFilterEdit • TListViewFilterEdit • TLvlGraphControl • TShortPathEdit • TSpinEditEx • TFloatSpinEditEx • TTreeFilterEdit • TExtendedTabControl •
RTTI TTIEdit • TTIComboBox • TTIButton • TTICheckBox • TTILabel • TTIGroupBox • TTIRadioGroup • TTICheckGroup • TTICheckListBox • TTIListBox • TTIMemo • TTICalendar • TTIImage • TTIFloatSpinEdit • TTISpinEdit • TTITrackBar • TTIProgressBar • TTIMaskEdit • TTIColorButton • TMultiPropertyLink • TTIPropertyGrid • TTIGrid
SQLdb TSQLQuery/ja • TSQLTransaction/ja • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection • TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TMySQL57Connection • TSQLite3Connection/ja • TIBConnection • TFBAdmin • TFBEventMonitor • TSQLDBLibraryLoader
Pascal Script TPSScript • TPSScriptDebugger • TPSDllPlugin • TPSImport_Classes • TPSImport_DateUtils • TPSImport_ComObj • TPSImport_DB • TPSImport_Forms • TPSImport_Controls • TPSImport_StdCtrls • TPSCustomPlugin
SynEdit TSynEdit • TSynCompletion • TSynAutoComplete • TSynMacroRecorder • TSynExporterHTML • TSynPluginSyncroEdit • TSynPasSyn • TSynFreePascalSyn • TSynCppSyn • TSynJavaSyn • TSynPerlSyn • TSynHTMLSyn • TSynXMLSyn • TSynLFMSyn • TSynDiffSyn • TSynUNIXShellScriptSyn • TSynCssSyn • TSynPHPSyn • TSynTeXSyn • TSynSQLSyn • TSynPythonSyn • TSynVBSyn • TSynAnySyn • TSynMultiSyn • TSynBatSyn • TSynIniSyn • TSynPoSyn
Chart TChart • TListChartSource • TRandomChartSource • TUserDefinedChartSource • TCalculatedChartSource • TDbChartSource • TChartToolset • TChartAxisTransformations • TChartStyles • TChartLegendPanel • TChartNavScrollBar • TChartNavPanel • TIntervalChartSource • TDateTimeIntervalChartSource • TChartListBox • TChartExtentLink • TChartImageList
IPro TIpFileDataProvider • TIpHtmlDataProvider • TIpHttpDataProvider • TIpHtmlPanel
Virtual Controls TVirtualDrawTree • TVirtualStringTree • TVTHeaderPopupMenu