两次打开彩色对话框?

时间:2018-05-22 04:48:44

标签: vb.net mouseclick-event colordialog

我有一个带有8个命令按钮的表单,我对所有按钮的Click事件使用AddHandler

但是当我按下按钮时,cmbColor_Click会跑两次。

Public Sub OpenForm()
    AddHandler cmbColor1.Click, AddressOf cmbColor_Click
    AddHandler cmbColor2.Click, AddressOf cmbColor_Click
    AddHandler cmbColor3.Click, AddressOf cmbColor_Click
    AddHandler cmbColor4.Click, AddressOf cmbColor_Click
    AddHandler cmbColor5.Click, AddressOf cmbColor_Click
    AddHandler cmbColor6.Click, AddressOf cmbColor_Click
    AddHandler cmbColor7.Click, AddressOf cmbColor_Click
    AddHandler cmbColor8.Click, AddressOf cmbColor_Click
End Sub


Private Sub cmbColor_Click(sender As Object, e As EventArgs)
    Dim _color As New ColorDialog
    Dim _button As Button = CType(sender, Button)
    _color.Color = _button.BackColor
    If _color.ShowDialog() = Windows.Forms.DialogResult.OK Then
        _button.BackColor = _color.Color
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

如果为一个事件调用了两次事件处理程序,那么事件处理程序已经连接到事件两次。

理想情况下,您可以追踪为何多次使用AddHandler并消除问题。

但是,作为快速修复,您可以在添加之前删除事件处理程序 - 尝试删除不存在的事件处理程序并不是错误。

因此...

foreach (string file in Directory.GetFiles(path)
{
    do stuff
}
foreach (string subdir in Directory.GetDirectories(path)
{
    if (subdir == "c:\\Users\\*\\AppData\\Local\\Application Data\\*")
    {
        continue;
    }
    recusivecall(subdir);
}