简体   繁体   English

C#从控件中删除自定义MouseClick EventHandler

[英]C# Remove custom MouseClick EventHandler from control

I am trying to add and then remove custom MouseClick event handlers to some Buttons. 我试图向某些按钮添加然后删除自定义MouseClick事件处理程序。 The controls on the window are being built at run-time. 窗口上的控件是在运行时构建的。 I can add my custom handler as per another answered question. 我可以根据其他回答的问题添加我的自定义处理程序。 localBtn.MouseClick += new MouseEventHandler((se, e) => Move_MouseClick(se, e, center, type));

I read the other questions about remove EventHandlers, but they seem to geared to the 'standard' event handler. 我阅读了有关删除EventHandlers的其他问题,但它们似乎适用于“标准”事件处理程序。 I finally got this to compile object se=null; MouseEventArgs e=null; localBtn.MouseClick -= new MouseEventHandler((se1, e1) => Move_MouseClick( se, e, center, type)); 我终于得到了这个来编译object se=null; MouseEventArgs e=null; localBtn.MouseClick -= new MouseEventHandler((se1, e1) => Move_MouseClick( se, e, center, type)); object se=null; MouseEventArgs e=null; localBtn.MouseClick -= new MouseEventHandler((se1, e1) => Move_MouseClick( se, e, center, type)); (Where center and type are passed in parameters.) I do not know if this works or not...as I have run out of time for today.... (在参数中传递了center和type的地方。)我不知道这是否有效……因为我今天的时间已经用完了……

I'm creating a grid (8x4) of buttons (maybe larger in the future), when one is clicked, the surrounding buttons 'change' into action buttons. 我正在创建一个按钮网格(8x4)(将来可能会更大),当单击一个按钮时,周围的按钮会“更改”为动作按钮。 Think of tic-tac-toe and the center button is the one clicked, all the others become 'active'. 想一想井字游戏,单击中心按钮是一个,其他所有按钮都变为“活动”状态。 The custom handler is created with a reference to the 'center' button and then an enum indicating which of the 8 buttons then handler is for, top,left,right, topLeft, BottomRight, etc.. Once one of the 'active' buttons is pressed 'something happens' and the handlers need to be removed and the buttons will revert to their 'inactive' state. 创建自定义处理程序时,将参考“中心”按钮,然后是一个枚举,该枚举指示该处理程序用于的8个按钮中的哪个,顶部,左侧,右侧,topLeft,BottomRight等。按下“发生了某些事情”,需要删除处理程序,并且按钮将恢复为“非活动”状态。 The buttons are derived from an object that has 'references' to all the other 'buttons' around it. 这些按钮派生自一个对象,该对象对其周围的所有其他“按钮”都有“引用”。

I read How to remove all event handlers from a control and C# removing an event handlerr . 我阅读了如何从控件中删除所有事件处理程序,以及如何C#删除 事件处理程序

Perhaps a better control is more suited to what I'm doing ? 也许更好的控件更适合我的工作?

[edit] I have tried the -= as shown above but that did not work. [edit]我已经尝试了-=,如上所示,但是没有用。 The control still had both event handlers attached. 该控件仍然具有两个事件处理程序。 The program did not fail in anyway, so I'm not sure what the -= did? 该程序无论如何都没有失败,所以我不确定-=做了什么? I guess nothing. 我猜没事。

It sounds like you should use a user control for what you're trying to accomplish. 听起来您应该对要完成的操作使用用户控件。 Here is an example of how it could be done in code behind (there may be a better way for what you're doing specifically; this is just to give you a working example): 这是一个如何在后面的代码中完成此操作的示例(对于您正在做的事情可能会有更好的方法;这只是给您一个有效的示例):

ButtonGrid.xaml: ButtonGrid.xaml:

<UserControl x:Class="WpfApplication1.ButtonGrid"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid x:Name="PART_Grid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    </Grid>
</UserControl>

ButtonGrid.cs (could also make the Grid in here, since everything else is being done in here): ButtonGrid.cs(也可以在此处制作网格,因为其他所有操作都在此处完成):

public partial class ButtonGrid : UserControl
    {
        private int rows, cols;
        private Button[] buttons;
        public ButtonGrid()
        {
            InitializeComponent();

            cols = 4;
            rows = 8;
            buttons = new Button[cols * rows];
            Button button;
            for (int i = 0; i < cols; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    // Make the button, set its place on the grid, set the necessary properties, give it OnClick, then add it to the grid
                    button = new Button();
                    button.SetValue(Grid.ColumnProperty, i);
                    button.SetValue(Grid.RowProperty, j);

                    button.Name = "Button" + (j + (rows * (i))).ToString();
                    button.Content = j + (rows * (i));
                    button.IsEnabled = false;

                    button.Click += OnClick;

                    buttons[j + (rows * (i))] = button;
                    PART_Grid.Children.Add(button);
                }
            }
            buttons[12].IsEnabled = true;
        }

        private void OnClick(object sender, RoutedEventArgs e)
        {
            int index;
            Button button = sender as Button;
            // Determine which button was pressed by finding the index of the button that called this from the button name
            if (button.Name.Length == 7)
            {
                index = Int32.Parse(button.Name.Substring(6, 1));
            }
            else
            {
                index = Int32.Parse(button.Name.Substring(6, 2));
            }

            // Make sure the buttons that are being affected are within bounds
            if ((index - 1) >= 0)
            {
                buttons[index - 1].IsEnabled = true;
            }

            if ((index - rows) >= 0)
            {
                buttons[index - rows].IsEnabled = true;
            }

            if ((index + 1) <= (cols * rows - 1))
            {
                buttons[index + 1].IsEnabled = true;
            }

            if ((index + rows) <= (cols * rows - 1))
            {
                buttons[index + rows].IsEnabled = true;
            }
        }
    }

I just put it in the main window with <local:ButtonGrid Width="500" Height="500" /> . 我只是将它放在<local:ButtonGrid Width="500" Height="500" />的主窗口中。

在这里输入代码

And when pressed: 当按下时:

在此处输入图片说明

And you can keep clicking the buttons to see how the ones around it become enabled (or whatever you want to happen to them). 而且,您可以继续单击按钮以查看其周围的按钮是如何启用的(或您想对它们进行的操作)。

you can create another on mouseclick event empty and assign your button's click to that empty button, 您可以将另一个mouseclick事件创建为空,然后将按钮的点击分配给该空按钮,

but this time dont use += , just use = 但这一次不使用+ =,只需使用=

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM