Search This Blog

Monday, March 22, 2010

Parse items to list box

Simple little sub I wrote to parse multiple items from a text box or string and add them to a listbox.

I thought you guys could get some use out of it... So here goes:

Somewhere in your form you need this code...


Public Sub l4nParse(ByVal strSource, strStart, strEnd As String, lSt As ListBox)
Dim lonPos, lonEnd As Long
Dim strParsed As String

'Make sure that the start of what we want to parse is really there.
lonPos = InStr(1, strSource, strStart, vbTextCompare)
'Do the following loop for as long as the starting string is in strSource
Do While lonPos > 0
lonPos = lonPos + Len(strStart)
lonEnd = InStr(lonPos, strSource, strEnd, vbTextCompare)
If lonEnd > 0 Then
strParsed = Mid$(strSource, lonPos, lonEnd - lonPos)
strParsed = Replace(strParsed, vbCrLf, vbNullString)
lSt.AddItem (strParsed)
lonPos = InStr(lonEnd, strSource, strStart, vbTextCompare)
Else
lonPos = lonEnd
End If
Loop
End Sub

And thats it. Now all you need to do is specify a source string, the string before what you want parsed, and the string after what you want parsed.

You would call l4nParse like this:


l4nparse strSource, strStart, strEnd, lstbox

And thats it.

How to use a ListView for the Complete Noob.

How to use a ListView for the Complete Noob.

This tutorial will go through two stages. The first stage will show how to get the listview onto your form and the different ways to set up your listview. The second stage will show how to add basic functionality to your listview.

Stage 1: The Setup

To get the ListView control onto your control toolbar, you must go to Project -> Components and select "Microsoft Windows Common Controls 6.0 (SP6)"



It will then add a bunch of controls to your toolbox.



After you add one to your form, right click on it and go to Properties so you can setup your ListView



These are the settings i'm going to use for this tutorial:



lvwReport is the standard view that everyone recognizes as a ListView. The other views you do see every day, you just don't recognize them. Your desktop is a ListView, when you open your folders and view the icons -- those are listviews.

putting LabelEdit to manual will stop users from being able to change the text on the ListView.

FullRowSelect makes it so the selected item highlights along the whole listview instead of just one column.

you should play with the different properties so you know what they all do.

Now you need to set up your columns. Still in the properties, click the Columns tab.



Add some columns and set the width and alignment to your liking. You can run your project to see what the columns look like.



Stage 2: The Code

When adding an item to a listview, you need to get the count of the listview and use it as the index. If you don't, it will add your item to the top of the listview instead of the bottom. here is the code to add text to your columns:



Static x As Integer
Dim i As Integer
With ListView1
'get how many items are in the listview so we
'can add items to the bottom, instead of the top
i = .ListItems.Count
'add text to our leftmost column
.ListItems.Add i + 1, , "Item " & x
'add text to the rest of the columns
.ListItems(i + 1).SubItems(1) = "Stashio " & x
.ListItems(i + 1).SubItems(2) = "Tacosalad " & x
End With

x = x + 1

Let me step through the code here.

Static x As Integer
Dim i As Integer
Just declaring some variables. Static is just a way of declaring a static variable locally instead of globally. it's more efficient in memory.

With ListView1
..
End With
is just for cleaner code, stops me from typing "ListView1" a bunch of times.

i = .ListItems.Count
this gets the number of items in the listview and stores it into variable i.

.ListItems.Add i + 1, , "Item " & x
ListView1.ListItems.Add [index], [key], [text]

we are adding text at index i + 1 (which would be 1 plus our Count)
key we don't need in this situation, but you are able to assign each line a unique key.
text is what we want to add.. I just threw in a string and an incrementing variable.

a few more pieces of code:

how to remove an item by index:

ListView1.ListItems.Remove [Index to remove]

Remove selected item:

With ListView1
If .ListItems.Count > 0 Then
.ListItems.Remove .SelectedItem.Index
End If
End With

how to clear a listview:

ListView1.ListItems.Clear



Thus ends the beginner tutorial for the ListView.

How to get all characters

If you ever need to get all the characters for you program. Say you're making an ascii program and need special characters. This is very simple to do.

Button
Listbox

In the button add the following code.


For i = 33 To 255
List1.AddItem (Chr$(i))
Next i
i = Empty

That's it. Enjoy

For no spaces you could use these.

33 To 42 ' Basic symbols
1 To 27 ' Special
43 To 127 'Basic Letters, Symbols
128 To 255 ' Special