简体   繁体   English

在 C# 中以编程方式激活 KeyDown 事件

[英]Activating KeyDown Events programmatically in C#

I have two Classes.我有两个班级。 MyClass.cs and another Class.cs MyClass.cs和另一个Class.cs

MyClass.cs contains textbox1 , and KeyDown event " textbox1_Keydown(object sender, KeyEventArgs e) ". MyClass.cs包含textbox1KeyDown事件“ textbox1_Keydown(object sender, KeyEventArgs e) ”。

My question is, how do I activate the Keydown event of textbox1 from Another Class.cs ?我的问题是,我该如何激活的keydown事件textbox1从另一个Class.cs

There are ways to do it, but it will lead to code that is hard to maintain.有很多方法可以做到,但这会导致代码难以维护。

I assume that you don't really want to trigger the event, but that you want to get the same behaviour as if you triggered the event.我假设您并不是真的想触发事件,而是希望获得与触发事件相同的行为。

My suggestion is to move the code in textbox1_Keydown() to a method in another class and call that method from both textbox1_Keydown() and Class.cs .我的建议是将textbox1_Keydown()的代码移动到另一个类中的方法,并从textbox1_Keydown()Class.cs调用该方法。

You can do that in your aspx code behind c# code.您可以在 c# 代码后面的 aspx 代码中执行此操作。 However if you want to use controllers in new c# class you can follow these steps.但是,如果您想在新的 c# 类中使用控制器,您可以按照以下步骤操作。

<%@ Page Title="" Language="C#" MasterPageFile="~/CRM/CRM.Master" AutoEventWireup="true" CodeBehind="CRMTRN02.aspx.cs" Inherits="CRM.CRMTRN02" %>
<%@ Page Title="" Language="C#" MasterPageFile="~/CRM/CRM.Master" AutoEventWireup="true" CodeBehind="Class1.cs" Inherits="CRM.C" %>

Add like these if you use master page.First line is already added when page created.如果您使用母版页,请添加这些内容。创建页面时已经添加了第一行。 You can add second line like above.您可以像上面一样添加第二行。
Then in your Class1.cs然后在你的 Class1.cs

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

namespace XONTCRM
{
public partial class C
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
}

Now you can access your controllers in aspx page in your Class1.cs file.现在您可以在 Class1.cs 文件的 aspx 页面中访问您的控制器。 you can enable disabel and do whatever you think.....您可以启用禁用并做您想做的任何事情.....

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

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