ComboBoxのドロップダウンリストの幅の変更
Form のOnActivate イベントに記述します。
procedure TForm1.FormActivate(Sender: TObject); begin //ドロップダウンリストの幅を2倍にします Combobox1.Perform(CB_SETDROPPEDWIDTH,Combobox1.width * 2,0); end;
ComboBoxを開閉するコード
ComboBox1.Perform(CB_SHOWDROPDOWN, 1, 0); //開く場合 ComboBox1.Perform(CB_SHOWDROPDOWN, 0, 0); //閉じる場合
ComboBoxにプリンタ名を割当てる
usesに Printers を追加して下さい。
ComboBox1.Items.Assign(Printer.Printers);
ComboBoxに入力し、リストに追加する
//ComboBoxのKeyPressイベント
procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
var
S: string;
i: Integer;
cmBox:TComboBox;
begin
if Key=chr(VK_RETURN) then begin //Enterキーで登録
cmBox:=TComboBox(Sender);
S:=cmBox.Text;
if S<>'' then begin
i:=cmBox.Items.IndexOf(S);
if i >= 0
then begin
cmBox.Items.Move(i,0); //同じものがあれば最上行に移動
cmBox.Text:=cmBox.Items[0];
end
else cmBox.Items.Insert(0,S); //新規は最上行に挿入 最下行に追加する時は cmBox.Items.Add(S);
end;
Key:=#0;
end;
end;
ComboBoxのリストをFILEに書き出す
//二つのComboBoxとListBoxのリストをテキストファイル「List.txt」に書き出します
private
procedure Write_List;
・
・
procedure TForm1.Write_List;
var stList: TStringList;
begin
stList:=TStringList.Create;
try
stList.Clear;
stList.Add('[List1]');
stList.AddStrings(ComboBox1.Items);
stList.Add('[List2]');
stList.AddStrings(ComboBox2.Items);
stList.Add('[ListBox用]');
stList.AddStrings(ListBox1.Items);
stList.SaveToFile(ExtractFilePath(Application.ExeName)+'List.txt');
// stList.SaveToFile(ExtractFilePath(Application.ExeName)+'List.txt', TEncoding.Unicode); //Delphi2009
finally
stList.Free;
end;
end;
ComboBoxのリストにFILEから読込む
//二つのComboBoxとListBoxのリストにテキストファイル「List.txt」から読込みます
private
procedure Read_List;
・
・
procedure TForm1.Read_List;
var
stList: TStringList;
i , ListNumber: Integer;
begin
stList:=TStringList.Create;
try
try
stList.LoadFromFile(ExtractFilePath(Application.ExeName)+'List.txt');
ListNumber:=0;
for i:=0 to stList.Count-1 do begin
if stList[i]='[List1]' then begin
ListNumber:=1;
ComboBox1.Clear;
Continue;
end;
if stList[i]='[List2]' then begin
ListNumber:=2;
ComboBox2.Clear;
Continue;
end;
if stList[i]='[ListBox用]' then begin
ListNumber:=3;
ListBox1.Clear;
Continue;
end;
case ListNumber of {各Listへ格納}
1: ComboBox1.Items.Add(stList[i]);
2: ComboBox2.Items.Add(stList[i]);
3: ListBox1.Items.Add(stList[i]);
end;
end;
except
on EFOpenError do ShowMessage('リストの読込みに失敗しました');
end;
finally
stList.Free;
end;
end;
ページトップ