Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListView: making editable & adding controls to cells

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Hi,

1) I have a TListView control that I would like the user to be able to directly edit at runtime (i.e. click the cell and type values straight into it) - is this possible?

2) Also, I would like to add a TComboBox to certain cells. I've heard this is possible using certain events (I think its OnDrawItem) but can anyone give an example of how to do this?

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
You will have to detect the conditions of an edit (e.g. double-click or F2),
place a control over the ListView initialized with the correct value,
and detect when you should remove the control and possibly copy the typed value into the ListView (e.g. when the user clicks outside the control).

The ComboBox would only come into play when they were editing, yes? Unless you wanted to draw the drop-down button at all times, then DrawItem might be a good place to do it. Look at the behavior of DBGrid: does that do what you want?

Cheers
 
Basically, I will be using the TListView for user input so it ideally should be editable all the time - I know it's not perhaps the ideal component but it seems to have more to offer than a TStringGrid. At present, it seems unreliable trying to double-click a cell of the TListView to type into it - is there not a way to type straight in without overlaying controls?

How do I actually draw a combobox into a cell using DrawItem - I have no idea how to go about this!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
To enable the TListView to be directly (and easily) editable I have used to following code in the ListView OnMouseDown event:
Code:
if ListView1.SelCount > 0 then
    ListView1.Selected.EditCaption;

I am still having problems with Q2 above.
I want to draw a combobox in SubItem 1 of each row of the ListView and I've come up with the following code in the OnCustomDrawSubItem. The problem is that the event is being called repeatedly which has the effect of making comboboxes flash on screen:
Code:
procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
var
  cb_Intent: TComboBox;
  leftPos: Integer;
begin
  if SubItem = 1 then
  begin
    leftPos := ListView1.Column[0].Width + ListView1.Column[1].Width;
    cb_Intent := TComboBox.Create(Self);
    with cb_Intent do
    begin
      Parent := ListView1;
      Items.Add('In');
      Items.Add('Out');
      Items.Add('InOut');
      Left := leftPos;
      Top := (Item.Index + 1) * (ListView1.Height div ListView1.VisibleRowCount);
      Width := ListView1.Column[2].Width;
    end;
  end;
end;

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
You are creating a new combo box every time you draw; I don't see where it is getting Freed. This might constitute a memory leak.

Perhaps your form should have a set of private instances of TComboBox (created in FormCreate or its constructor)--one per row of ListView1--which you keep positioned where you want them.

Note that this can use a lot of memory if the TListView finds itself with many rows (which is why the DBGrid waits until the edit to move a single, grid-global edit instance around).

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top