繁体   English   中英

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

[英]C# Remove custom MouseClick EventHandler from control

我试图向某些按钮添加然后删除自定义MouseClick事件处理程序。 窗口上的控件是在运行时构建的。 我可以根据其他回答的问题添加我的自定义处理程序。 localBtn.MouseClick += new MouseEventHandler((se, e) => Move_MouseClick(se, e, center, type));

我阅读了有关删除EventHandlers的其他问题,但它们似乎适用于“标准”事件处理程序。 我终于得到了这个来编译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)); (在参数中传递了center和type的地方。)我不知道这是否有效……因为我今天的时间已经用完了……

我正在创建一个按钮网格(8x4)(将来可能会更大),当单击一个按钮时,周围的按钮会“更改”为动作按钮。 想一想井字游戏,单击中心按钮是一个,其他所有按钮都变为“活动”状态。 创建自定义处理程序时,将参考“中心”按钮,然后是一个枚举,该枚举指示该处理程序用于的8个按钮中的哪个,顶部,左侧,右侧,topLeft,BottomRight等。按下“发生了某些事情”,需要删除处理程序,并且按钮将恢复为“非活动”状态。 这些按钮派生自一个对象,该对象对其周围的所有其他“按钮”都有“引用”。

我阅读了如何从控件中删除所有事件处理程序,以及如何C#删除 事件处理程序

也许更好的控件更适合我的工作?

[edit]我已经尝试了-=,如上所示,但是没有用。 该控件仍然具有两个事件处理程序。 该程序无论如何都没有失败,所以我不确定-=做了什么? 我猜没事。

听起来您应该对要完成的操作使用用户控件。 这是一个如何在后面的代码中完成此操作的示例(对于您正在做的事情可能会有更好的方法;这只是给您一个有效的示例):

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(也可以在此处制作网格,因为其他所有操作都在此处完成):

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;
            }
        }
    }

我只是将它放在<local:ButtonGrid Width="500" Height="500" />的主窗口中。

在这里输入代码

当按下时:

在此处输入图片说明

而且,您可以继续单击按钮以查看其周围的按钮是如何启用的(或您想对它们进行的操作)。

您可以将另一个mouseclick事件创建为空,然后将按钮的点击分配给该空按钮,

但这一次不使用+ =,只需使用=

暂无
暂无

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

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