Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
497 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (1)


How to create a Combobox winthin in a StringgridGo to Boris Benjamin Wittfoth's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Component Writing
Skill Level:
Scoring:
Last Update:
10/21/2004
Search Keys:
delphi delphi3000 article borland vcl code-snippet How to create a Combobox in a Stringgrid TCombobox TStringGrid TControlStringGrid WM_COMMAND
Times Scored:
7
Visits:
7714
Uploader: Boris Benjamin Wittfoth
Company: Currex GmbH
Reference: N/A
 
Question/Problem/Abstract:
How to dynamically create a Combobox within a Cell of a StringGrid
Answer:



You need a descendent of TStringgrid that properly reflects WM_COMMAND to
embedded controls. The standard grid does not do it since it is not intended
to play parent to other controls.

Additionaly simply declare a Set- and GetMethod to access the items of die combobox

best regards
Boris Benjamin Wittfoth



unit BWControlStringGrid;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids,stdctrls;

type
  TBWControlStringGrid = class(TStringGrid)
  private
    fComboBox:TCombobox;
    Procedure WMCommand( var msg: TWMCommand ); message WM_COMMAND;
    procedure DblClick;override;
    procedure Click;override;
    procedure RelocateComboBox;
    procedure HideCombobox;
  protected
    procedure KeyPress(var Key: Char); override;
  public
    Constructor Create(AOWner:TComponent);override;
    Destructor Destroy;override;
  published
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('hEaDRoOm', [TBWControlStringGrid]);
end;
procedure TBWControlStringGrid.WMCommand(var msg: TWMCommand);
begin
  If EditorMode and ( msg.Ctl = fComboBox.Handle ) Then
    inherited
  Else
    If msg.Ctl <> 0 Then
      msg.result :=
        SendMessage( msg.ctl, CN_COMMAND,
                     TMessage(msg).wparam,
                     TMessage(msg).lparam );
end;
procedure TBWControlStringGrid.KeyPress(var Key: Char);
begin
  if Key=#13 then
    RelocateComboBox
   else HideCombobox;

end;
procedure TBWControlStringGrid.DblClick;
begin
  inherited;
  RelocateComboBox;
end;
procedure TBWControlStringGrid.Click;
begin
  inherited;
  HideCombobox;
end;
procedure TBWControlStringGrid.RelocateComboBox;
begin
  fcombobox.boundsrect := CellRect( Selection.Left, Selection.Top);
  fcomboBox.Visible:=TRUE;
  fcombobox.setfocus;
end;
procedure TBWControlStringGrid.HideCombobox;
begin
  fcomboBox.Visible:=false;
end;
Constructor TBWControlStringGrid.Create(AOWner:TComponent);
begin
  inherited Create(Aowner);
  fComboBox:=TComboBox.Create(self);
  fComboBox.Parent:=self;
  fComboBox.Visible:=FALSE;
  Options:=Options-[goRangeSelect];
end;
Destructor TBWControlStringGrid.Destroy;
begin
  fComboBox.Destroy;
  inherited destroy;
end;



end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
what to do with the combo box...
    duncan parsons (Jul 19 2002 6:33PM)

this is great, but is just the skeleton, of course..

There needs to be some mechansim for getting the combo's text/selection into the cell, also for relaying the cells contents into the combo in the first place.

This can be done in the Hide and Relocate methods.

The whole thing can get unwieldy if you add a lot of get/set methods for updating the combos dropdownlist, etc, so making the Combo a Public property, rather than just a private field might help with that - the onus is then on the programmer to deal with the combo directly - it is unlikely, for instance, that the dropdownlist would be the same for each column.

Or two new events could be triggered - OnHide and OnRelocate
eg:
TComboVisibleChangeEvent=procedure (Sender:TObject;Row,Col:Longint;Combo:TComboBox;AllowVisibleChange:boolean) of object

fOnHide:TComboVisibleChangeEvent;
fOnRelocate:TComboVisibleChangeEvent;

etc.

This way the Combo would be make public when needed. When the Relocate fires, the dropdownlist could be repopulated, etc

Just ideas for whoever wants them!

DSP

Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
E. DSpirito
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)