Q&A

  • StringGrid에 콤보박스 삽입하는 방법좀 알려주세요...
procedure TForm1.StringGridSelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  if ACol = 3 then
  begin
    StringGrid.Objects[ACol, ARow] := TComboBox.Create(StringGrid);
    with TComboBox(StringGrid.Objects[ACol, ARow]) do
    begin
      onDropDown := ComboBoxDropDown;
      onSelect   := ComboBoxSelect;
      Style := csDropDownList;
      Parent := StringGrid;
      BoundsRect := StringGrid.CellRect(ACol, ARow);
      Height := StringGrid.DefaultRowHeight-1;
      items.add('aaaaaaaaaaaaaaa');
      onDropDown(Sender);
    end;
  end;
end;


뭐 대충 이런식으로 StringGrid의 셀에 포커스가 갈때마다 셀안에
콤보박스가 생성되게 했는데요..
여기서 생성된 콤보박스의 셀에 포커스가 나갔을때 콤보박스를 Free 시킬려고 하거든요....

아시느분 방법좀 알려주세요~~~
1  COMMENTS
  • Profile
    홍성락 2002.07.23 22:51
    hsr///////////////////////////////////////////////////////////
    예로 2가지입니다(파일참조)
    1.위에것처럼 StringGrid의 Objects로 생성하는예입니다
    procedure TForm1.StringGridSelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
    var
      i : integer;
    begin
      for i := 1 to StringGrid.RowCount-1 do
         if (StringGrid.Objects[3,i] is TComboBox) then
            TComboBox(StringGrid.Objects[3,i]).Visible := False;

      if (ACol = 3) then begin
        if (StringGrid.Objects[ACol, ARow] is TComboBox = False) then begin
           StringGrid.Objects[ACol, ARow] := TComboBox.Create(StringGrid);
           with TComboBox(StringGrid.Objects[ACol, ARow]) do begin
             onDropDown := ComboBoxDropDown;
             onSelect   := ComboBoxSelect;
             Style := csDropDownList;

             Parent := StringGrid;
             BoundsRect := StringGrid.CellRect(ACol, ARow);
             Height := StringGrid.DefaultRowHeight-1;
             items.add('aaaaaaaaaaaaaaa');
             ClientHeight:=100;
             onDropDown(Sender);
           end;
        end
        else TComboBox(StringGrid.Objects[ACol, ARow]).Visible := True;
      end;
    end;

    2.콤보하나로 공통으로 사용
    콤보하아를 스트링 그리드 위에 올려 놓은 다음 Visible을  false로 해놓습니다
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
       if(gdfocused in state) then begin
          with ComboBox1 do begin
               if (StringGrid1.Col = 3) then begin //스트링그리드 1컬럼만 적용
                   Top := Rect.Top + StringGrid1.Top+3;
                   Left := Rect.Left + StringGrid1.Left+3;
                   Width := Rect.Right - Rect.Left-2;
                   //..........여기는 조건에 맡게 아이템 넣기

                   //원상태에 맞게 작업콤보박스셋팅
                  ItemIndex := Items.IndexOf(StringGrid1.Cells[StringGrid1.Col,
                                                                        StringGrid1.Row]);
                  visible := true;
               end
               else
                  Visible := false;
           end;
       end;
    end;

    procedure TForm1.ComboBox1Change(Sender: TObject);
    var
        Set_state, Temp_STR : string;
    begin
        Set_state := ComboBox1.Items[ComboBox1.ItemIndex];
        StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row] := Set_state;
        ComboBox1.Visible := false;
    end;