mousedoubleclick对于动态创建的按钮不起作用

mousedoubleclick对于动态创建的按钮不起作用

本文介绍了mousedoubleclick对于动态创建的按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在按钮事件中,通常将MouseDoubleClick添加到窗体中时会包括在内,但是当我以编程方式将按钮添加到窗体中时,即使在我自己编写程序的过程中也不会出现任何错误,但即使在我自己编写程序的情况下,IDE建议事件中也不存在MouseDoubleClick,但是它不会不要在MouseDoubleClick事件上做任何事情

In Button events MouseDoubleClick is included when normally add it to the form but when I programmatically add buttons to the form MouseDoubleClick doesn't exist in IDE suggestion events even if I write by myself the program execute without any error but it doesn't do anything on MouseDoubleClick event

这是我的代码:

Dim pb As New Button
pb.Text = "Hello"
pb.Size = New Size(150, 110)
frmAddImage.flPanel.Controls.Add(pb)

AddHandler pb.MouseDoubleClick, AddressOf pbButton_MouseDoubleClick

Private Sub pbButton_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    'Do something
End Sub

推荐答案

其归结为以下内容:按钮通常不使用双击事件.但是,按钮类从提供双击事件的Control继承.这样就可以了,但是没有被班级解雇.

What it boils down to is the following: Buttons don't normally use the double-click event.The button class however inherits from Control which provides the double-click event. So it is there but it's not fired by the class.

您可以在MouseDown事件中使用MouseEventArgs变量的.Clicks属性:

You can use the .Clicks property of the MouseEventArgs variable in the MouseDown event though:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim pb As New Button
    pb.Text = "Hello"
    pb.Size = New Size(150, 110)
    frmAddImage.flPanel.Controls.Add(pb)
    AddHandler pb.MouseDown, AddressOf pbButton_MouseDown
End Sub

Private Sub pbButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If e.Clicks = 2 Then
        MessageBox.Show("The button was double-clicked.")
    End If
End Sub

来自: http://social.msdn.microsoft.com/Forums/zh-CN/83ac6fbd-af42-4b9c-897e-142abb0a8199/can-not-use-event-double-click按钮上

一种 解决方法 是为按钮启用StandardClick和StandardDoubleClick ControlStyles.您需要创建自己的按钮类,并在构造函数中将标志设置为true.然后,您可以处理DoubleClick(不是MouseDoubleClick)事件.

A workaround is to enable the StandardClick and StandardDoubleClick ControlStyles for the button. You need to create your own button class and set the flags to true in the constructor.Then you can handle the DoubleClick (NOT MouseDoubleClick) event.

Public Class MyButton
    Inherits Button
    Public Sub New()
        MyBase.New()
        SetStyle(ControlStyles.StandardDoubleClick, True)
        SetStyle(ControlStyles.StandardClick, True)
    End Sub
End Class

像以前一样将事件连接到另一个类中,只需创建一个MyButton而不是一个Button.

Wire the event up in your other class as before, just create a MyButton and not a Button.

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim pb As New MyButton
    pb.Text = "Hello"
    pb.Size = New Size(150, 110)
    Me.Controls.Add(pb)
    AddHandler pb.DoubleClick, AddressOf pbButton_DoubleClick
End Sub

Private Sub pbButton_DoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    MessageBox.Show("The button was double-clicked.")
End Sub

这篇关于mousedoubleclick对于动态创建的按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 06:56