派生自基类和覆盖函数 [英] derive from base class and override functions

查看:76
本文介绍了派生自基类和覆盖函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试以前从未做过的事情!我不知道它是否可能!



当我们使用WinForm时(在visual studio中添加新表单:例如form1),我们有此类用于标签控件(标签[来自元数据]):

(当光标位于标签字样时按F12时出现此类)



  public   class 标签:Control 
{
...
受保护 覆盖 void OnTextChanged(EventArgs e);
...
}





好​​的。现在我想通过编写一些代码来覆盖OnTextChanged事件!

我该怎么做?



我想我应该派生一个子类来自Label类,然后覆盖这样的函数



  public  < span class =code-keyword> class  Class1:Label 
{
protected override void OnTextChanged(EventArgs e)
{
MessageBox.Show( S);
}
}





如果是这样,我应该如何以及在哪里添加这个课程?

如果没有,我怎么能覆盖这样的函数(在控件itselt中定义)?



提前谢谢。

解决方案

你的代码是完全正确的,但是......看起来正确 System.Windows.Forms.Label 没有这个方法,但是 System.Windows.Forms.TextBox 有。因此,如果您使用 TextBox 基类和此方法,您的代码将是正确的:

http://msdn.microsoft.com/en-us/library/z0bhdw6s.aspx [ ^ ]。



只有从不使用Class1之类的名称。使用良好的Microsoft命名约定;没有数字,给它语义名称。在类的名称中使用Class一词与应用程序名称MyApplication.EXE,某些标签的MyLabel等一样奇怪。



现在,通过这种覆盖,你不会覆盖事件。没有这样的想法。您只能覆盖方法。通过重写此方法,您已经相应地处理了事件,您不需要任何其他内容。或者,您可以通过将事件处理程序添加到调用列表来处理相应的事件。 >文本框。这是一种相关的替代技术,但它不是您问题的一部分。自己学习活动;这个知识对于开发来说至关重要。



关于我应该如何以及在哪里添加这个类?的问题,它完全没有意义。您可以在需要的地方添加它。如果你不知道在哪里,我不确定你需要它。使用语言的一般规则和一般语义。



基本上是这样的:

  public   class  MyTextBox:TextBox { / *   ... * / } 

// ...

MyTextBox textBox = new MyTextBox();
textBox.Parent = somePanel; // 这会将您的控件添加到UI
// 替代形式:
// somePanel.Controls.Add(textBox);





设计师也能识别您的控件类型。这与此无关;编译代码时,请查看工具箱。如果您使用设计器添加自定义控件,最终会生成一些代码,相当于我上面显示的代码。



-SA

Hi,

I''m trying something I''ve never done before ! I don''t know if it''s possible or not!

when we use WinForm ( add new form in visual studio : form1 for example ), we have this class for Label Controls (Label [from metadata]):
(this class appears when you press F12 when the cursor is on "Label" word)

public class Label : Control
{
...
       protected override void OnTextChanged(EventArgs e);
...
}



OK. Now I wanna override the OnTextChanged event by writing some code !
How should I do this ??

I think I should derive a subclass from Label class and then override the function like this

public class Class1 : Label
{
    protected override void OnTextChanged(EventArgs e)
    {
        MessageBox.Show("S");
    }
}



if so, how and where should I add this class?
if not, how can I override functions like this ( which are defined inside the control itselt ) ?

Thanks in advance.

解决方案

Your code would be quite correct, but… look properly System.Windows.Forms.Label does not have this method, but System.Windows.Forms.TextBox has. So, your code will be correct if you use TextBox base class and this method:
http://msdn.microsoft.com/en-us/library/z0bhdw6s.aspx[^].

Only never use the names like "Class1". Use good Microsoft naming conventions; no numeric, give it semantic names. And using the word "Class" in a name of the class is as weird as the name "MyApplication.EXE" for application, "MyLabel" for some label, etc.

Now, by such override, you do not "override event". There is no such notion. You can only override method. And by overriding this method, you already handle the event accordingly, you don''t need anything else. Alternatively, you can handle corresponding event by adding an event handler to an invocation list of corresponding event instance of some instance of TextBox. This is a related alternative technique, but it was not a part of your question. Learn event by yourself; this knowledge is critically important for development.

As to the question "how and where should I add this class?", it makes no sense at all. You add it where you need it. If you don''t know where, I''m not sure you need it. Use the general rules of the language and general semantic.

Basically, something like this:

public class MyTextBox : TextBox {/* ... */}

//...

MyTextBox textBox = new MyTextBox();
textBox.Parent = somePanel; // this will add your control to UI
// alternative form:
// somePanel.Controls.Add(textBox);



The designer will also be able to recognize your control type. There is nothing to do about that; when your code is compiled, look at the toolbox. If you add your custom control using the designer, eventually, it will generate some code equivalent to the code I''ve shown above.

—SA


这篇关于派生自基类和覆盖函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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