Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

How Can You Arrange Items Alphabetically In A Listview?

That is, if the first column is arranged alphabetically, all the data in the other columns will be arranged properly too..

Apple Eat Tree
Dog Play Ball
Cat Meow Food

so when its arranged alphabetically, it appears as

Apple Eat Tree
Cat Meow Food
Dog Play Ball

View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Re-arrange An Items In LISTVIEW?

The app have a LISTVIEW with multi-columns in it, I want to let the user move the selected item up and down to whatever position they want to go.

Preferred to create two "UP" and "DOWN" button on the form, so the user just hightlighted the item in the listview then move it up and down.

No need to handle select multiple-items

Any simple will be great.


Simplest Way To Arrange My Listview Items
i have this listview that gives me weird Icons displays

try to Populate it

and then switch to Icon View

you will notice that there is a space on the left side of the Icons, there shouldnt be a space there, and the Icons should be on that particular space since i arranged the listview with autoleft

attached file is a sample program that is similar to my original program

i just wanted it to look like windows explorer

btw, im using VB6 sp6

Simple Way To Arrange My Listview Items
i have a listview that can display in 4 types of view, list and report view is good, but with smallicons and Icons i sometimes get these results:

as you can see, this is with smallicons, some 2nd column items overlap the 1st column, which is okay, what's not good is when 1st column items overlap 2nd column items

this problem I also encounter when smallicons and Icons view, notice the space on the left side (circled red).

thing is, sometimes i get desirable views, labels wrap, icons aligned, and no overlappings when changing views and or changing the items in the list

does anybody have work-around for this?
all i wanted is something that looks like windows explorer...

Arrange Worksheets Alphabetically

I have a workbook with 40+ worksheets. The names of the worksheets are read into a ComboBox using:

For Each sheet In ActiveWorkbook.Sheets
If sheet.Name <> "Summary Private Equity" Then
SecBox.AddItem sheet.Name

Is it possible to either;
(a) Arrange the sheets alphabetically so that they will appear in alphabetical order in the ComboBox (preferred), or,
(b) Just list them in alphabetical order in the ComboBox.

Your assistance is greatly appreciated!

Help...How Can I Arrange My Records Alphabetically?
Hi everyone,

How can I arrange my records in a MSACCESS DataBase in ascending order by using codes in VB? I need to display records in ascending order on a datagrid. Hope you can help me.


Sorting Items Alphabetically In Textbox
Does anyone have a code to sort items alphabetically in a textbox
cause a textbox does not have the property "Sorted-->True" like
a listbox. Thanks.

Arrange ListBox Items
I have a list box with a lot of names; I just want it when I click on command1 to arrange the names inside by alphabetical order….

Arrange ListBox Items!
A VB6 project has 2 Forms, named Form1 & Form2. Both the Forms have a ListBox. When the project loads for the very first time, the ListBox in Form1 is empty. The ListBox in Form2 is populated with items from a text file, named Text.txt, when Form2 gets loaded everytime. All the items in Text.txt are sorted alphabetically from A to Z. Both the Forms have a OK CommandButton as well. Additionally, Form1 has 2 more CommandButtons - the first one when clicked opens Form2 & the second one is for users to remove any item from the ListBox in Form1.

When an item in the ListBox in Form2 is selected & the OK button is clicked, that item gets deleted from this ListBox & this item gets added to the ListBox in Form1. At the same time, a new text file, named Text2.txt, gets created which is populated with all the items existing in the ListBox in Form2.

Conversely when an item is removed from the ListBox in Form1, that item gets added to the ListBox in Form2 (but Form2 doesn't open). When the OK button in Form1 is clicked, a new text file, named Text1.txt, gets created which is populated with all the items existing in the ListBox in Form1.

When the 2 Forms get loaded next time, the Form1 ListBox gets populated with the items existing in Text1.txt & the Form2 ListBox gets populated with the items existing in Text2.txt.

The problem is in arranging the items in the 2 text files, Text1.txt & Text2.txt (& also in the 2 ListBoxes), alphabetically.

Assume that Text.txt has the following items:


I open Form2 (by clicking a CommandButton in Form1). The ListBox in Form2 gets populated with the above-mentioned items. Next I select Denmark from the Form2 ListBox & click the OK button. Denmark gets deleted from the Form2 ListBox & gets added to the Form1 ListBox. At the same time, Text2.txt gets created which is populated with all the items existing in the Form2 ListBox (which are the above-mentioned items excluding Denmark).

Next I again open Form2, select Bahamas from the ListBox & click the OK button. Bahamas gets deleted from the Form2 ListBox & it gets added to the Form1 ListBox. I again open Form2, select India from the ListBox & click the OK button. India gets deleted from the Form2 ListBox & it gets added to the Form1 ListBox.So the Form1 ListBox now looks like this:


But the 3 items in the Form1 ListBox aren't sorted alphabetically from A to Z. So when the OK button is clicked in Form1, then Text1.txt gets created with the above 3 items (in that order). Similarly, when an item in the Form1 ListBox is deleted, that item gets added to the Form2 ListBox but here as well, the items in the Form2 ListBox aren't sorted alphabetically.

Now how do I sort the items in the 2 ListBoxes in an alphabetical manner when they get populated dynamically?

Re-arrange List Items
gd day gents.
This will be hard to explain with my bad english...

Is there any way to "re-arrange" the order of a lists items?
I would like them to be randomly re-arranged..

Sort A Listview But Not Alphabetically
Hello all,
I'm working with a listview. I need to sort the listview in this order. (These are names of data types and they are stored in one of the listview's columns)


As you can see, they are not alphabetical. There are approx. 20,000 to 30,000 items in the listview, so you can understand my need for an efficient algorithm.

I've considered using an enumeration, but apparently REAL is a keyword in VB.

Help greatly appreciated.


Alphabetically Re-organising A ListView :p
How would you alphabetically re-organise the stuff in a listview??

so it's like 0-9 and then A-Z
also taking into respect lowercase and uppercase

Any idea's?

Cheers, sphynx

Listview Problems With Arrange And View

I have couple of issues with my listview control.

a) when I set arrange=lvwautoleft, I expect the coloumn headers to appear vertically on the left hand side. That does not happen.

b) When I set view = lvwicon, lvwsmallicon, or lvwlist, I see nothing in the listview control, only the lvwreport works (I see the image and the text in the colomn header). I have set up the image icon and all the setting etc.



Is It Possible To Re-arrange The Position Of Colomn Headers In Listview?
Can we do that at run time? If not with listview, is there any way I can make it work?

Also I would be interested in knowing how a listview performs with 50000 records. I know there is FlexGrid control but I don't really like it's format. Any ideas?


Combine 2 Listview Items Into One Listview
I have 2 listviews that have a different listitems.


LV 1 LV 2
1. A 1. B
2. C 2. D
3. E 3. F

Is there a way to combine the 2 to another listview so that they all look like this with out doubles of matching items?


LV 3

1. A
2. C
3. E
4. D
5. E
6. F

Combine 2 Listview Items Into One Listview
I have 2 listviews that have a different listitems.

LV 1 l LV 2 l
l l
1. A l 1. B l
2. C l 2. D l
3. E l 3. F l

Is there a way to combine the 2 to another listview so that they all look like this with out doubles of matching items?

LV 1 l
1. A l
2. C l
3. E l
4. D l
5. E l
6. F l

Msg All Listview Items
hi i am wanting to display a message box for each item in a listview. the code i currently have is:

Public Sub msgALL()
Dim i As Integer
For i = 0 To lstSYS.ListItems.Count - 1
MsgBox lstSYS.SelectedItem
Next i
End Sub

this only displays the first item as the code states to display the selected item. how can i make it cycle through?


Getting Listview Items

i would like to retrieve the item text selected (by double clicking) in a listview.

How can I do?



Listview Add Items
Hi, I have a Listview in my project and I would like to know how can I add all the text of the listview column to specific field name in my database Access. What Can I do? This is the code to add info to my Database:

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = OpenDatabase(App.Path & "Base de DatosPros2003.mdb")
With db Set rs = .OpenRecordset("Tratamientos Pacientes")
End With

On Error Resume Next
With rs
!ID = Text2.Text
!Precio = txtTotalAmount.Text
!Usuario = txtCheckedBy.Text
!Codigo = txtProdCode.Text
!Tratamiento = Listview1.WHAT CODE I NEED TO ADD HERE?
!Descuento = txtDiscount.Text
!Tratamiento = txtDescription.Text
!Cantidad_Pagada = txtCash.Text
!Numero_Cheque = txtCheckNumber.Text
!Banco = txtBank.Text
MsgBox "Congratulations", vbOKOnly + vbDefaultButton1, "Pro 2003"
End With


Same Items In Listview
Hello... I have one problem.

I fill the listview with some standard sql query.

If there are few same names, then they are all filled down.
Now, I want this:

if there are some names that they are exatcly the same I want just first one to be writed down, then the next different name and so on...

Is this possible?

Get ListView Items From Other App's
i need to get Item texts of a ListView control that we know it's hwnd from other applications.
we can easily send LVM_GETITEMTEXT message to the listview object and retrive any item. but i want to get items of a listview in another application and when i call sendmessage in this case, my program fail and close by windows. i think this is because of memory access or something like that. may other applications doesn't have access to the memory buffer of my program.
note that i can get list count (number of items) by sending LVM_GETITEMCOUNT message and it has no problem but i can't get text of items.

here is the basic code to retrive an item in vb (it works if you call it for listview controls that exists on your program and not other windows applications)


--------------------------- Module LVAPI.bas

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Const LVM_FIRST = &H1000 ' ListView messages
Public Const LVM_GETITEMTEXTA = (LVM_FIRST + 45) 'unicode

Public Type LVITEM
mask As Long
iItem As Long
iSubItem As Long
State As Long
stateMask As Long
pszText As Long 'String
cchTextMax As Long
iImage As Long
lParam As Long
iIndent As Long 'for IE>3
End Type

Function GetListViewText(ByVal hwnd As Long, ByVal ItemIndex As Long) As String
' Obtain the name of the specified Item of the list view
On Error Resume Next
Dim objItem As LVITEM
Dim baBuffer(320) As Byte, n As Long
With objItem
.mask = LVIF_TEXT
.iSubItem = 0
.pszText = VarPtr(baBuffer(0))
.cchTextMax = UBound(baBuffer) - 1
End With
n = SendMessage(hwnd, LVM_GETITEMTEXTA, ItemIndex - 1, objItem) ' ItemIndex-1 is because of list is zero-based index
GetListViewText = Left$(StrConv(baBuffer, vbUnicode), n) 'convert from binary byte array to a String
End Function

Function GetListViewCount(ByVal hwnd As Long) As Long
'this simply get number of items
GetListViewCount = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, ByVal 0)
End Function

---------------------- Form1

Private Sub Form_Load()

With ListView1
.View = lvwReport
.ColumnHeaders.Add Text:="Name"
.ListItems.Add Text:="John"
.ListItems.Add Text:="Kharazmi"
.ListItems.Add Text:="ABCD"
End With

Label1.Caption = "N = " & GetListViewCount(ListView1.hwnd)
Label2.Caption = "Item 1 = " & GetListViewText(ListView1.hwnd, 1)
Label3.Caption = "Item 3 = " & GetListViewText(ListView1.hwnd, 3)

'The Problem is that we can't call this way for ListView controls of other applications!

End Sub

Need Help In ListView Items
Hi All
i use this code to add an item to listview

VB Code:
Dim itmX As ListItemSet itmX = ListView1.ListItems.Add(, , "aaaaaaa")    itmX.SubItems(1) = Time    itmX.SubItems(2) = Date

it add new item in the last row
how to add item in order newst item is in first row


Items In A ListView
I have a combobox which acts as a Lookup table, a command button and a ListView. My aim is that when i select an item from the combo, on clicking the command button, the selected item from the combo gets listed in the ListView. The code for filling the combo is :
VB Code:
Dim strSQL As String    'Declare the variables we need                   strSQL = "SELECT movieId, title FROM movieInfo"  Set rsMovie = New ADODB.Recordset         rsMovie.Open strSQL, cnAddMovie, adOpenForwardOnly, adLockReadOnly, adCmdText    With cboTitle        .Clear            Do While Not rsMovie.EOF        .AddItem rsMovie.Fields("title").Value        .ItemData(.NewIndex) = rsMovie.Fields("movieId").Value          rsMovie.MoveNext    Loop      End With   rsMovie.Close  Set rsMovie = Nothing

BUT now when the selected item is listed in the ListView, I want the following code executed.

VB Code:
'strSQL = "SELECT movieInfo.movieId, movieInfo.title, MediaType.StrKey, MediaType.DefRentPricePerDay, MediaType.DefRentPeriodInDays FROM movieInfo INNER JOIN MediaType ON movieInfo.media_catId = MediaType.media_catId WHERE movieId = " & cboTitle.ItemData(cboTitle.ListIndex)  

So that in the ListView, I have columnsheaders showing Movie title, Format, rentPrice etc

How can i do that?

Listview Max Items
Can someone please tell me what is the maximum number of items in the list with Listview?

My program searches a database with thousands of records and reports errors and I would like to be able to list them on the screen so the end user can review.(Printing them out is another story) A listbox runs out at about 32k items.
I would only need 4 colums in the listview.
If anyone has any ideas on what or how I can do this , I am all ears...

Thanks in Advance


Get Listview Items
Is it possible to save all the items in another apps listview?

Listview Items
How do I give items in a listview a tag?

Getting Sum Of Items In Listview......
Ok, I got a response to a question similar to this before, but for some reason, I couldn't even modify the code to work, and kept getting an object required error. Maybe it had something to do with my use of the listview control Anyway, I'll be more specific this time. Here goes.


In the main column is text, and in subitem1 column is a number (i.e 2:30)

What I want to do is on a command1_click() get the sum of all the numbers in listview2 subitem1, and set that = to sum

Then let listview3.listitems.add SUM

Hope that makes sense. Anway since there is a ":" in the number, it must currently be formatted as a string and not an integer right? So, what do I need to do to total up all of these numbers? Any help is greatly appreciated.

Listview Items....
Hey peops, I was wondering if there was anyone out there that could tell me how to disable editing within a listview's columns...What I'm doing is connecting to an Oracle DB and pulling data and then putting that recordset data onto my listview so i can view them like a spreadsheet (obviously). The problem is that when i click on an item and hold down the mouse button long enough, I'm able to edit the first column...I want to disable this ability. Basically i want none of the recordset data to be renamed or changed, but the items can be selected...thanks in advance

Add Items To ListView?
I have the code below which puts two column headers at the top of my ListView control, (Title) and (Description). . .


Private Sub Form_Load()

Me.ListView1.View = lvwReport

Me.ListView1.ColumnHeaders.Add , , "Title", ListView1.Width / 2
Me.ListView1.ColumnHeaders.Add , , "Description", ListView1.Width / 2
End Sub

Can anyone show me the code I can add to this that will add one item in the list view control with the text (Item Title) under the ‘Title’ column and (Item Description) under the ‘Description’ column?


Moving Through Items In A Listview
Am I being a complete , or what? I'm trying to move through the items in a listview, so that I can insert the entries one-by-one in to DB - ie each item in the listview represents a new record in a DB table.

Now, I would have expected (as with all things VB) this to be quite straight forward. Something along the lines:


I know this a bit hopeful, but I can't believe there's no way to incrementally move through all the items.

I've been through MSDN and searched this entire forum for a similar thing but I cant find anything.

I must be missing something really obvious here!

Highlighting Items In Listview...
I have this small application that has a listview wherein it gets populated by database records. My DB table is arranged alphabetically so it will also be arranged in the listview as well. But I'm haveing trouble displaying/highlighting the last item that was entered in the table so the user will have to scroll through the whole listview to see if what they entered did go in since the table is arranged alphabetically. The display type of my listview is in lvwReport by the way. Is there anyway I can do this?

Listview Items To Excel...
I was just wondering, is it possible to export the listview items to excel? If so, would it also be possible to export the selected items only?

ListView Adding Items.
Private Sub Form_Load()

With ListView1
.ColumnHeaders.Add Text:="Server"
.ColumnHeaders.Add Text:="Ram"
.ColumnHeaders.Add Text:="CPU"
.ColumnHeaders.Add Text:="C:"
.ColumnHeaders.Add Text:="Ping"
.View = lvwReport
End With

End Sub
i have this code to give the listview1 5 diffrent columns.

can someone give me the code that when command1 is clicked, it will add a row underneath the columns in this order:


just so i know how to do this.

Swap Items In Listview
Hello I have a scenario here.
I have a Listview and two command butons (up and down)
Initial Listview Items

A text1 text1
B text2 text2
C text3 text3 - Selected

now when the Up button this is what will happen

A text1 text1
C text3 text3 - Selected
B text2 text2

Hope someone here help me.


Search Through ListView Items
pls can anyone show me how to search through listview items

for example - find a specific text in a ListView

Adding N Items Into ListView
I am using following code to add records read from a text file into a ListView object.

Dim S
Dim SP
Dim i
Dim LItem As ListItem

i = 1
Open App.Path & "dataspdates.txt" For Input As #1

Do While Not EOF(1)
Line Input #1, S
SP = Split(S, "+", , vbTextCompare)

Set LItem = ListView1.ListItems.Add(, , SP(0))
LItem.SubItems(1) = SP(1)
i = i + 1
Close #1
My ListView Control has just two columns, and the text file contains records like:

3/10/04+Mr.D Birthday

In the above code, SP will store the splitted line read from the file. The problem is that the above code displays an error "Subscript out of range", at the line starting with "Set LItem".
How to keep adding records to ListView till EOF.

Listview Selected Items
hey i was wondering if it is possible to retrieve a collection of selected items from a listview which has multiselect set to true

(not checked, but selected)

i was hoping i could do something like this ...

dim li as listitem
for each li in listview1.selecteditems
' do something
next li
... but it doesn't appear to be so easy

any hints ?



Please Help {listview Items To Textbox}
I want to add all the data from the first column to a textbox. I was thinking of this:

For i = 1 To ListView1.ListItems.Count
Text1.Text = ListView1.ListItems(i)

But this only gets the last item. So I thought how about if I have i and add 1 to it each time after putting the next item into the textbox and if there is no more data it would stop.

I don't know how to do this, can someone please help?

Listview Sort Items

how can I move Items in a ListView with an up and down button?

Navigate Through ListView Items...
I have a listview control on a form, and I want to be able to navigate through rows with Up/Down buttons, I can't figure out how this is achieved atall, I wish to do similar to "List1.ListIndex = List1.ListIndex + 1" if i was using a listbox control... But i cant find the appropriate commands for a listview control... any help would be greatly appreciated

Searching Listview Items
anyone know how to search listview items?

Listview Non-moveable Items
How do you set the items in a listview box to be non draggable?

Adding Items To A Listview
I have searched thru the forum, just cant find what im looking for. this is the layout of my listview

Fkey | Action

I wish to add items to the fkey column, and also text to the action clolumn.
listview1.ListItems.Add.Text = "blah" <-- i tried that, but that only goes in Fkey...

How would i do this please,


Coloring Listview Items
Could someone simplify this code:

Private Sub Command4_Click()
Dim i As Integer
1) ListView1.SelectedItem.ForeColor = vbRed
For i = 1 To 4
2) lista.ListSubItems.Item(i).ForeColor = vbRed
End Sub

lista is a listview object

It does work but it seems like it should could be easier made.
I´m a little lost with all the subitems, listitems, item, selected, selecteditem etc.

1) just colors the first column in the row.
2) colors the next columns (the subitems).

so the code colors a row, and it works but it looks stupid (I think).

Removing Items From Another ListView
I have 2 Listviews on separate forms.
I can add the checked item from one listview to the other ok.
But i am having trouble removing the item from the 2nd listview when i uncheck it on the 1st listview as i am having trouble determining what the index of the item in the second list view is.

So basically i want to ba able to add and remove items to/from a listview by checking and unchecking a listview on a seperate form.


Print Only 15 Items From Listview.
Aight, I got this:

Dim i As Integer
Close #1
Open (App.Path & "asdf.txt") For Output As #1
For i = 1 To Form1.ChannelList.ListItems.Count
Print #1, Form1.ChannelList.ListItems.Item(i)
Next i
Close #1

That works fine, but I cant figure out how to make it print only 15 items.


Edit Items In Listview
I have a form that has a listview control on it with 3 columns. These columns are being populated from 3 text boxes and a command button.
This part works fine.

But I would like to select an item entered into the listview and have the items and subitems populate the text boxes in the event I make a mistake on entering data. I have tried:

to obtain the item key and populate the text boxes in the reverse order that I populated the listview.
Here is my code for populating the listview from the textboxes:

Private Sub cmdAddExt_Click()
On Error GoTo errhandler

dHNum = txtExtNum.Text
dHPair = txtPairNum.Text
sHName = txtExtName.Text

iHItems = iHItems + 1
lvwHunt.ListItems.Add iHItems, , dHNum
lvwHunt.ListItems(iHItems).SubItems(1) = dHPair
lvwHunt.ListItems(iHItems).SubItems(2) = sHName

dHNum = vbNull
dHPair = vbNull
sHName = vbNull
txtExtNum.Text = ""
txtPairNum.Text = ""
txtExtName.Text = ""

Exit Sub
Debug.Print Err.Number & " " & Err.Description
Resume Next

End Sub

Hope this makes sense, thanks in advance for your assistence.


Selecting Listview Items
I am having trouble moving to the next item in a listview automatically. I am trying to create option buttons and put the text from the listview to the option button to have users select with Name they want. I do not know how to select the next item below or above the selected item in the listview? Any sugestions? Thanks

Editing ListView Sub Items
I have a listview component which is populated and fine. i need to be able to edit a single items subitem in the listview,cant get it sorted?

Any thoughts or ideas on this?

Copyright © 2005-08, All rights reserved