一、初始化控件状态
procedure TForm7.FormCreate(Sender: TObject); begin with StringGrid1 do begin ColWidths[0] := 15; Cells[1, 0] := 'Combobox'; ColWidths[1] := 100; Cells[2, 0] := 'DateTimePicker'; ColWidths[2] := 100; Cells[3, 0] := 'Form'; ColWidths[3] := 100; end; ComboBox1.visible := False; DateTimePicker1.Visible := False; end;
二、选择单元格显示控件
procedure TForm7.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean); var R: TRect; org: TPoint; begin if Form8 = nil then Application.CreateForm(TForm8, Form8); with Sender as TStringGrid do begin //第一列 if ACol = 1 then begin Perform(WM_CANCELMODE, 0, 0); R := CellRect(ACol, ARow); org := Self.ScreenToClient(ClientToScreen(R.topleft)); with ComboBox1 do begin SetBounds(org.X, org.Y, R.right - R.left, Height); itemindex := Items.IndexOf(Cells[ACol, ARow]); Show; BringToFront; SetFocus; DroppedDown := True; end; end; if ACol = 2 then begin Perform(WM_CANCELMODE, 0, 0); R := CellRect(ACol, ARow); org := Self.ScreenToClient(ClientToScreen(R.topleft)); with DateTimePicker1 do begin SetBounds(org.X, org.Y, R.right - R.left, Height); if Cells[ACol, ARow] <> '' then Date := StrToDate(Cells[ACol, ARow]); Show; BringToFront; SetFocus; end; end; if ACol = 3 then begin Perform(WM_CANCELMODE, 0, 0); Form8.Caption := VarToStr(ARow) + '-' + VarToStr(ACol); Form8.ShowModal; with StringGrid1 do StringGrid1.cells[ACol, ARow] := Form8.Caption; end; end; end;
三、DateTimePicker1失去焦点
procedure TForm7.DateTimePicker1Exit(Sender: TObject); begin with Sender as TDateTimePicker do begin Hide; with StringGrid1 do cells[Col, Row] := DateToStr(DateTimePicker1.Date); end; end;
四、ComboBox1失去焦点
procedure TForm7.ComboBox1Exit(Sender: TObject); begin with Sender as TComboBox do begin Hide; if itemindex >= 0 then begin with StringGrid1 do cells[Col, Row] := items[itemindex]; end; end; end;
效果展示
本文来自博客园,作者:liessay,转载请注明原文链接:https://www.cnblogs.com/liessay/p/16228372.html
标签:do,begin,end,ACol,ARow,ComboBox,单元格,StringGrid,Sender From: https://www.cnblogs.com/ynmsnc/p/18370032