How to use Columns.Contains and Columns.IndexOf

andycee

Member
Joined
Mar 24, 2006
Messages
11
Programming Experience
1-3
Hi,

On a ListView with View type Details, I need to check to see if a specific column exists and at what position it is.

e.g. you can't just use :

colexists=ListView.Columns.Contains("Filename")
colindex=ListView.Columns.IndexOf("Filename")

...as these require a variable of type System.Windows.Forms.ColumnHeader

What is the correct way to use these ?

thanks
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
It seems that i don't get your idea but anyway this is how you can find whether certain column exists and get its index:
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListView1.Columns.Contains(ColumnHeader1) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"It contains column "[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ColumnHeader1.Text.ToString & [/SIZE][SIZE=2][COLOR=#800000]" and it has index "[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ColumnHeader1.Index.ToString())
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]

Regards ;)
 

andycee

Member
Joined
Mar 24, 2006
Messages
11
Programming Experience
1-3
I'm trying to search for a string within a column header collection, but .contains and .indexof expect a ColumnHeader datatype. How to search for a string ?

I thought maybe .containskey and .indexofkey were maybe the way to do it, but this does not work either, for example :


ListView.Columns.Add("Filename")
ListView.Columns.Add("Folder")

Dim colm As ListView.ColumnHeaderCollection = ListView.Columns

foldercolumnexists=colm.ContainsKey("Folder")
folderindex=colm.IndexOfKey("Folder")


thanks!

confused
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Althrough i am still not positive about the purpose i will show you couple ways more how to search for a string within column header collection

1st way:
VB.NET:
[SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] col [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ColumnHeader [/SIZE][SIZE=2][COLOR=#0000ff]In [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListView1.Columns
[/SIZE][SIZE=2][COLOR=#0000ff]  If[/COLOR][/SIZE][SIZE=2] col.Text = [/SIZE][SIZE=2][COLOR=#800000]"test" [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]     MsgBox([/SIZE][SIZE=2][COLOR=#800000]"found"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]

and 2nd way:
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListView1.Columns.Count - 1
[/SIZE][SIZE=2][COLOR=#0000ff]   If[/COLOR][/SIZE][SIZE=2] ListView1.Columns(i).Text = [/SIZE][SIZE=2][COLOR=#800000]"test" [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]      MsgBox([/SIZE][SIZE=2][COLOR=#800000]"found"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]   End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]

HTH
Regards ;)
 

andycee

Member
Joined
Mar 24, 2006
Messages
11
Programming Experience
1-3
Thanks. Just to make it exactly clear, I'm reading data from a text file and need to place it in the correct columns in a ListView.

I've been using the raw methods you mentioned, but wanted to try and use some of the integrated search functions. I mean, thats what they're there for !!

cheers
 

andycee

Member
Joined
Mar 24, 2006
Messages
11
Programming Experience
1-3
No worries, cheers !

I'll need to do this sort of search a few times, so as I can't seem to get .indexof to work, I was going to use a function that I can send collections to (either ColumnHeaderCollection or ListViewItemCollection) and search for a string match in the header/item :

Private Function FindIndex(ByVal itm As String, ByVal list As Collection)
Dim i As Integer = 0
Do While (i < list.Count) And (list.Item(i).ToString <> itm)
i = i + 1
Loop
If list.Item(i).ToString = itm Then
FindIndex = i
Else
FindIndex = -1
End If
End Function


Problem is, I can't call this with list as a ColumnHeaderCollection:

"System.Windows.Forms.ListView.ColumnHeaderCollection' cannot be converted to 'Microsoft.VisualBasic.Collection'."

Grrrr.... I would have thought that ColumnHeaderCollection would have been a subset of Collection.

Any ideas on this route ?
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
to get index just go like this:
VB.NET:
[SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] col [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ColumnHeader [/SIZE][SIZE=2][COLOR=#0000ff]In [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListView1.Columns
[/SIZE][SIZE=2][COLOR=#0000ff]   If[/COLOR][/SIZE][SIZE=2] col.Text = [/SIZE][SIZE=2][COLOR=#800000]"test" [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]      MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"index of this column is: "[/COLOR][/SIZE][SIZE=2] & col.Index.ToString, col.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
[/SIZE][SIZE=2][COLOR=#0000ff]   End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]

Regards ;)
 
Top Bottom