Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么';是否在我的DataGridView中单击此按钮?_C#_Winforms_Events_Button_Datagridview - Fatal编程技术网

C# 为什么';是否在我的DataGridView中单击此按钮?

C# 为什么';是否在我的DataGridView中单击此按钮?,c#,winforms,events,button,datagridview,C#,Winforms,Events,Button,Datagridview,我在DataGridView中添加一个按钮,当一行悬停在上面时,允许用户删除该行。我以前把这个列作为一个额外的列,但现在我只想当鼠标悬停在上面时按钮显示出来。该按钮将自身移动到悬停在其上并放置在最后一列末尾的行 为什么按钮不允许我点击它(事件从不触发) 我尝试添加其他点击事件,看看它们是如何与此交互的,它们似乎工作正常 this.Click += (s, e) => MessageBox.Show("DataGridView Clicked!", "", MessageBoxBut

我在DataGridView中添加一个按钮,当一行悬停在上面时,允许用户删除该行。我以前把这个列作为一个额外的列,但现在我只想当鼠标悬停在上面时按钮显示出来。该按钮将自身移动到悬停在其上并放置在最后一列末尾的行

为什么按钮不允许我点击它(事件从不触发)

我尝试添加其他点击事件,看看它们是如何与此交互的,它们似乎工作正常

this.Click += (s, e) =>
    MessageBox.Show("DataGridView Clicked!", "", MessageBoxButtons.OK);
this.CellClick += (s, e) => 
    MessageBox.Show("Cell Clicked!", "", MessageBoxButtons.OK);
this.MouseClick += (s, e) =>
    MessageBox.Show("DataGridView Mouse Clicked!", "", MessageBoxButtons.OK);
this.CellContentClick += (s, e) =>
    MessageBox.Show("Cell Content Clicked!", "", MessageBoxButtons.OK);
创建CustomDataGridView:

public partial class MainForm : Form {      
    public MainForm() {
        InitializeComponent();

        CustomDataGridView dgv = new CustomDataGridView();
        dgv.Location = new Point(100, 100);
        dgv.Width = 500;
        dgv.Height = 300;
        this.Controls.Add(dgv);

        dgv.Rows.Add("text1", "", "");
        dgv.Rows.Add("text2", "", "");
        dgv.Rows.Add("text3", "", "");
        dgv.Rows.Add("text4", "", "");
        dgv.Rows.Add("text5", "", "");
    }
}

尽量不要在
CellMouseLeave
事件处理程序中设置
按钮的
可见属性

由于您正在为整个
DataGridView
控件使用单个
按钮创建一个
,只需更改其位置,因此不需要处理
CellMouseLeave
事件。这对我很有用:

public class CustomDataGridView : DataGridView
{
    private Button deleteButton = new Button();

    public CustomDataGridView()
    {
        this.Columns.Add("Column1", "Column1");
        this.Columns.Add("Column2", "Column2");
        this.Columns.Add("Column3", "Column3");

        this.CellMouseEnter += this_CellMouseEnter;

        deleteButton.Height = this.RowTemplate.Height - 1;
        deleteButton.Width = this.RowTemplate.Height - 1;
        deleteButton.Text = "";
        deleteButton.Visible = false;

        deleteButton.MouseClick += (s, e) =>
            MessageBox.Show("Delete Button Clicked!", "", MessageBoxButtons.OK);

        this.Controls.Add(deleteButton);
    }

    private void this_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex != -1)
        {
            deleteButton.Visible = true;
            Rectangle rect = this.GetCellDisplayRectangle(2, e.RowIndex, true);
            deleteButton.Location = new Point(rect.Right - deleteButton.Width - 1, rect.Top);
        }
    }
}

哇!这么简单的解决方案!我真的不知道为什么去掉它会改变很多,但我很高兴你明白了!!!!谢谢大家!@BlakeThingstad,因为当鼠标移动到按钮上时,它会触发CellMouseLeave事件,该事件隐藏按钮,该事件触发CellMouseEnter事件,该事件显示按钮,等等。您的代码忙于显示和隐藏按钮,无法实际执行任何操作。@LarsTech这是有意义的。谢谢你的解释!
public class CustomDataGridView : DataGridView
{
    private Button deleteButton = new Button();

    public CustomDataGridView()
    {
        this.Columns.Add("Column1", "Column1");
        this.Columns.Add("Column2", "Column2");
        this.Columns.Add("Column3", "Column3");

        this.CellMouseEnter += this_CellMouseEnter;

        deleteButton.Height = this.RowTemplate.Height - 1;
        deleteButton.Width = this.RowTemplate.Height - 1;
        deleteButton.Text = "";
        deleteButton.Visible = false;

        deleteButton.MouseClick += (s, e) =>
            MessageBox.Show("Delete Button Clicked!", "", MessageBoxButtons.OK);

        this.Controls.Add(deleteButton);
    }

    private void this_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex != -1)
        {
            deleteButton.Visible = true;
            Rectangle rect = this.GetCellDisplayRectangle(2, e.RowIndex, true);
            deleteButton.Location = new Point(rect.Right - deleteButton.Width - 1, rect.Top);
        }
    }
}