TComboBox/ja
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
日本語 (ja) │
A TComboBox is a combination of an edit box and a (drop-down) list allowing one of several options to be chosen.
使い方
TComboBoxは、エディットボックスと(ドロップダウン)リストの組み合わせで、いくつかのオプションの中から一つを選択できるようにするものである。
フォーム上でTComboBoxを使用するには、Component Palette/jaのStandard tab/jaタブからそれを選択し、フォーム上でクリックして配置するだけである。
ComboBox内に保存された文字列は、TStrings型のプロパティItemsに保存される。そのため、TStringListやその親クラスであるTStringsで行うように、ComboBox内の文字列を割り当てたり削除したりすることができる。
フォームForm1上のcombobox ComboBox1を使用するいくつかの例を紹介する:
コンボボックスを埋める
オブジェクトインスペクタによって
- フォーム上のComboBoxをクリックで選択する。
- オブジェクトインスペクタでプロパティタブに移動し、プロパティItemsを選択する。
- 3つのドットがあるボタンをクリックする。すると、文字列エディタが開く。
- テキストを入力し、OKで作業を確定する。
フォームを作るときにコードから
- フォームのOnCreateイベントハンドラを作成するには、まずフォームをクリックし、オブジェクトインスペクタでイベントタブを選択する。OnCreateイベントを見つけ、[...]ボタンをクリックするか、フォーム内のどこかをダブルクリックする。
- ソースエディタに移動したら、望む選択テキストを挿入することになる。例えば、以下のように記述することになる:
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.Clear; //存在しているすべての選択を解除
ComboBox1.Items.Add('Red'); //選択を加える
ComboBox1.Items.Add('Green');
ComboBox1.Items.Add('Blue');
ComboBox1.Items.Add('Random Color');
end;
選択したときに何かを起こさせる
すべてのコンポーネントと同様に、TComboBoxもさまざまなイベントを提供しており、これらはユーザーがコンボボックスを使用したときに呼び出される。ComboBoxでの選択の変更に応答するには、OnChangeイベントを使用できる:
- フォーム上のComboBoxをダブルクリックするか、オブジェクトインスペクタでOnChangeイベントを選択し、[...]ボタンをクリックする。
- イベントハンドラが作成される。ここで、必要なソースコードを挿入できる。私たちの例では、フォームの背景色を変更したいと考えている:
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
case ComboBox1.ItemIndex of //どれが(どのアイテムが)現在選択されているか
0: Color:=clRed;
1: Color:=clGreen;
2: Color:=clBlue;
3: Color:=Random($1000000);
end;
end;
- アプリケーションを起動すると、によりフォームの背景色が変更される。
イベント
OnGetItemsイベントは、ウィジェットセットのアイテムリストを作成できる場合に呼び出される。そのため、コンボボックスのボタンが押されたときにトリガーされる。
所有者によって描かれたコンボボックス
一般的には、ユーザーが設定で選択したテーマをComboBoxに表示させる方が有利だ。しかし、場合によっては(例えば、カラフルな表面のゲームをプログラムする場合など)、この標準から逸脱し、独自の選択に基づいて描画することもできる。その方法は次の通りである:
Note: Parameters of ComboBoxDrawItem:
Control:
If multiple controls (e.g. multiple ComboBoxes) access this event handle, you know which control caused the event. In our example, instead of ComboBox1.Canvas.FillRect(ARect)
you could also write TComboBox(Control).Canvas.FillRect(ARect)
. However, you should still check in advance, whether it is a TComboBox:
if Control is TComboBox then
TComboBox(Control).Canvas.FillRect(ARect);
Index:
Specifies the item location, so you have access to the string <ComboBox>.Items[Index]
.
ARect:
Describes the rectangle, which is necessary for drawing the background.
State:
Status of the items, whether normal, focused, selected etc.
塗りつぶされた四角形を描く
- You can modify the example Fill ComboBox by code when you create the form.
- Change from ComboBox1 in the Object Inspector the property Style to csOwnerDrawFixed.
- Create in the Object Inspector the event handler for the event OnDrawItem, by clicking on the button [...].
- Add the following code to the handler:
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
var
ltRect: TRect;
procedure FillColorfulRect(aCanvas: TCanvas; myRect: TRect); //paint random color
// Fills the rectangle with random colours
var
y: Integer;
begin
for y:=myRect.Top to myRect.Bottom - 1 do begin
aCanvas.Pen.Color:=Random($1000000);
aCanvas.Line(myRect.Left, y, myRect.Right, y);
end;
end;
begin
ComboBox1.Canvas.FillRect(ARect); //first paint normal background
ComboBox1.Canvas.TextRect(ARect, 22, ARect.Top, ComboBox1.Items[Index]); //paint item text
ltRect.Left := ARect.Left + 2; //rectangle for color
ltRect.Right := ARect.Left + 20;
ltRect.Top := ARect.Top + 1;
ltRect.Bottom := ARect.Bottom - 1;
ComboBox1.Canvas.Pen.Color:=clBlack;
ComboBox1.Canvas.Rectangle(ltRect); //draw a border
if InflateRect(ltRect, -1, -1) then //resize rectangle by one pixel
if Index = 3 then
FillColorfulRect(ComboBox1.Canvas, ltRect) //paint random color
else begin
case Index of
0: ComboBox1.Canvas.Brush.Color := clRed;
1: ComboBox1.Canvas.Brush.Color := clGreen;
2: ComboBox1.Canvas.Brush.Color := clBlue;
end;
ComboBox1.Canvas.FillRect(ltRect); //paint colors according to selection
end;
end;
- Your example might look like:
Preceded image
In this example, we load a few images in a TImageList and draw them in front of the items in the combobox. It is a simple example which only generally show what you can do. I don't run explicitly details, such as checking, whether the corresponding image exists etc. in this example. This should be done by you depending on the need.
- Create an application analogous example Fill ComboBox by code when you create the form.
- Change from ComboBox1 in the Object Inspector the property Style to csOwnerDrawFixed.
- Add a TImageList from the component palette Common controls on your form.
- The Height and Width of 16 pixels is preset in ImageList1. We allow this. To fit neatly the images into our combo box, we make the property ItemHeight from ComboBox1 to 18 in the Object Inspector.
- Add four images in the ImageList:
- Doubleclick ImageList1 or leftclick ImageList1 and select ImageList Editor....
- Click on Add and select an image (see <Lazarus directory>/images/... there are various images or icons in 16x16px size).
- Have you added four images, confirm your work with [OK].
- Create in the Object Inspector the event handler for the event OnDrawItem, by clicking on the button [...].
- Add the following code to the handler:
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
begin
ComboBox1.Canvas.FillRect(ARect); //first paint normal background
ComboBox1.Canvas.TextRect(ARect, 20, ARect.Top, ComboBox1.Items[Index]); //paint item text
ImageList1.Draw(ComboBox1.Canvas, ARect.Left + 1, ARect.Top + 1, Index); //draw image according to index on canvas
end;
- Your example might look like:
スタイル
csSimple
csSimple represents itself a simple edit box with a list box underneath. The list box is only shown, if there's enough height allocated. By default the height is set to shown the edit box only.
There's no dropdown icon to show the list box. Items can be changed by using keyboard (arrow keys), or by clicking an an item, if the control is high enough to show the items list.
csSimple style is Windows specific. Non-widows widgetset most likely don't implement and fall back to csDropDown.
Here's an example of (non-autosized) csSimple ComboBox on two different widgetset. (as of Lazarus 2.0.8):
提案されている特徴
読み取り専用
ReadOnly property has been deprecated since 2017. It has been removed as of today.
However, it's possible to reinstate ReadOnly property as a mirror of TEdit readonly.
The complexity of ComboBox is that it bears features of both ListBox and Edit (but it's not a descendant of either). While ComboBox does provides the means to change the selection (just like TEdit), it might be helpful also to make the edit box readonly. (Note, that this is different to csDropDownList. ccDropDownList typically implements a dropdown as a button).
How, ReadOnly property is expected to work:
- it's available for any ComboBox style
- it's does not influence or is not influenced by any ComboBox style selected or changed (unlike the previous implementation of ReadOnly)
- if set to true, the edit box (if supported by the comboBox style on the widgetset) becomes ReadOnly (in terms of TEdit). The content cannot be changed by any text input actions (such as editing, pasting, or dragging the text around).
- the combobox value CAN be changed by selecting a different item from the dropdown, by either using a mouse (or respective key combinations, i.e. up or down arrows)
テキストヒント
As ComboBox bears traits of TEdit, it's should be possible to set TextHint for comboBox, if the value is empty.
Introduce TextHint property that should act similar to TEdit
- if text value of combobox is empty, the TextHint should be presented (in the colors and font style, that's applicable to the current widgetset theme)
以下も参照のこと