Windows Develop Bookmark and Share   
 index > Windows Forms General > Retrieving Key from ListView using default Listview
 

Retrieving Key from ListView using default Listview

Hello everyone,
Newbie here... Can someone please explain how to retrieve the KEY from a ListView.
I have a form with Tabs on it, and in each Tab is ListView.
Each Listview has numerous objects. Using Icons as the display method.
I load the ListView, for instance in the 1st ListView, with 15 Items. The Key is assigned as:
lstvwMain.BeginUpdate()
With lstvwMain.Items
.Add("A", "Windows Update", 0) '0
imglstMain.Images.Keys.Add("A")
.Add("B", "Microsoft Updates", 1) '1
.Add("C", "Security Center", 2) '2
.Add("D", "System Restore", 3) '3
.Add("E", "Disk Cleanup", 4) '4
.Add("F", "Disk Defragmenter", 5) '5
.Add("G", "Event Viewer", 6) '6
.Add("H", "Add/Remove Programs", 7) '7
.Add("I", "Backup System", 8) '8
.Add("J", "Local Security", 9) '9
.Add("K", "Local Users and Groups", 10) '10
.Add("L", "User Accounts", 11) '11
.Add("M", "Group Policy", 12) '12
.Add("N", "Resultant Set of Policy", 13) '13
.Add("O", "System Properties", 14) '14
End With
lstvwMain.EndUpdate()

as you can see the Key is a Letter.
Now I've been able to retrieve the Key on a Click, however, on a MouseMove, no such luck.

Private Sub lstvwMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstvwMain.MouseMove
' Dim realIndex As Integer
Dim item = lstvwMain.GetItemAt(e.X, e.Y)
Dim whatever As Integer
Try
whatever = item.Index
ToolTip1.SetToolTip(lstvwMain, AllTips(whatever))
'For realIndex = 65 To 79 ' cause the key is A to O
' If lstvwMain.SelectedItems.ContainsKey(Chr(realIndex)) Then
' realIndex = Asc(Chr(realIndex)) - 65
' ToolTip1.SetToolTip(lstvwMain, AllTips(realIndex))
' Exit For
' End If
'Next
Catch ex As Exception
ToolTip1.SetToolTip(lstvwMain, vbNullString)
End Try
End Sub

If i can get to the key then i can display the tooltip I've made. I need to have the ability to retrieve the Key no matter what way the Icons are displayed. I can get the Index, as you can see, but no matter what I've tried, no luck on getting the Key.
Any ideas...
Marshall Neill
Herbie643  Wednesday, November 21, 2007 11:17 PM

Hi,base on my understanding,you want to get the select item value ,don't you?

I think you can do as this

Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Me.Text = Me.ListView1.SelectedItems(0).Text.ToString
End Sub

Hope it helps

Best Regards

Gavin

Gavin Jin - MSFT  Monday, November 26, 2007 8:41 AM
Gavin, etal...
I found how to do it... Finally... LOL...

Private Sub lstvwSysMgmt_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles lstvwSysMgmt.MouseMove
lstvwSysMgmt.Items(whatever).Selected = False
Dim item = lstvwSysMgmt.GetItemAt(e.X, e.Y)
Try
whatever = item.Index
lstvwSysMgmt.Items(whatever).Selected = True
If Not tipShown Then
For Me.realIndex = 15 To 29
If lstvwSysMgmt.SelectedItems.ContainsKey(realIndex) Then
ToolTip1.ToolTipIcon = ToolTipIcon.Info
ToolTip1.SetToolTip(lstvwSysMgmt, AllTips(realIndex))
tipShown = True
Exit For
End If
Next
End If
Catch ex As Exception
ToolTip1.SetToolTip(lstvwSysMgmt, vbNullString) : tipShown = False
End Try
End Sub

This way the ListView can be sorted or not... I have specialized Tooltips and needed to be able to get to them whether the list was sorted or not as the way to access them was not by the index, as it changes if you sort, but by the key i added when the ListView was created... The key line was:

lstvwSysMgmt.Items(whatever).Selected = True

From there is it was cake...
Thanks again for everyones help..
Herbie643  Monday, November 26, 2007 6:35 PM
Ok. I see a lot of people have viewed my question but as of yet no one had provided a solution, so I have another question:
Can someome show how to programatically Select a ListView item?
If you can then my problem is solved.
Thanks in advance.
Marshall Neill
Herbie643  Thursday, November 22, 2007 3:18 PM

Hi,base on my understanding,you want to get the select item value ,don't you?

I think you can do as this

Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Me.Text = Me.ListView1.SelectedItems(0).Text.ToString
End Sub

Hope it helps

Best Regards

Gavin

Gavin Jin - MSFT  Monday, November 26, 2007 8:41 AM

you can assign key to tag of item as item.Tag = "K"; In SelectedIndexChanged event you can reach key like

private void listView1_SelectedIndexChanged(.....)

{

MessageBox.Show( listView1.SelectedItems[0].ToString());

}

( Sorry, i use c#)

if you want display or reach to key on mouse move (instead of select item) use listview hittestinfo

private void lvData_MouseMove(object sender, MouseEventArgs e)

{

ListViewHitTestInfo info = lvData.HitTest(e.X, e.Y);

if (info.Item == null)

return;

MessageBox.Show(info.Item.Text);

}

Koray Samsun  Monday, November 26, 2007 9:41 AM
Gavin, etal...
I found how to do it... Finally... LOL...

Private Sub lstvwSysMgmt_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles lstvwSysMgmt.MouseMove
lstvwSysMgmt.Items(whatever).Selected = False
Dim item = lstvwSysMgmt.GetItemAt(e.X, e.Y)
Try
whatever = item.Index
lstvwSysMgmt.Items(whatever).Selected = True
If Not tipShown Then
For Me.realIndex = 15 To 29
If lstvwSysMgmt.SelectedItems.ContainsKey(realIndex) Then
ToolTip1.ToolTipIcon = ToolTipIcon.Info
ToolTip1.SetToolTip(lstvwSysMgmt, AllTips(realIndex))
tipShown = True
Exit For
End If
Next
End If
Catch ex As Exception
ToolTip1.SetToolTip(lstvwSysMgmt, vbNullString) : tipShown = False
End Try
End Sub

This way the ListView can be sorted or not... I have specialized Tooltips and needed to be able to get to them whether the list was sorted or not as the way to access them was not by the index, as it changes if you sort, but by the key i added when the ListView was created... The key line was:

lstvwSysMgmt.Items(whatever).Selected = True

From there is it was cake...
Thanks again for everyones help..
Herbie643  Monday, November 26, 2007 6:35 PM

You can use google to search for other answers

Custom Search

More Threads

• System.UriFormatException Problem
• construct mdi appear only once
• Using RSAEncryption
• Compatibility.VB6.FileListBox Question
• ComboBox Changes in .NET 2.0
• How to make a multi lingual Windows Forms application in C#
• Firing the help requested event from a menu item?
• Create a new instance of a User Control each time the tab is active
• Drawing upon a Windows Form
• Smart Client Vs Windows Forms ?