我确信做这样的事情不是好习惯,但我只是好奇如果有可能......
我尝试使用Visual Studio 2015但是我收到了多个编译错误(很多人要发布) - 但基本上我很好奇的是你是否可以在不同的类中使用文本框的_Leave()事件。像下面的东西
namespace BlueGreenRed
{
public partial class Red : UserControl
{
//In this class I have a text box called txtName
}
}
namespace BlueGreenRed
{
class ReusableMethods
{
private void txtName_Leave(object sender, EventArgs e)
{
//Do something here
}
}
}
答案 0 :(得分:1)
当然你可以这样做:
public class Red
{
public TextBox TxtName {get; set; }
}
public static class ReusableMethods
{
public static void WireUp(Red red)
{
red.TxtName.Leave += txtName_Leave;
}
private static void txtName_Leave(object sender, EventArgs e)
{
//Do something here
}
}
//some other code
var red1 = new Red() { TxtName = new TextBox() };
var red2 = new Red() { TxtName = new TextBox() };
ReusableMethods.WireUp(red1);
ReusableMethods.WireUp(red2);