[英]Sub-class a control
因此,我知道如何创建子类并调用它们,但是当我不得不调用/使用控件的子类时,我迷路了:例如:
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));
}
}
如本文所创建。 特别是当我有多个控制按钮时,如何使用该方法,我想调用方法“ SimulateButtonDown()”。
您正在使用WinForms还是WPF? 为了这个答案,我将假设使用WinForms。
首先,将您在此处发布的代码添加到WinForms项目中的某个位置。 然后编译它。 这样做之后,应该将KeyboardButton
自动添加到您的工具箱中。 在Visual Studio的设计器中打开一个窗体或用户控件,您应该看到一个名为“ YourProjectName组件”的部分。 此部分应具有KeyboardButton
。 只需将其拖放到窗体或用户控件上即可。
如果要替换现有表单或用户控件上的现有按钮,则更新xxx.Designer.cs
的代码可能会更容易。 (注意:编辑xxx.Designer.cs文件时要小心。)在解决方案资源管理器中,找到具有您要替换的按钮的表单或用户控件。 展开表单/用户控件的节点,您应该看到一个名为Form1.Designer.cs
的文件。 双击打开该文件。 在其中,对于每个现有按钮,您应该看到诸如this.saveButton = new System.Windows.Forms.Button();
。 将System.Windows.Forms.Button
替换为YourNamespace.KeyboardButton
。 您还将看到类似于private System.Windows.Forms.Button saveButton;
。 在那里也替换名称空间和类名称。
在窗体/用户控件上具有KeyboardButton
,只需编写诸如saveButton.SimulateButtonUp()
类的代码,就可以从窗体/用户控件内调用SimulateButtonDown
和SimulateButtonUp
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.