• StringGrid单元格绑定ComboBox、DateTimePicker或窗口传值


    一、初始化控件状态

    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;

    效果展示

    Demo下载

  • 相关阅读:
    [BJWC2010]外星联络
    [NOI2015]品酒大会
    工艺 /【模板】最小表示法
    [NOI2016]优秀的拆分
    [HEOI2016/TJOI2016]字符串
    [SDOI2016]生成魔咒
    【模板】后缀自动机 (SAM)【SA解法】
    [湖南集训]图森
    [USACO17DEC]Standing Out from the Herd P
    Annihilate
  • 原文地址:https://www.cnblogs.com/liessay/p/16228372.html
Copyright © 2020-2023  润新知