简体   繁体   English

两次打开颜色对话框?

[英]Twice Open Color Dialog box?

I have a form with 8 command button, I use AddHandler for Click event for all buttons. 我有一个带有8个命令按钮的表单,我对所有按钮使用AddHandler进行Click事件。

but when I press a button, cmbColor_Click run twice. 但是当我按下一个按钮时, 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

If the event handler is being called twice for one event then the event handler has been attached to the event twice. 如果针对一个事件两次调用事件处理程序,则该事件处理程序已附加到该事件两次。

Ideally, you would track down why AddHandler is being used more than once and eliminate the problem. 理想情况下,您将跟踪为什么多次使用AddHandler并消除该问题。

However, as a quick fix, you can remove the event handler before adding it - it is not an error to try to remove a non-existent event handler. 但是,作为快速解决方案,您可以在添加事件处理程序之前先将其删除-尝试删除不存在的事件处理程序并不是错误。

So... 所以...

Friend Sub OpenForm()
    Dim cmbs = {cmbColor1, cmbColor2, cmbColor3, cmbColor4, cmbColor5, cmbColor6, cmbColor7, cmbColor8}
    For Each cmb In cmbs
        RemoveHandler cmb.click, AddressOf cmbColor_Click
        AddHandler cmb.click, AddressOf cmbColor_Click
    Next
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在浏览器中以编程方式与“打开文件”对话框进行交互 - programmatically interact with Open File Dialog Box in browser 在VB中使用打开文件对话框和保存文件对话框以及列表框 - Using open file dialog and save file dialog with a list box in VB Excel文件“打开”“另存为”对话框未在asp.net中显示 - Excel file “Open” “Save As” dialog box not showing in asp.net 如何使用vb脚本浏览和打开窗口对话框上的文件 - how to browse and open a file on window dialog box using vb scripting VB或C#中带有“打开”对话框的“用户控件”属性 - User Control property with Open dialog box in VB or C# 如何在打开文件对话框中使用多选功能 - How to use the multiselect feature in open file dialog box 在确认对话框之前,在ColorDialog仍打开的同时获取color属性? - Get color property while ColorDialog still open, before confirming the dialog? 如何在VB.net的“打开文件”对话框中包含消息框 - How to include the message box within the Open file dialog in VB.net VB.NET如何检查打开/另存为对话框是否打开 - How to check whether the Open/Save As dialog box is opened in VB.NET 如何在vb.net中禁止调用dll来打开警告对话框 - How to suppress a call to a dll to open warning dialog box in vb.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM