英文:
LViewItem of Windows.Forms select with mouseclick
问题
在ListView中,有3列。第一列是ListViewItem,它有2个ListViewSubitems。涉及它的程序行如下。
Public Class Form1
Friend Shared WithEvents LView As ListView = New ListView
Shared Sub ShowTable(codes As Byte())
LView.View = View.Details
LView.Width = 800
LView.Height = 300
LView.FullRowSelect = False
LView.GridLines = True
LView.Columns.Add("Id1", 200) '这是ListViewItem
LView.Columns.Add("Id2", 230) '这和下一行是ListViewSubitems
LView.Columns.Add("Id3", 345)
'这里是加载表格项的部分
DisplayForm.Controls.Add(LView) 'DisplayForm是窗体的名称
LView.Show()
End Sub
在表格出现后,想要通过鼠标单击来选择其中的项。这会调用MouseClick处理程序,以下是代码:
Private Shared Sub LView_MouseClick(sender As Object, e As MouseEventArgs) Handles LView.MouseClick
If e.Button.Equals(MouseButtons.Right) Then
DelSelected(e) '这个子程序定义了被单击的行
Else
MessageBox.Show("这是左键单击", "单击消息", MessageBoxButtons.OKCancel)
Exit Sub
End If
End Sub
End Class
只有在单击ListViewItem时才会触发MouseClick事件。如果单击了SubItems,则不会发生任何操作,即使它们在ListView上。如果要求整行点击时触发事件子程序,但ListViewSubitems仍然禁用以进行任何涉及其值的活动,那么鼠标单击只会调用处理程序函数以定义单击了哪个元素。因此,将FullRowSelect属性设置为True不太好。在这种情况下,整行会被着色,并且单击子项(而不是子项本身)会返回父ListItem,因此无法定义单击了哪个子项。也许通过单击的坐标可以通过大量计算来定义,但这会非常耗费资源。
英文:
In a ListView there are 3 columns. First is the ListViewItem, which has 2 ListViewSubitems. The lines of the program which refers to it are below.
Public Class Form1
Friend Shared WithEvents LView As ListView = New ListView
Shared Sub ShowTable(codes As Byte())
LView.View = View.Details
LView.Width = 800
LView.Height = 300
LView.FullRowSelect = False
LView.GridLines = True
LView.Columns.Add("Id1", 200) 'This is ListViewItem
LView.Columns.Add("Id2", 230) 'This and the next line are the ListViewSubitems
LView.Columns.Add("Id3", 345)
'Here is the loading of the items of the table
DisplayForm.Controls.Add(LView) 'DisplayForm is the Form's name
LView.Show()
End Sub
After appearing the table, want to select an item of it with the mouse click on it. This invokes the MouseClick handler, code below
Private Shared Sub LView_MouseClick(sender As Object, e As MouseEventArgs) Handles LView.MouseClick
If e.Button.Equals(MouseButtons.Right) Then
DelSelected(e) 'This sub define the clicked line
Else
MessageBox.Show("This is left click", "Click message", MessageBoxButtons.OKCancel)
Exit Sub
End If
End Sub
End Class
MouseClick event is invoked only, when the ListViewItem is clicked. If SubItems clicked nothing happen, even they are on the ListView.
What would be required, that event sub is invoked on the click of the whole line, but the ListViewSubitems remain disabled for any activities implied their values, the mouse click only invoke the handler function to define which element was clicked. Therefore not good the FullRowSelect property set to True. In that case the whole line is colored, and clicking the subitem (instead itself) returns the parent ListItem, so cannot be defined which subitem was clicked. Maybe by the coordinates of the click can define it with a lot of calculation, but very consumable.
答案1
得分: 0
以下是您要翻译的内容:
答案很简短:ListView
不能在任何地方点击,而只能在属性 FullRowSelect=False
时点击 "ListViewItem"。
此外,ListViewSubitem
没有 Index
、Selected
、Focused
属性。
为了解决这个问题,我在处理 ListView
数据的类中应用了以下更改。
在 ShowTable 子程序中的第一处更改为
LView.FullRowSelect = False
改为
LView.FullRowSelect = True
在声明部分中
Shared oldcolor1 As Color = Nothing, oldcolor2 As Color = Nothing
Shared selitemindex As Integer = 0, selsubitemindex As Integer = 0
Shared oldcolor1 As Color = Nothing, oldcolor2 As Color = Nothing
Shared selitemindex As Integer = 0, selsubitemindex As Integer = 0
在子程序中
Private Shared Sub ItemSelected(e As MouseEventArgs)
Dim coordx, coordy, buttpress As Integer
Dim rowitem As ListViewItem
Dim info As ListViewHitTestInfo
If Not oldcolor1.IsEmpty Then
With LView.Items(selitemindex).SubItems(selsubitemindex)
.BackColor = oldcolor1
.ForeColor = oldcolor2
End With
End If
coordx = e.Location.X
coordy = e.Location.Y
rowitem = LView.GetItemAt(coordx, coordy)
info = LView.HitTest(coordx, coordy)
selitemindex = info.Item.Index
selsubitemindex = info.Item.SubItems.IndexOf(info.SubItem)
With LView.Items(selitemindex)
.Focused = False
.Selected = False
oldcolor1 = .BackColor
oldcolor2 = .ForeColor
.UseItemStyleForSubItems = False
.SubItems(selsubitemindex).BackColor = Color.DodgerBlue
.SubItems(selsubitemindex).ForeColor = Color.White
End With
End Sub
只针对扩展的变体,可以使用 ListView.FocusedItem
属性,当然代码必须相应地调整。随您喜欢。
必须知道,在这种情况下,ListViewSubItem 既不具有 "Focused" 属性,也没有 "Selected" 属性。
这段代码的一部分基于问题评论中的链接表。在那里可以找到关于这个主题的其他信息。
所以,这就是我解决这个问题的方法。
英文:
The short answer is: A ListView
cannot be clicked anywhere rather only "ListViewItem"-s if the property FullRowSelect=False
.
Furthermore ListViewSubitem
hasn't Index
, Selected
, Focused
properties.
To resolve this issue i applied this in my class where the ListView
datas are processed.
First change in the ShowTable sub the line
LView.FullRowSelect = False
to
LView.FullRowSelect = True
In the declaration section
Shared oldcolor1 As Color = Nothing, oldcolor2 As Color = Nothing
Shared selitemindex As Integer = 0, selsubitemindex As Integer = 0
Shared oldcolor1 As Color = Nothing, oldcolor2 As Color = Nothing
Shared selitemindex As Integer = 0, selsubitemindex As Integer = 0
In the Sub procedure
Private Shared Sub ItemSelected(e As MouseEventArgs)
Dim coordx, coordy, buttpress As Integer
Dim rowitem As ListViewItem
Dim info As ListViewHitTestInfo
If Not oldcolor1.IsEmpty Then
With LView.Items(selitemindex).SubItems(selsubitemindex)
.BackColor = oldcolor1
.ForeColor = oldcolor2
End With
End If
coordx = e.Location.X
coordy = e.Location.Y
rowitem = LView.GetItemAt(coordx, coordy)
info = LView.HitTest(coordx, coordy)
selitemindex = info.Item.Index
selsubitemindex = info.Item.SubItems.IndexOf(info.SubItem)
With LView.Items(selitemindex)
.Focused = False
.Selected = False
oldcolor1 = .BackColor
oldcolor2 = .ForeColor
.UseItemStyleForSubItems = False
.SubItems(selsubitemindex).BackColor = Color.DodgerBlue
.SubItems(selsubitemindex).ForeColor = Color.White
End With
End Sub
Only for extending varieties, instead of HitTest
method, ListView.FocusedItem
property can be used, of course code must adjust to it. As you like.
Has to know that ListViewSubItem in this case neither Focused
nor Selected
.
Part of this code based on the linked sheets in the comments of the question. There additional infos can be found on this subject.
So, this how i got through it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论