如何使用不同的功能? [英] How to access different fuctions?

查看:87
本文介绍了如何使用不同的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这听起来很愚蠢,请不要拒绝投票,请回答,我可能会接受.


所以这是我的问题

If this sounds stupid don''t down vote it please, answer it and I''ll probably accept it.


So here''s my problem

public void btnOk_Click(object sender, EventArgs e)
{
    //when btnOk is clicked execute this
}
public void btnOk2_Click(object sender, EventArgs e)
{
    btnOk_Click(/*I don't know what to enter here for 'object sender, EventArgs e' to get to 'btnOk_Click'*/);
    
}


我知道我可以做到


I know I could do this

btnOk2.Click += new EventHandler(btnOk_Click);


但这对我想要的不起作用.


but that doesn''t work for what I want.
Any suggestions?

推荐答案

一个重要的问题是:您是否在btnOk_Click中使用参数?
如果否,请执行以下操作:
An important question is: Are you using the parameters in btnOk_Click?
If no, then do this:
public void btnOk2_Click(object sender, EventArgs e)
{
     btnOk_Click(null,null);
}


如果是,那么您需要使用必要的参数调用该方法.


If yes, then you need to call the method with the necessary parameters.


您好,

您似乎需要为两个ButtonClick事件执行通用代码.我认为最好的方法是编写要在单独方法中执行的代码.假设方法为Common_Code().然后执行以下操作:

Hi,

You seem to need to execute a common code for two ButtonClick events. I think the best way is to write the code to be executed in a separate method. Let''s say the method be Common_Code(). Then do the following:

btnOk.Click += new EventHandler(btnOk_Click);
btnOk2.Click += new EventHandler(btnOk2_Click);



然后执行以下操作:



Then do this:

public void btnOk_Click(object sender, EventArgs e)
{
    this.Common_Code();
}
public void btnOk2_Click(object sender, EventArgs e)
{
    this.Common_Code();

}




在调用 Common_Code()方法时,请根据需要使用适当的参数. this 关键字可帮助您指向通过其调用方法的当前对象.

我认为这将是执行通用代码的更好方法.




While calling Common_Code() method use appropriate parameters as you might be needing. The this keyword helps you to point at the current object through which you are calling the method.

I think this will be better way to execute a common code.


对于"sender"参数,您还可以使用关键字"this",这意味着表单本身.
For the ''sender'' argument, you can also use the keyword ''this'', meaning that the form itself.


这篇关于如何使用不同的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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