从基类控制事件 [英] Controling event from base class

查看:48
本文介绍了从基类控制事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个表单上有几个DataGridView。当用户选择行时,它将显示一些数据/结果,但有条件。如果该条件通过则只显示结果。



我可以检查每个DataGridView的每个事件的条件。但是我通过继承DataGridView来创建自定义控件。并希望对基类进行条件检查。如果条件在基类上传递,那么它将触发子类的事件,否则它将阻止或不通过事件触发。



我也可以写单独的事件这种情况,但作为学习或基金,我想举办实际活动。



提前致谢。



我尝试过:



I have several DataGridView on several form. When user select rows it will show some data/result, but there is condition. If that condition pass then only result display.

I can check condition on each event of each DataGridView. But I create custom control by inheriting DataGridView. And want to put condition check on base class. If condition passed on base class then it will fire the event of child class, else it will block or not pass the event firing.

I can also write separate event with this condition, but as a learn or fund I want to twit actual event.

Thanks in advance.

What I have tried:

/*Inherited base Class*/
public partial class MyDataGridView : DataGridView
{
  public bool ShowData = new bool();
  public new event System.EventHandler SelectionChanged;

  public CustomDataGridView()
  {
     InitializeComponent();
  }

  protected override void base_SelectionChanged(object sender, EventArgs e)
  {
     if(ShowData)
     {
       //Want to raise/pass/trigger event on child class
       if (this.SelectionChanged != null) this.SelectionChanged(sender, e);
     }
     else
     {
       //do not want to raise/pass/trigger event on child class
     }
  }

}

/*On Win Form*/
public partial class Form1 : Form
{
  public Form1()
  {
    InitializeComponent();
  }

  private void MyDataGridView1_SelectionChanged(object sender, EventArgs e)
  {
    MessageBox.Show("My Result........");
  }
}

推荐答案

问题是如果基类方法一直没有执行的话在派生类中重写,除非明确调用它:

The problem is that base class methods are not executed at all if they have been overridden in a derived class unless they are explicitly called from it:
    public static void Main()
        {
        Base b = new Derived();
        b.MyMethod();
        Console.WriteLine();
        }
    }
public abstract class Base
    {
    public virtual void MyMethod()
        {
        Console.WriteLine("Base");
        }
    }
public class Derived : Base
    {
    public override void MyMethod()
        {
        Console.WriteLine("Derived");
        }
    }

只会给你Derived - 你需要显式调用基本版本:

Will just give you "Derived" - you need to explicitly call teh base version:

    public static void Main()
        {
        Base b = new Derived();
        b.MyMethod();
        Console.WriteLine();
        }
    }
public abstract class Base
    {
    public virtual void MyMethod()
        {
        Console.WriteLine("Base");
        }
    }
public class Derived : Base
    {
    public override void MyMethod()
        {
        base.MyMethod();
        Console.WriteLine("Derived");
        }
    }

获得Base和Derived。

这意味着你想要做的不是开始工作!



您可以做的是将DataGridView封装在您的控件中,并创建您自己的派生类可以访问的方法和事件。



如果你创建一个抽象的UserControl,那么如果没有在抽象类和派生实现之间添加具体的派生实现,你将无法直接在设计器中编辑它。如果您尝试,设计师将拒绝显示表格。

to get "Base" and "Derived".
Which means that what you want to do isn't going to work!

What you can do is encapsulate the DataGridView in your control, and create your own methods and events which your derived classes can access.

Do not that if you create an abstract UserControl, you will not be able to edit it directly in the designer without adding a concrete derived implementation between the abstract class and your derived implementation. If you try, the designer will refuse to show the form.


这篇关于从基类控制事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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