-
Jun 20th, 2013, 07:18 AM
#1
Thread Starter
Member
Last edited by nabspot; Jun 20th, 2013 at 07:19 AM.
Reason: Adding Images
-
Jun 20th, 2013, 07:57 AM
#2
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
-
Jun 20th, 2013, 09:03 AM
#3
Thread Starter
Member
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)
-
Jun 20th, 2013, 09:17 AM
#4
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
-
Jun 20th, 2013, 09:28 AM
#5
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
-
Jun 20th, 2013, 10:35 AM
#6
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|