ListView Final Column Autosize creates scrollbar

前端 未结 8 932
闹比i
闹比i 2021-01-13 18:23

I am implementing a custom control which derives from ListView.

I would like for the final column to fill the remaining space (Quite a common task), I have gone abou

8条回答
  •  余生分开走
    2021-01-13 19:08

    You need to override the protected method SetBoundsCore() and perform the column resizing either before or after delegating to base.SetBoundsCore(), depending on whether the ListView width is narrowing or widening.

    The following example is hardcoded for a single column ListView:

    protected override void SetBoundsCore( int x, int y, int width, int height, BoundsSpecified specified )
    {
        ResizeColumns( width, true );
        base.SetBoundsCore( x, y, width, height, specified );
        ResizeColumns( width, false );
    }
    
    private void ResizeColumns( int controlWidth, bool ifShrinking )
    {
        if( Columns.Count < 1 || Parent == null )
            return;
    
        int borderGap = Width - ClientSize.Width;
        int desiredWidth = controlWidth - borderGap;
    
        if( (desiredWidth < Columns[0].Width) == ifShrinking )
            Columns[0].Width = desiredWidth;
    }
    

    Note his code doesn't deal with the specified parameter, i'll leave that up to you!

提交回复
热议问题