| Add Columns and Titles Effortlessly to a ListView Control Add all of your titles to a ListView and size their column-widths with a single call to one procedure. Documentation, troubleshooting notes, and the author's notes are contained within the code-sample. Sample Call: AddColumnsToViewBox "First", 30, "Second", 70 'Add columns to a ListView box. 'Copyright released to the receiver. 'WF Matthews, Leland, NC (USA) 'Format is: ' 'AddColumnsToViewBox "First Title", PercentWidth, _ "Second Title", PercentWidth, ... 'NOTE: You must include this procedure within a form ' And the listview control is named ListView1 ' by Default. It's easy enough to adjust the code ' for your own needs (e.g., change ListView1 to the ' the name of your List View, or rewrite the function ' to accept the ListView control as a parameter 'Each PercentWidth value, (an integer), is the percent of the 'ListView box's width that is to be assigned to the column. 'If the total of the PercentWidth values exceed 100, the box will 'have a horizontal scrollbar. 'If the total is less than 100, an unusable section of the 'title area will be present in the ListView box. If your 'data includes the need to scroll vertically, you might size 'your columns in such a way as to leave room for the vertical 'scrollbar. Leaving room'for a scrollbar isn't mandatory. 'Author's Notes: 'This code assumes zero-based array-indexing. If you're 'using 1-based indexing, make the necessary adjustments 'to and within the For-loop and to the parameter-check code. ' 'While the code works as-is, including its error-handling, 'you should adapt its error 'handling to your own requirements. ' 'After debugging, if all of your calls to this procedure are 'hard-coded and aren't under software-control, the 'parameter-check may be removed. However, if your code 'constructs an indeterminate number of titles for the box, 'you may wish to incorporate the parameter-check into your 'error-handling scheme. ' 'The "80" is an "adjustment" factor. When using the VB5 ListBox, 'change the value from "80" to 850. Without it, specifying 100 'as the total for the various widths always includes a scrollbar 'in the ListView box. That's not nice. 'Troubleshooting Problems With Input-Parameters '------------------------------------------------ 'Input Parameters Result '----------------------------------------------- ' ' "First", 30 "First" occupies 30% of the ' box's width. The remainder is ' an inactive (blank) title bar. ' ' "First", 30, "Second" After display of the parameter- ' check error message,the ' display is the same as the ' previous sample. ' ' "First", "Second" "Type Mismatch" error. "Second" ' is a string and it should ' have been an integer. ' ' 40, "First" "Type Mismatch" error. The ' parameters are reversed. An ' integer should be first, ' followed by a string. ' ' "First", 40, 40 See second example. 'When presented with an odd-number of titles and widths, the 'procedure displays each pair and entirely omits the last 'parameter. '----------------------------------------------------------- Public Sub AddColumnsToViewBox(ParamArray TitlesAndWidths()) Dim i, Width, ParameterCount, Msg On Error GoTo ErrorHandler ListView1.View = lvwReport 'Input-parameter check. (See Author's Note) ParameterCount = UBound(TitlesAndWidths) + 1 If ParameterCount / 2 <> CInt(ParameterCount / 2) Then Msg = "Input-parameter count is incorrect. You must " & _ "specify an equal number of titles and widths." MsgBox Msg, , "Prameter-Count Error" End If Width = ListView1.Width - 80 With ListView1.ColumnHeaders .Clear For i = 0 To UBound(TitlesAndWidths) - 1 Step 2 .Add , , TitlesAndWidths(i), (TitlesAndWidths(i + 1) _ * Width) / 100 Next i End With Exit Sub ErrorHandler: MsgBox Err.Description & "." End Sub |
Add Columns and Titles Effortlessly to a ListView |
Express News India | Freelance ecommerce web development India