Results 1 to 6 of 6

Thread: [RESOLVED] How can I "Float" a listview column header to the right?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2013
    Posts
    42

    Resolved [RESOLVED] How can I "Float" a listview column header to the right?

    I have a listview box that contains 2 column headers "File Name" & "File Type". I have my form set to allow you to grow and shrink to the right. The listview grows and shrinks with it to the right. By default I have the columns populated using this code:
    Code:
            ListView1.View = View.Details
            ' Add a column with width 80 and left alignment
            ListView1.Columns.Add("File Name", 306, HorizontalAlignment.Left)
            ListView1.Columns.Add("File Type", 70, HorizontalAlignment.Right)
            'ListView1.Columns.Add("Date Modified", 150, HorizontalAlignment.Left)
    I tried to set the alignment to the right for column 2 but I see no difference. I have read a few other posts but I do not understand gridviews since I am new to programming. Most of my code is built around the listview so I would really like to find a solution to this using the listview controls options.

    Thanks in advance.

    Name:  after.png
Views: 2269
Size:  70.8 KBName:  before.png
Views: 1933
Size:  67.1 KB
    Last edited by nabspot; Jun 20th, 2013 at 07:19 AM. Reason: Adding Images

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: How can I "Float" a listview column header to the right?

    Hi,

    If what you are trying to do is to set the final column in the ListView to occupy the remaining space within the ListView Header then you can use the special ListView Header Width Value of "-2" to do this. i.e:-

    Code:
    Dim colHeaders() As ColumnHeader = {New ColumnHeader With {.Text = "Col1", .Width = 100, .TextAlign = HorizontalAlignment.Left},
                                       New ColumnHeader With {.Text = "Col2", .Width = -2, .TextAlign = HorizontalAlignment.Right}}
    ListView1.Columns.AddRange(colHeaders)
    Hope that helps.

    Cheers,

    Ian
    Last edited by IanRyder; Jun 20th, 2013 at 08:09 AM. Reason: Forgot to add Alignment Attribute

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2013
    Posts
    42

    Re: How can I "Float" a listview column header to the right?

    @ Ian

    The add range code you provided worked great as far as distinguishing the second column to be placed to the right most position of the listbox. This is partially what I was trying to figure out.

    The whole reason I want this is because some of the items in the listbox have long file names which cause the listbox to become scrollable. I'd rather have the user just widen the form to view the rest of the file name.

    So my next question would be, when the user widens the form via size-grip to view the rest of the file name that is cut off, is there a way to refresh the columns so "col2" stays to the right. Basically, as the form size widens I want col2 to stick to the right while col1 widens with the form. Here is your code adjusted to how I am using it:

    Code:
           Dim colHeaders() As ColumnHeader = {New ColumnHeader With {.Text = "File Name", .Width = 306, .TextAlign = HorizontalAlignment.Left},
                                       New ColumnHeader With {.Text = "File Type", .Width = -2, .TextAlign = HorizontalAlignment.Right}}
            ListView1.Columns.AddRange(colHeaders)

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: How can I "Float" a listview column header to the right?

    Hi,

    Easy done. All you need to do is code the Form Resize event to remove the existing Headers of the ListView and then re-add them. i.e:-

    Code:
    Private Sub AddLVHeaders()
      Dim colHeaders() As ColumnHeader = {New ColumnHeader With {.Text = "Col1", .Width = 100, .TextAlign = HorizontalAlignment.Left},
                                            New ColumnHeader With {.Text = "Col2", .Width = -2, .TextAlign = HorizontalAlignment.Right}}
      ListView1.Columns.Clear()
      ListView1.Columns.AddRange(colHeaders)
    End Sub
     
    Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
      AddLVHeaders()
    End Sub
    That should solve the issue for you.

    Cheers,

    Ian

  5. #5
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: How can I "Float" a listview column header to the right?

    Hi,

    Actually, re-reading your last post, to keep column 1 a variable size in relation to the width of the ListView with column 2 justified to the right you could say:-

    Code:
    Private Sub AddLVHeaders()
      Dim colHeaders() As ColumnHeader = {New ColumnHeader With {.Text = "Col1", .Width = CInt(ListView1.Width * 0.75), .TextAlign = HorizontalAlignment.Left},
                                            New ColumnHeader With {.Text = "Col2", .Width = -2, .TextAlign = HorizontalAlignment.Right}}
      ListView1.Columns.Clear()
      ListView1.Columns.AddRange(colHeaders)
    End Sub
    This ensures that column 1 stays a fixed 75% of the width of the ListView and column 2 is justified to the remaining space on the right when the Form is resized.

    Cheers,

    Ian

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2013
    Posts
    42

    Re: How can I "Float" a listview column header to the right?

    @ Ian

    Using the form re-size event worked perfectly. I ran into a small hiccup with col1. As the form widened col1 did not expand to reveal the rest of it's value. I corrected this but setting it's width to -1 (width of widest value). This caused it to look goofy however when it's value was 0. To correct this I just added an If statement stating that when listview items count is equal to 0/nothing that the columns be set to a numeric value rather than -1 & -2.

    Code:
        Private Sub AddLVHeaders()
            Dim colHeaders() As ColumnHeader = {New ColumnHeader With {.Text = "File Name", .Width = -1, .TextAlign = HorizontalAlignment.Left},
                                                New ColumnHeader With {.Text = "File Type", .Width = -2, .TextAlign = HorizontalAlignment.Right}}
            ListView1.Columns.Clear()
            ListView1.Columns.AddRange(colHeaders)
    
            If ListView1.Items.Count = 0 Then
                Dim colHeaders_alt() As ColumnHeader = {New ColumnHeader With {.Text = "File Name", .Width = 306, .TextAlign = HorizontalAlignment.Left},
                                        New ColumnHeader With {.Text = "File Type", .Width = -2, .TextAlign = HorizontalAlignment.Right}}
                ListView1.Columns.Clear()
                ListView1.Columns.AddRange(colHeaders_alt)
            End If
    
        End Sub
        Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
            AddLVHeaders()
        End Sub
    Thank you again Ian

    Resolved

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width