在checkedListBox中中止mouseClick事件 [英] abort mouseClick event in a checkedListBox

查看:55
本文介绍了在checkedListBox中中止mouseClick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。

我有一个问题。



在一个checkedListBox我有5件物品。

如果我在没有项目的区域中单击鼠标,则会检查实际的选择项目



如果我单击控件的干净区域,我不希望这样selectedIndex更改值。

我只能更改它,如果我点击它。



我找到了如何检查鼠标点击它在一个干净的区域:



Hello.
I have a problem.

In a checkedListBox i have 5 items.
If i click with mouse in a zone without an item, the actual selecteditem became checked

I don't want that if i click in a clean zone of the control, the selectedIndex change value.
I vant only change it, if i click over it.

I have found how to check if the mouseClick it's in a clean zone:

Private Sub checkedLB_Click(sender as Object, e as EventArgs)

dim MousePoint as point = checkedLB.PointToClient(Form.MousePosition)
Dim I as integer = checkedLB.IndexFromPoint(MousePoint)
If I = -1 Then
   'Here, i usually use an
   'e.Handled = True
   'but there isn't that property in the MouseClick EventArgs
End If

Ens Sub



有人知道热修复吗?

谢谢。

____________________________________________________



感谢回答Dave。



在网上搜索,很多人都说'没有模式可以中止点击事件',我知道。

但是有一个问题。

我的意思是:

如果你复制并运行这段代码,你会理解我的意思。


Did someone know hot to fix it?
Thanks.
____________________________________________________

Thanks for answer Dave.

Searching in the web, lots of people say that 'there is no mode to abort a click event', i know.
But there is a problem.
I mean:
if you copy and run this piece of code, you will understand what i mean.

Public Class Form1
    Dim checkedLB As CheckedListBox
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        checkedLB = New CheckedListBox
        With checkedLB
            .Size = New Size(100, 200)
            .Location = New Point(10, 10)
            .Items.Add("PAUL")
            .Items.Add("NICK")
            .Items.Add("VIRGINIA")
        End With
        Me.Controls.Add(checkedLB)

    End Sub
End Class





如果单击checkedListBox的底部,而不是单击某个项目,您会注意到selectedIndex值更改,即使您没有点击该项



处理itemCheck事件的问题是,如果你修改'newValue'你开始一个循环:每次修改ify事件,你再次调用它!

这不能解决问题。



If you click in the bottom of the checkedListBox, not over an item, you'll notice that the selectedIndex value change, even if you don't click over that item.

The problem handling the itemCheck event it's that if you modify the 'newValue' you start a loop: every time you modify the event, you call it again!!!
That's don't solve the problem.

推荐答案

你没有中止Click事件。所有它正在告诉你鼠标点击这里。而已。你没有机制可以中止。



我不知道你在区域内是什么意思。 CheckedListBox中没有这样的概念。



您可以做的是处理ItemCheck事件并修改 ItemCheckEventArgs [ ^ ] .NewValue属性,用于控制是否检查项目。
You don't "abort" the Click event. All it's doing is telling you that "ehy the mouse clicked here". That's it. There is no mechanism for you to abort.

I have no idea what you mean by "in a zone". There's no such concept in the CheckedListBox.

What you can possibly do is handle the ItemCheck event and modify the ItemCheckEventArgs[^].NewValue property to control whether an item is checked or not.


经过一些测试我找到了解决方案



After some test i found the solution

Public Class Form1
    Dim checkedLB As CheckedListBox
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        checkedLB = New CheckedListBox
        With checkedLB
            .Size = New Size(100, 200)
            .Location = New Point(10, 10)
            .Items.Add("PAUL")
            .Items.Add("NICK")
            .Items.Add("VIRGINIA")
            .CheckOnClick = True
            AddHandler .Click, AddressOf checkedLB_Click
        End With
        Me.Controls.Add(checkedLB)

    End Sub
    Private Sub checkedLB_Click(sender As Object, e As EventArgs)

        Dim MousePoint As Point = checkedLB.PointToClient(Form.MousePosition)
        Dim I As Integer = checkedLB.IndexFromPoint(MousePoint)
        If I = -1 Then
            checkedLB.SelectedIndex = -1
        End If

    End Sub

End Class





因为在itemCheck事件之前处理了click事件,我们有是时候查看是否点击了某个项目。

如果没有,我们将selectedIndex设置为-1



我们输了索引的选择,但我们避免如果我们点击CheckedListBox上的任何地方,我们不会改变实际selectedIndex的值。



(也许如果我们需要保留索引值,我们可以使用变量)



since the click event is processed before the itemCheck event, we have time to see if the click was made over an item or not.
If not, we set the selectedIndex to -1

We lose the selection of the index, but we avoid that if we click anywhere on the CheckedListBox, we don't change the value of the actual selectedIndex.

(maybe if we need to preserve the index value, we can use a variable)


这篇关于在checkedListBox中中止mouseClick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆