如何以其他形式使用按钮的单击事件 [英] How to use a button's click event in another form

查看:134
本文介绍了如何以其他形式使用按钮的单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net CF 3.5

我有一个带有几个按钮的表单,所有表单都导致(如果单击)到表单2,在表单2中,我需要找出我在第一个表单中单击的女巫按钮,然后才能使用该事件.

我试图从第二种形式调用form.btn1_Click事件,但是我得到了这个信息:

非静态字段,方法或属性需要对象引用

我将所有表单和所有按钮事件都公开了.

我该怎么做.

.Net CF 3.5

I have a form with several buttons on it, all are leading(if clicked) to form 2, in form 2 i need to find out witch button i clicked in the first form and to be able to use that event.

I tried to call the form.btn1_Click event from second form, but i get this :

An object reference is required for the non-static field, method, or property

I put all the forms and all the buttons events on public.

How do i do it.

推荐答案

首先,删除您已经完成的所有公共工作(事件处理程序和控件),因为这不是解决方案解决您的问题.

为什么您需要知道按下了哪个按钮?我无法想象需要哪种方案.

如果由于Form2中发生了某些事情而需要ButtonForm中单击点击,则Form2应该引发Form所预订的事件,并在该事件的处理程序中它应该为ButtonClick调用处理程序,或者调用ButtonPerformClick()方法.
First of all, remove all the public stuff (event handlers and controls) that you''ve done as that is not the solution to your problem.

Why do you need to know which button was pressed? I can''t imagine a scenario where that would be needed.

If you need a Button to Click in Form due to something happening in Form2 then Form2 should raise an event that Form subscribes to, and in the handler for that event it should either call the handler for the Button''s Click or call the Button''s PerformClick() method.
// Form2.cs
public event EventHandler ReclickRequest;

private void WhateverItIsThatCausesThis()
{
    OnReclickRequest(EventArgs.Empty);
}

protected virtual void OnReclickRequest(EventArgs e)
{
    EventHandler eh = ReclickRequest;
    if(eh != null)
        eh(this, e);
}


// Form1.cs
// In method that instanciates Form2
    Form2 form2 = new Form2();
    form2.ReclickRequest = form2_ReclickRequest;
//

    private void form2_ReclickRequest(object sender, EventArgs e)
    {
        // Call the button's Click event handler or it's PerformClick method
    }



请参见此处 [



See here[


这篇关于如何以其他形式使用按钮的单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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