Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to writeA combobox in a cell of a stringgrid Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
10-Sep-02
Category
VCL-General
Language
Delphi 2.x
Views
89
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: William Gerbert

A combobox in a cell of a stringgrid

Answer:

Here's another great trick by programmer illusionists. What appears as embedded 
comboboxes within a stringgrid is actually just a combobox floating above the 
stringgrid, and it just so happens to be the exact same size as the stringgrid cell 
underneath it. 

Here's the basics:


1   procedure TfrmMain.FormCreate(Sender: TObject);
2     begin
3       StringGrid1.DefaultRowHeight := ComboBox1.Height;
4     end;
5   
6     procedure TfrmMain.StringGrid1DrawCell(Sender: TObject; Col, Row:
7       Integer; Rect: TRect; State: TGridDrawState);
8     var
9       R: TRect;
10    begin
11      if (Col >= StringGrid1.FixedCols) and
12        (Row >= StringGrid1.FixedRows) and
13        (gdFocused in State) then
14        with ComboBox1 do
15        begin
16          BringToFront;
17          CopyRect(R, Rect);
18          R.TopLeft :=     frmMain.ScreenToClient(
19                           StringGrid1.ClientToScreen(R.TopLeft));
20          R.BottomRight := frmMain.ScreenToClient(
21                           StringGrid1.ClientToScreen(R.BottomRight));
22          SetBounds(R.Left, R.Top, R.Right-R.Left, R.Bottom-R.Top);
23        end;
24    end;
25  
26    procedure TfrmMain.StringGrid1TopLeftChanged(Sender: TObject);
27    var
28    R: TRect;
29    begin
30      with StringGrid1 do
31          CopyRect(R, CellRect(Col, Row));
32  
33      with ComboBox1 do
34      begin
35        Visible := False;
36        R.TopLeft :=     frmMain.ScreenToClient(
37                         StringGrid1.ClientToScreen(R.TopLeft));
38        R.BottomRight := frmMain.ScreenToClient(
39                         StringGrid1.ClientToScreen(R.BottomRight));
40        SetBounds(R.Left, R.Top, R.Right - R.Left, R.Bottom - R.Top);
41      end;
42  
43      with StringGrid1 do
44        if (TopRow <= Row) and (TopRow + VisibleRowCount > Row) then
45           ComboBox1.Show;
46    end;
47  
48    procedure TfrmMain.ComboBox1Change(Sender: TObject);
49    begin
50      with StringGrid1 do
51        Cells[Col, Row] := ComboBox1.Text;
52    end;



In essence, the main routine here is the stringgrid's OnDrawCell event handler. Of 
course, I also set the stringgrid's DefaultRowHeight property to be the same height 
as the combobox. In addition, the stringgrid's OnTopLeftChanged event handler is 
used to hide the combobox when the user scrolls out of view. Also, when the user 
selects an item from the combobox, simply place the text in the current Col/Row. 

You can also do a couple other little tricks such as setting the stringgrid's Objects[] property to point to the combobox, as well as possibly setting the combobox's Parent property to point to the stringgrid. However, I've had problems with the Parent approach -- namely, that of dropping down the listbox associated with the combobox.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC