我如何CheckOnClick在的CheckedListBox但只有当通过复选框? [英] How do I CheckOnClick in a CheckedListbox but only when over the checkbox?
问题描述
我有一个的CheckedListBox。我想是能够选择的项目,当我点击文本,但选中/取消他们当我点击左边的复选框区域。如果我设置CheckOnClick那么项目选中和未选中时,我点击,连上的文字,所以这是没有好。但是,如果我明确CheckOnClick然后我必须单击两次检查,并取消勾选。
I have a CheckedListBox. I would like to be able to select items when I click on the text but check/uncheck them when I click on the checkbox area on the left. If I set CheckOnClick then the items are checked and unchecked whenever I click, even on the text, so that's no good. But if I clear CheckOnClick then I have to click twice to check and uncheck.
我首先想到的是处理鼠标点击或MouseDown事件中,并呼吁IndexFromPoint找出点击哪一行。那我就只是猜测,该复选框是在左边,从x =位置,从0到,比方说,ItemRectangle.Height。根据从左边的距离,我可以选择或检查/取消选中。
My first thought is to handle MouseClick or MouseDown events and call IndexFromPoint to find out which row is clicked. Then I would just guess that the checkbox is on the left, from x=position from 0 to, say, ItemRectangle.Height. Depending on the distance from the left, I could select or check/uncheck.
现在的问题是,是否有更好的方法,以确定是否将鼠标悬停在复选框或在文本上。不同风格可以有不同大小的复选框,并可能将它们放在左,右,等等......
The question is whether there is a better way to determine if the mouse is over the checkbox or over the text. Different styles could have different sized checkboxes and might put them on the left, right, etc...
推荐答案
我写了这一点,它似乎工作,感谢SLaks。要使用此,CheckOnClick必须真实,CheckInCheckbox为好。从的CheckedListBox继承。
I wrote this and it seems to work, thanks to SLaks. To use this, CheckOnClick must be true and CheckInCheckbox as well. Inherit from CheckedListbox.
这样做是为了找出其中的复选框,如果点击的复选框之外,设置的CheckState到它的反面。后来,收到基类中的鼠标点击时的CheckedListBox,它将再次改变复选框状态恢复到它是什么。
The idea is to figure out where the checkbox is and, if the click is outside of the checkbox, to set the checkstate to its opposite. Later, when the mouseclick is received by the base class, CheckedListbox, it will again change the checkbox state back to what it was.
一个小哈克改变状态来回,但我无法找到任何其他方式来解决这的CheckedListBox使用的SelectedIndex检查/取消选中,这是有点一个黑客攻击,太方法。
A little hacky changing the state back and forth but I couldn't find any other way to get around the way that CheckedListbox uses SelectedIndex to check/uncheck, which is sort of a hack, too.
Private MyCheckInCheckbox As Boolean = False
''' <summary>
''' Only change the checkbox value when clicking on the box
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property CheckInCheckbox() As Boolean
Get
Return MyCheckInCheckbox
End Get
Set(ByVal value As Boolean)
MyCheckInCheckbox = value
End Set
End Property
Private Sub MyCheckedListBox_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
If CheckInCheckbox Then
Dim border As Integer = 1
Dim index As Integer = IndexFromPoint(e.Location)
If index <> ListBox.NoMatches Then
Dim bounds As Rectangle = Me.GetItemRectangle(index)
Dim idealCheckSize As Integer
If Application.RenderWithVisualStyles Then
Dim cbState As VisualStyles.CheckBoxState
Select Case Me.GetItemCheckState(index)
Case CheckState.Checked
cbState = VisualStyles.CheckBoxState.CheckedNormal
Case CheckState.Indeterminate
cbState = VisualStyles.CheckBoxState.MixedNormal
Case CheckState.Unchecked
cbState = VisualStyles.CheckBoxState.UncheckedNormal
End Select
Dim g As Graphics = Me.CreateGraphics
idealCheckSize = CheckBoxRenderer.GetGlyphSize(g, cbState).Width
g.Dispose()
End If
Dim centeringFactor As Integer = Math.Max((bounds.Height - idealCheckSize) \ 2, 0)
If centeringFactor + idealCheckSize > bounds.Height Then
centeringFactor = bounds.Height - idealCheckSize
End If
Dim box As Rectangle = New Rectangle(bounds.X + border, bounds.Y + centeringFactor, idealCheckSize, idealCheckSize)
If RightToLeft = Windows.Forms.RightToLeft.Yes Then
box.X = bounds.X + bounds.Width - idealCheckSize - border
End If
If Not box.Contains(e.Location) Then
Me.SelectedIndex = index
SetItemChecked(index, Not GetItemChecked(index))
End If
End If
End If
End Sub
这篇关于我如何CheckOnClick在的CheckedListBox但只有当通过复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!