StringGridの2セル目にComboBoxを配置します。
ComboBoxのStyleはDropDownListです。
過去ログを見て配置はできたのですけど、
ComboBoxにフォーカスがあるとき、上下左右キーを押したら
隣のセルに移動させたいのです。
特に↓キー押下の場合がよくわかりません。
Alt+↓が押されたときはList、↓だけの場合は下のセルへ移動・・・。
見よう見真似でkeybd_eventなどを使ってみたのですが、実行したらひたすらRETURNキーが押されっぱなしになってしまったりと上手くできません。
どなたかご教授をお願いします。
>https://www.petitmonte.com/bbs/answers?question_id=914
ここの最後尾のサンプルを下のようにチョット書き換え。
{ EVENT : ComboBox1 OnKeyDown }
procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Shift <> [] then exit;
with StringGrid1 do begin
if Key = VK_RETURN then begin
ActiveControl := StringGrid1;
Col := Col + 1;
end else
if Key = VK_LEFT then begin
ActiveControl := StringGrid1;
Col := Col - 1;
end else
if Key = VK_RIGHT then begin
ActiveControl := StringGrid1;
Col := Col + 1;
end else
if (Key = VK_UP)and(Row > 1) then begin
ActiveControl := StringGrid1;
Row := Row - 1;
end else
if (Key = VK_DOWN)and(Row < RowCount - 1) then begin
ActiveControl := StringGrid1;
Row := Row + 1;
end;
Key := 0;
end;
end;
※ただし、ComboBox1の Styleが DropDownListの場合は、これだけではマズイと思うけど…
モスッラ〜や!さん
ありがとうございます。
参考にして、あれもダメ、これもダメなどと
いろいろ工夫しながらやってみた所、どうにか完成しました。
ソースはこんな感じになりました。
public
FGridCtrl:TControl;
//==========================================================
procedure TForm1.GridGetEditText(Sender: TObject; ACol, ARow: Integer;
var Value: String);
begin
if (ACol = 2) then begin
FGridCtrl := Grid.Controls[0];
if FGridCtrl is TInplaceEdit then begin
ShowCombo;
end;
end;
end;
//==========================================================
procedure TForm1.ComboExit(Sender: TObject);
begin
Grid.Cells[Grid.Col,Grid.Row] := Combo.Text;
Combo.Visible := False;
ActiveControl := Grid;
end;
//==========================================================
procedure TForm1.ComboKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key = VK_RETURN) then begin
ComboExit(Sender);
end;
end;
//==========================================================
procedure TForm1.ShowCombo;
var
ARect:TRect;
begin
ARect := Grid.CellRect(Grid.Col, Grid.Row);
Combo.Top := ARect.Top + Grid.Top + 2;
Combo.Left := ARect.Left + Grid.Left + 2;
Combo.Width := ARect.Right - ARect.Left+2;
Combo.Text := Grid.Cells[Grid.Col,Grid.Row];
Combo.Visible := True;
ActiveControl := Combo;
end;
ツイート | ![]() |