How to add link SubItem to Details ListView in C# and VB.NET?
Posted by Admin L in .NET Programming on 26-12-2021. Tags: add link to listview, simulate link in listview
Author: Nosa Lee
Original Address: https://www.seeksunslowly.com/how-to-add-link-subitem-to-details-listview-in-c-and-vb-net
To reprint this article, please indicate the source, thank you.
_____________________________________
Do you have such a requirement: for making the concise interface, you don’t want to use context menu or separate button to manipulate ListView data (assuming ListView is named lv, and lv.View = View.Details, the same below), instead it is directly adding a link to the data rows for operation? Like this:
Here is the perfect method developed today by me (VB.NET code, C# can be directly referenced):
Step 1: Let SubItems can use the different style from ListViewItem, and set the link style of SubItems. Code:
For Each lvi As ListViewItem In lv.Items
lvi.UseItemStyleForSubItems = False
lvi.SubItems(1).ForeColor = Color.Blue
lvi.SubItems(1).Font = New Font(lvi.Font.Name, lvi.Font.Size, FontStyle.Underline)
Next
Step 2: Set the cursor when move mouse on the link. Code:
Private Sub lv_MouseMove(sender As Object, e As MouseEventArgs) Handles lv.MouseMove
Dim lvhti As ListViewHitTestInfo = lv.HitTest(e.Location)
If lvhti.SubItem Is Nothing Then
lv.Cursor = Cursors.Default
Else
If lvhti.SubItem.Text = “Remove” Then lv.Cursor = Cursors.Hand Else lv.Cursor = Cursors.Default
End If
End Sub
Step 3: Respond the Click event of “links”. Code:
Private Sub lv_MouseUp(sender As Object, e As MouseEventArgs) Handles lv.MouseUp
Dim lvhti As ListViewHitTestInfo = lv.HitTest(e.Location)
If lvhti.SubItem IsNot Nothing Then If lvhti.SubItem.Text = “Remove” Then MsgBox(lvhti.Item.Text) ‘ Displaying ListViewItem.Text here just for test, means you can get the data as you want!
End Sub
So far, regardless of appearance or function, above code has completely realized adding “link” SubItems to ListView data grid.
best effort