Combobox in a GridString

Hey Gang!

Can anyone show me how to add a dropdown box to a grid string. I know I can buy the component, but I just want to add the additional component to the grid without all the other fluff.

Thanks,
Chad
dhertzfeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simonetCommented:
Just set the PickList property of the Columns object of the TStringGRid and the ComboBox will be displayed automatically.

Alex
0
intheCommented:
hi,

procedure TForm1.FormCreate(Sender: TObject);
begin
combobox1.parent:=stringgrid1;
combobox1.Top := stringgrid1.TopRow;
combobox1.Left := stringgrid1.LeftCol;
end;


then set up the onchange events and should be ok..
Regards Barry
0
dhertzfeAuthor Commented:
I'm sorry Barry, I didn't make myself clear. You did put a combobox in the upper left hand corner of the grid. What I was looking for was placing a combobox in each cell of the second column in the grid.

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

dhertzfeAuthor Commented:
Alex,
Can you give me an example of your suggestion? I'm just getting the hang of String grids and all that you can do with them.
0
victor_christovCommented:
Try this.


TYPE
  TForm1 = class(TForm)

    StringGrid1: TStringGrid;
    ComboBox1  : TComboBox  ;

    procedure FormCreate         (Sender: TObject);
    procedure StringGrid1Click   (Sender: TObject);
    procedure StringGrid1KeyPress(Sender: TObject; var Key: Char);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Visible := False ;
end;


Procedure ComboToStrGrid ;
Var
  nComboLeft, nComboTop, nComboWidth : LongInt ;

Begin
   With Form1 do begin
      With StringGrid1 do begin
       nComboTop   := Top   + CellRect(Col,Row).Top ;
       nComboLeft  := Left  + CellRect(Col,Row).Left;
       nComboWidth := CellRect(Col,Row).Right - CellRect(Col,Row).Left ;
      end;
      With ComboBox1 do begin
       Top      := nComboTop   ;
       Left     := nComboLeft  ;
       Width    := nComboWidth ;
       Visible  := True        ;
       SetFocus ;
      end;
   end;
End ;

procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  ComboToStrGrid ;
end;


procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
  ComboToStrGrid ;
end;

END.

You can set the additional constrains for activation of ComboBox1.

Regards Victor

0
dhertzfeAuthor Commented:
Victor,

It almost works....the dropdown boxes appear at the top of my form rather than within the confines of the grid. It is showing up in the correct column position.

What am I missing?

var
  nComboLeft, nComboTop, nComboWidth : LongInt ;

Begin
   With Form1 do begin
      With grdTestCase do begin
       nComboTop   := CellRect(Col,Row).Top ;
       nComboLeft  := CellRect(Col,Row).Left;
       nComboWidth := CellRect(Col,Row).Right - CellRect(Col,Row).Left ;
      end;
      With ComboBox1 do begin
       Top      := nComboTop   ;
       Left     := nComboLeft  ;
       Width    := nComboWidth ;
       Visible  := True        ;
       SetFocus ;
      end;
   end;
End ;

Thanks
0
dhertzfeAuthor Commented:
Victor,

I found that adding the line
      combobox1.parent:=stringgrid1;

to yopur code solved the problem of placing the combobox correctly within the grid. Now, my items that I assigned to the grid aren't showing up.  I'm using the code below on the onClick event.
  with Form1 do
  begin
      combobox1.parent:=stringgrid1;
      combobox1.Items.Add ('test');
      combobox1.Items.Add ('test1');
      combobox1.Items.Add ('test2');
      ...
      ...
  end;

Any ideas?
0
dhertzfeAuthor Commented:
I should clarify that last comment, the box dosen't show being dropped down. It looks like the dropdownbox is within the grid cell and not showing the items within the box.

Arrrrrrgggggg!!!!
0
dhertzfeAuthor Commented:
Ok gang,

I am offering all my points to get this problem solved. I know its probably something stupid I'm over looking but I have to get this solved.

Thanks
0
dhertzfeAuthor Commented:
Victor,

If you add a comment, I'll give you the 100 points. Your answer was correct, I just forgot to mention that my grid was on a tab. I used your code with the recognition of the tab and everything worked great.

Thanks
0
victor_christovCommented:
Hi Chad

I am happy that my code is qood for you.Your question was very usefull for me, because when I try to solve it there raised some additional problems. When I find its solution, I'll send you e-mail.

Regards, Victor.



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhertzfeAuthor Commented:
Thanks for your help!

Chad
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.