一、初始化控件状态
procedure TForm7.FormCreate(Sender: TObject); beginwith StringGrid1 dobeginColWidths[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); varR: TRect;org: TPoint; beginif Form8 = nil thenApplication.CreateForm(TForm8, Form8);with Sender as TStringGrid dobegin//第一列if ACol = 1 thenbeginPerform(WM_CANCELMODE, 0, 0);R := CellRect(ACol, ARow);org := Self.ScreenToClient(ClientToScreen(R.topleft));with ComboBox1 dobeginSetBounds(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 thenbeginPerform(WM_CANCELMODE, 0, 0);R := CellRect(ACol, ARow);org := Self.ScreenToClient(ClientToScreen(R.topleft));with DateTimePicker1 dobeginSetBounds(org.X, org.Y, R.right - R.left, Height);if Cells[ACol, ARow] <> '' thenDate := StrToDate(Cells[ACol, ARow]);Show;BringToFront;SetFocus;end;end;if ACol = 3 thenbeginPerform(WM_CANCELMODE, 0, 0);Form8.Caption := VarToStr(ARow) + '-' + VarToStr(ACol);Form8.ShowModal;with StringGrid1 doStringGrid1.cells[ACol, ARow] := Form8.Caption;end;end; end;
三、DateTimePicker1失去焦点
procedure TForm7.DateTimePicker1Exit(Sender: TObject); beginwith Sender as TDateTimePicker dobeginHide;with StringGrid1 docells[Col, Row] := DateToStr(DateTimePicker1.Date);end; end;
四、ComboBox1失去焦点
procedure TForm7.ComboBox1Exit(Sender: TObject); beginwith Sender as TComboBox dobeginHide;if itemindex >= 0 thenbeginwith StringGrid1 docells[Col, Row] := items[itemindex];end;end; end;
效果展示
Demo下载
本文来自博客园,作者:liessay,转载请注明原文链接:https://www.cnblogs.com/liessay/p/16228372.html