无法在表单加载时触发button1_Click事件 [英] button1_Click event could not be fired on form load

查看:101
本文介绍了无法在表单加载时触发button1_Click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void Form1_Load(object sender, EventArgs e)
        {
           button1.Click += new EventHandler(button1_Click);
        }


private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.CellMouseClick += dataGridView1_CellMouseClick;
        }










void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
       {
           if (e.Button == MouseButtons.Right && e.RowIndex == -1 && e.ColumnIndex == -1)
           {
               mCheckedListBox.Items.Clear();
               foreach (DataGridViewColumn c in dataGridView1.Columns)
               {
                   mCheckedListBox.Items.Add(c.HeaderText, c.Visible);
               }
               int PreferredHeight = (mCheckedListBox.Items.Count * 16) + 7;
               mCheckedListBox.Height = (PreferredHeight < MaxHeight) ? PreferredHeight : MaxHeight;
               mCheckedListBox.Width = this.Width;
               mPopup.Show(dataGridView1.PointToScreen(new Point(e.X, e.Y)));
           }
       }







我要解雇dataGridView1.CellMouseClick + = dataGridView1_CellMouseClick;

表格加载。




I want to fire "dataGridView1.CellMouseClick += dataGridView1_CellMouseClick;"
on form load.

推荐答案

挂钩活动 - 这就是你在这里做的事情:

Hooking up an event - which is what you are doing here:
private void Form1_Load(object sender, EventArgs e)
        {
           button1.Click += new EventHandler(button1_Click);
        }

在这里:

And here:

private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.CellMouseClick += dataGridView1_CellMouseClick;
        }



不会导致事件被触发 - 它只是向事件触发时调用的处理程序集合添加一个方法。因此,在按钮中重复添加处理程序Click handler对任何事情都没有帮助:每次用户按下按钮时,它都会添加另一个处理程序,因此当事件最终触发时,它将多次调用相同的处理程序方法。 />


在正常情况下你应该只添加一次处理程序。



你不应该试图强制事件 - 相反,重构您的代码,以便CellMouseClick处理程序中的代码在一个单独的方法中,并从您的单击事件中调用它:


Does not cause the event to be fired - it just adds a method to the collection of handlers which will be called when the event does fire. So repeatedly adding the handler in your button Click handler doesn't help anything: each time your user presses the button, it will add another handler so when the event finally fires, it will call the same handler method a number of times.

You should only add the handler once, under normal circumstances.

And you shouldn't be trying to "force" events - instead, restructure your code so that the code inside your CellMouseClick handler is in a separate method, and call that from both your click events:

private void Form1_Load(object sender, EventArgs e)
        {
           button1.Click += new EventHandler(button1_Click);
           dataGridView1.CellMouseClick += dataGridView1_CellMouseClick;
        }

private void button1_Click(object sender, EventArgs e)
        {
            DoMyClickAction();
        }

void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
       {
           if (e.Button == MouseButtons.Right && e.RowIndex == -1 && e.ColumnIndex == -1)
           {
            DoMyClickAction(e.X, e.Y);
           }
       }

private void DoMyClickAction(int x = 0, int y = 0)
       {
           mCheckedListBox.Items.Clear();
           foreach (DataGridViewColumn c in dataGridView1.Columns)
           {
               mCheckedListBox.Items.Add(c.HeaderText, c.Visible);
           }
           int PreferredHeight = (mCheckedListBox.Items.Count * 16) + 7;
           mCheckedListBox.Height = (PreferredHeight < MaxHeight) ? PreferredHeight : MaxHeight;
           mCheckedListBox.Width = this.Width;
           mPopup.Show(dataGridView1.PointToScreen(new Point(x, y)));
       }


未调用该事件,因为您甚至没有尝试这样做。但还有更基本的原因:除了声明事件实例的类的方法之外,你不能直接调用任何事件。因为类 System.Windows.Forms.Control 不是你的,所以根本不能。这不是问题,而是一个重要的.NET 傻瓜式功能。 你永远不需要它。如果你解释你想要实现的目标,我会回答。



除此之外,你的问题表明你不知道甚至是什么。不, button1_Click 不是活动。这只是一种方法。只使用运算符+ =使它成为事件处理程序。你实际上是这样做的,因此它被用作处理程序。事件是 System.Windows.Forms.Control.Click



不用担心;只是彻底了解代表和事件;很快你就会得到这一切。



-SA
The event is not invoked, because you did not even try to do it. But there is more fundamental reason: you cannot directly invoke any event anywhere but the method of the class which declared the event instance. And as the class System.Windows.Forms.Control is not yours, you cannot to it at all. This is not a problem, but an important .NET fool-proof feature. You just never need it. If you explain what you want to achieve, I'll answer.

In addition to that, your question shows that you have no clue what even is. No, button1_Click is not an event. This is just a method. You make it an event handler only by using the operator +=. You actually do it, so it is used as a handler. The event is System.Windows.Forms.Control.Click.

Not to worry; just learn the delegates and events thoroughly; and soon you will get it all.

—SA


这篇关于无法在表单加载时触发button1_Click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆