Jump to content
David P

TListItem.MinWidth doesn't work

Recommended Posts

Hello

 

I'm using a TListView in Report mode and I'm creating columns dynamically.  I am setting TListItem.MinWidth = 50, but when ran, this is being ignored.

 

What am I missing?

 

Thanks

 

David

Share this post


Link to post
Posted (edited)

TListItem does not have a MinWidth property, you meant TListColumn instead.

 

In any case, what version of Delphi are you using?  I can reproduce the problem in Delphi 12 w/ Patch 1 installed.  When dragging a column divider in the header, the TListColumn.MinWidth and TListColumn.MaxWidth values get ignored when the TListView is processing an HDN_ITEMCHANGING notification from the header.  TListView looks for HDN_ITEMCHANGING to trigger a redraw of the active TListItem, but there is an 'else' that skips the MinWidth/MaxWidth processing:

if (Mask and HDI_WIDTH) <> 0 then
  begin
    if code = HDN_ITEMCHANGING then
      EnsureItemRedrawn(nil)
    else // <-- HERE!!
    begin
      Col := GetColumnFromTag(Item);
      if Col.MinWidth >= cxy then
        cxy := Col.MinWidth
      else
        if (Col.MaxWidth > 0) and (Col.MaxWidth <= cxy) then
          cxy := Col.MaxWidth;
        Col.Width := cxy;
    end;
  end;

When I take that 'else' out, the MinWidth/MaxWidth values work properly as expected:

if (Mask and HDI_WIDTH) <> 0 then
  begin
    if code = HDN_ITEMCHANGING then
      EnsureItemRedrawn(nil);
    //else // <-- HERE!!!
    begin
      Col := GetColumnFromTag(Item);
      if Col.MinWidth >= cxy then
        cxy := Col.MinWidth
      else
        if (Col.MaxWidth > 0) and (Col.MaxWidth <= cxy) then
          cxy := Col.MaxWidth;
        Col.Width := cxy;
    end;
  end;

This bug does not exist in Delphi 10.3, so it was introduced sometime after that version:

if (Mask and HDI_WIDTH) <> 0 then
  begin
    // NO redraw LOGIC HERE!!!
    Col := GetColumnFromTag(Item);
    if Col.MinWidth >= cxy then
      cxy := Col.MinWidth
    else
      if (Col.MaxWidth > 0) and (Col.MaxWidth <= cxy) then
        cxy := Col.MaxWidth;
      Col.Width := cxy;
  end;

I would suggest filing a bug report with Embarcadero, but Quality Portal is still down.  I'll report it privately.

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post
Posted (edited)

Thanks. I'm using CB 11.3

 

In the interim I thought I'd detect the column widths changing and then enforce the min width, but there doesn't appear to be any event that fires when a colmn has changed size.

 

Never mind, found a solution you wrote in 2012.

Edited by David P

Share this post


Link to post
1 hour ago, David P said:

In the interim I thought I'd detect the column widths changing and then enforce the min width, but there doesn't appear to be any event that fires when a colmn has changed size.

Correct, there is no event published for this.  You would have to subclass the ListView to handle the HDN_ITEMCHANGING notification directly.  Alternatively, if you are building with Runtime Packages disabled, you can make a copy of Vcl.ComCtrls.pas and add it to your project, and then make the code change I mentioned earlier to fix the bug.

1 hour ago, David P said:

Never mind, found a solution you wrote in 2012.

:classic_biggrin:

Share this post


Link to post

I submitted this as a bug and they could not reproduce it in 11.3. Now, neither can I.  I can reduce the column width to below MinWidth but as soon as I release the mouse button it springs back to MinWidth.  I'm  starting to doubt my sanity.

Share this post


Link to post
Posted (edited)

As I said, I confirmed the bug does not exist in 10.3, but does exist in 12.0, so it's possible that it does not exist in 11.3 either. I don't have that version to check with. If you look in your IDE's copy of the Vcl.ComCtrls.pas source file, do you see the code I showed earlier?

Edited by Remy Lebeau

Share this post


Link to post

Both 11.3 and 12.1 have that 'else' in place and both enforce the MinWidth setting.

 

It is possible to drag to a smaller width than MinWidth but upon release it springs to MinWidth width.  I'm now wondering if I was just fixating on the dragging and not when the mouse was released.

Share this post


Link to post
On 4/9/2024 at 7:44 AM, David P said:

It is possible to drag to a smaller width than MinWidth but upon release it springs to MinWidth width.  I'm now wondering if I was just fixating on the dragging and not when the mouse was released.

Interesting.  I just tested that in 12.0 Patch 1 and sure enough the springing works.  However, removing the 'else' does prevent the dragging from going smaller than the MinWidth to begin with.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×