简体   繁体   English

子类化控件

[英]Sub-class a control

So I know how to create sub-classes and call them, however I get lost when I have to call / use a sub-class of a control: For example: 因此,我知道如何创建子类并调用它们,但是当我不得不调用/使用控件的子类时,我迷路了:例如:

   public class KeyboardButton : Button
    {
        public void SimulateButtonDown()
        {
            this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0));
        }

        public void SimulateButtonUp()
        {
            this.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0));
        }
    }

As created in this article . 本文所创建。 How to I put to use especially when I have multiple control buttons I want to call the method "SimulateButtonDown()". 特别是当我有多个控制按钮时,如何使用该方法,我想调用方法“ SimulateButtonDown()”。

Are you using WinForms or WPF? 您正在使用WinForms还是WPF? For the sake of this answer, I will assume WinForms. 为了这个答案,我将假设使用WinForms。

First, add the code you've posted here into your WinForms project somewhere. 首先,将您在此处发布的代码添加到WinForms项目中的某个位置。 Then compile it. 然后编译它。 After doing so, KeyboardButton should be automatically added to your Toolbox. 这样做之后,应该将KeyboardButton自动添加到您的工具箱中。 Open a form or user control in the designer of Visual Studio, and you should see a section called "YourProjectName Components". 在Visual Studio的设计器中打开一个窗体或用户控件,您应该看到一个名为“ YourProjectName组件”的部分。 This section should have KeyboardButton in it. 此部分应具有KeyboardButton Just drag and drop it onto your form or user control. 只需将其拖放到窗体或用户控件上即可。

If you want to replace existing buttons on existing forms or user controls, it may be easier to update the code in xxx.Designer.cs . 如果要替换现有表单或用户控件上的现有按钮,则更新xxx.Designer.cs的代码可能会更容易。 (NOTE: EXERCISE CAUTION WHEN EDITING xxx.Designer.cs FILES.) In the Solution Explorer, find the form or user control that has the button(s) you want to replace. (注意:编辑xxx.Designer.cs文件时要小心。)在解决方案资源管理器中,找到具有您要替换的按钮的表单或用户控件。 Expand the node for the form/user control, and you should see a file called something like Form1.Designer.cs . 展开表单/用户控件的节点,您应该看到一个名为Form1.Designer.cs的文件。 Double-click to open that file. 双击打开该文件。 In there, for each existing button, you should see lines such as this.saveButton = new System.Windows.Forms.Button(); 在其中,对于每个现有按钮,您应该看到诸如this.saveButton = new System.Windows.Forms.Button(); . Replace System.Windows.Forms.Button with YourNamespace.KeyboardButton . System.Windows.Forms.Button替换为YourNamespace.KeyboardButton You will also see lines like private System.Windows.Forms.Button saveButton; 您还将看到类似于private System.Windows.Forms.Button saveButton; . Replace the namespace & class name there too. 在那里也替换名称空间和类名称。

Once you've got a KeyboardButton on your form/user control, you can call SimulateButtonDown and SimulateButtonUp from within your form/user control just by writing code like saveButton.SimulateButtonUp() . 在窗体/用户控件上具有KeyboardButton ,只需编写诸如saveButton.SimulateButtonUp()类的代码,就可以从窗体/用户控件内调用SimulateButtonDownSimulateButtonUp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM