[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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
0
dhertzfe
Asked:
dhertzfe
1 Solution
 
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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
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
 
dhertzfeAuthor Commented:
Thanks for your help!

Chad
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now