dcsimg
Results 1 to 3 of 3
  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    ListVew autosize last column to fill in the blank space

    hello i have a question about the listview how do you fill stretch the last column to fill the right blank side of the listview ?

    Code:
    Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Adding ListView Columns
            ListView1.View = System.Windows.Forms.View.Details
            ListView1.CheckBoxes = False
            ListView1.Columns.Add("Name", 300, HorizontalAlignment.Left)
            ListView1.Columns.Add("Size", 150, HorizontalAlignment.Left)
            ListView1.Columns.Add("Path", ?, HorizontalAlignment.Left) 'auto stretch this column 
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,742

    Re: ListVew autosize last column to fill in the blank space

    Here's how you can do it. Bear in mind that a VScrollBar will be added if your listview needs to scroll...

    Code:
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        'Pinvokes - these are usually Shared methods in a 
        ' Win32NativeMethods class you accumulate 
        Private Const GWL_STYLE As Integer = -16
        Private Const WS_HSCROLL As Integer = &H100000
        Private Const WS_VSCROLL As Integer = &H200000
    
        <DllImport("user32.dll", SetLastError:=True)> _
        Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
        End Function
    
        ' sometimes you use wrappers since many, many, many things could call
        ' SendMessage and so that your code doesnt need to know all the MSG params
        Friend Shared Function IsVScrollVisible(ByVal ctl As Control) As Boolean
            Dim wndStyle As Integer = GetWindowLong(ctl.Handle, GWL_STYLE)
            Return ((wndStyle And WS_VSCROLL) <> 0)
    
        End Function
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Adding ListView Columns
            ListView1.View = System.Windows.Forms.View.Details
            ListView1.CheckBoxes = False
            ListView1.Columns.Add("Name", 300, HorizontalAlignment.Left)
            ListView1.Columns.Add("Size", 150, HorizontalAlignment.Left)
            ListView1.Columns.Add("Path", ListView1.Width - 4 - 450 - If(IsVScrollVisible(ListView1), SystemInformation.VerticalScrollBarWidth, 0), HorizontalAlignment.Left) 'auto stretch this column
    
            For x As Integer = 1 To 20
                ListView1.Items.Add(New ListViewItem(New String() {"test", "test", "test"}))
                ListView1.Columns(2).Width = ListView1.Width - 4 - 450 - If(IsVScrollVisible(ListView1), SystemInformation.VerticalScrollBarWidth, 0)
            Next
        End Sub
    
    End Class

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,527

    Re: ListVew autosize last column to fill in the blank space

    To adjust all columns call AutoResizeColumns
    vb.net Code:
    1. ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)

    To adjust specific column call AutoResizeColumn
    vb.net Code:
    1. ListView1.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.HeaderSize)' adjust the first column
    2. ListView1.AutoResizeColumn(ListView1.Columns.Count - 1, ColumnHeaderAutoResizeStyle.HeaderSize)' adjust the last column



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width